Skip to content

Commit 6fd40ef

Browse files
committed
Permit inferring Self for unannotated self
1 parent 21d67ca commit 6fd40ef

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

docs/spec/annotations.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ general type possible, or ignored, by any type checker.
2727
It is recommended but not required that checked functions have
2828
annotations for all arguments and the return type. For a checked
2929
function, the default annotation for arguments and for the return type
30-
is ``Any``. An exception is the first argument of instance and
31-
class methods. If it is not annotated, then it is assumed to have the
32-
type of the containing class for instance methods, and a type object
33-
type corresponding to the containing class object for class methods.
34-
For example, in class ``A`` the first argument of an instance method
35-
has the implicit type ``A``. In a class method, the precise type of
36-
the first argument cannot be represented using the available type
37-
notation.
30+
is ``Any``. An exception to the above is the first argument of
31+
instance and class methods (conventionally named ``self`` or ``cls``),
32+
which type checkers should assume to have an appropriate type, as per
33+
:ref:`annotating-methods`.
3834

3935
(Note that the return type of ``__init__`` ought to be annotated with
4036
``-> None``. The reason for this is subtle. If ``__init__`` assumed
@@ -354,9 +350,16 @@ Annotating instance and class methods
354350
-------------------------------------
355351

356352
In most cases the first argument of class and instance methods
357-
does not need to be annotated, and it is assumed to have the
358-
type of the containing class for instance methods, and a type object
359-
type corresponding to the containing class object for class methods.
353+
(conventionally named ``self`` or ``cls``) does not need to be annotated.
354+
355+
If the argument is not annotated, then for instance methods it is
356+
assumed to have the type of the containing class or :ref:`Self
357+
<self>`, and for class methods the type object type corresponding to
358+
the containing class object or ``type[Self]``. For example, in class
359+
``A`` the first argument of an instance method has the implicit type
360+
``A``. In a class method, the precise type of the first argument
361+
cannot be represented using the available type notation.
362+
360363
In addition, the first argument in an instance method can be annotated
361364
with a type variable. In this case the return type may use the same
362365
type variable, thus making that method a generic function. For example::

0 commit comments

Comments
 (0)