Skip to content

Commit 3b987a9

Browse files
hauntsaninjacarljm
andauthored
Permit inferring Self for unannotated self (#1860)
Co-authored-by: Carl Meyer <[email protected]>
1 parent d25bc6c commit 3b987a9

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

docs/spec/annotations.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ functions with no annotations, but this behavior is not required.
3232
It is recommended but not required that checked functions have
3333
annotations for all arguments and the return type. For a checked
3434
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`.
4339

4440
(Note that the return type of ``__init__`` ought to be annotated with
4541
``-> None``. The reason for this is subtle. If ``__init__`` assumed
@@ -358,10 +354,14 @@ types cannot be specified::
358354
Annotating instance and class methods
359355
-------------------------------------
360356

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+
365365
In addition, the first argument in an instance method can be annotated
366366
with a type variable. In this case the return type may use the same
367367
type variable, thus making that method a generic function. For example::

0 commit comments

Comments
 (0)