@@ -32,14 +32,10 @@ functions with no annotations, but this behavior is not required.
32
32
It is recommended but not required that checked functions have
33
33
annotations for all arguments and the return type. For a checked
34
34
function, the default annotation for arguments and for the return type
35
- is ``Any ``. An exception is the first argument of instance and
36
- class methods. If it is not annotated, then it is assumed to have the
37
- type of the containing class for instance methods, and a type object
38
- type corresponding to the containing class object for class methods.
39
- For example, in class ``A `` the first argument of an instance method
40
- has the implicit type ``A ``. In a class method, the precise type of
41
- the first argument cannot be represented using the available type
42
- notation.
35
+ is ``Any ``. An exception to the above is the first argument of
36
+ instance and class methods (conventionally named ``self `` or ``cls ``),
37
+ which type checkers should assume to have an appropriate type, as per
38
+ :ref: `annotating-methods `.
43
39
44
40
(Note that the return type of ``__init__ `` ought to be annotated with
45
41
``-> None ``. The reason for this is subtle. If ``__init__ `` assumed
@@ -358,10 +354,14 @@ types cannot be specified::
358
354
Annotating instance and class methods
359
355
-------------------------------------
360
356
361
- In most cases the first argument of class and instance methods
362
- does not need to be annotated, and it is assumed to have the
363
- type of the containing class for instance methods, and a type object
364
- type corresponding to the containing class object for class methods.
357
+ In most cases the first argument of instance and class methods
358
+ (conventionally named ``self `` or ``cls ``) does not need to be annotated.
359
+
360
+ If the argument is not annotated, then for instance methods it may be
361
+ inferred to have either the type of the containing class, or the type :ref: `Self
362
+ <self>`. For class methods it may be inferred to have either the type object
363
+ type corresponding to the containing class object, or ``type[Self] ``.
364
+
365
365
In addition, the first argument in an instance method can be annotated
366
366
with a type variable. In this case the return type may use the same
367
367
type variable, thus making that method a generic function. For example::
0 commit comments