Skip to content

Commit 52b1ff9

Browse files
author
Guido van Rossum
committed
Rewrite "What is checked" as "The meaning of annotations" per recommendations of Mark Shannon.
Fixes #110.
1 parent b612c8e commit 52b1ff9

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

pep-0484.txt

+28-31
Original file line numberDiff line numberDiff line change
@@ -96,41 +96,38 @@ typed language, and the authors have no desire to ever make type hints
9696
mandatory, even by convention.**
9797

9898

99-
What is checked?
100-
================
99+
The meaning of annotations
100+
==========================
101101

102-
Any function (or method -- for brevity we won't be repeating this)
103-
with at least one argument or return annotation is checked, unless
104-
type checking is disabled by the ``@no_type_check`` decorator or a
105-
``# type: ignore`` comment (see below).
102+
Any function without annotations should be treated as having the most
103+
general type possible, or ignored, by any type checker. Functions
104+
with the ``@no_type_check`` decorator or with a ``# type: ignore``
105+
comment should be treated as having no annotations.
106106

107-
It is recommended but not required that checked function have
108-
annotations for all its arguments and its return type. For a checked
107+
It is recommended but not required that checked functions have
108+
annotations for all arguments and the return type. For a checked
109109
function, the default annotation for arguments and for the return type
110110
is ``Any``. An exception is that the first argument of instance and
111-
class methods should not be annotated; it is assumed to have the type
112-
of the containing class for instance method, and ``type`` for class
113-
methods. Note that the return type of ``__init__`` ought to be
114-
annotated with ``-> None`` (there is no exception for ``__init__``).
115-
116-
The body of a checked function is checked for consistency with the
117-
given annotations. The annotations are also used to check correctness
118-
of calls appearing in other checked functions.
119-
120-
Functions without any annotations (or whose checking is disabled) are
121-
assumed to have type ``Any`` when they are referenced in checked
122-
functions, and this should completely silence complaints from the
123-
checker regarding those references (although a checker may still
124-
request that a type be specified using a cast or a ``# type:`` comment
125-
if a more specific type than ``Any`` is needed for analysis of
126-
subsequent code).
127-
128-
A type checker should understand decorators; this may require
129-
annotations on decorator definitions. In particular, a type checker
130-
should understand the built-in decorators ``@property``,
131-
``@staticmethod`` and ``@classmethod``. The first argument of a class
132-
method should not be annotated; it is assumed to be a subclass of the
133-
defining class.
111+
class methods does not need to be annotated; it is assumed to have the
112+
type of the containing class for instance methods, and ``type`` for
113+
class methods.
114+
115+
(Note that the return type of ``__init__`` ought to be annotated with
116+
``-> None``. The reason for this is subtle. If ``__init__`` assumed
117+
a return annotation of ``-> None``, would that mean that an
118+
argument-less, un-annotated ``__init__`` method should still be
119+
type-checked? Rather than leaving this ambiguous or introducing an
120+
exception to the exception, we simply say that ``__init__`` ought to
121+
have a return annotation; the default behavior is thus the same as for
122+
other methods.)
123+
124+
A type checker is expected to check the body of a checked function for
125+
consistency with the given annotations. The annotations may also used
126+
to check correctness of calls appearing in other checked functions.
127+
128+
Type checkers are expected to attempt to infer as much information as
129+
necessary. The minimum requirement is to handle the builtin
130+
decorators ``@property``, ``@staticmethod`` and ``@classmethod``.
134131

135132

136133
Type Definition Syntax

0 commit comments

Comments
 (0)