Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit inferring Self for unannotated self #1860

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions docs/spec/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ general type possible, or ignored, by any type checker.
It is recommended but not required that checked functions have
annotations for all arguments and the return type. For a checked
function, the default annotation for arguments and for the return type
is ``Any``. An exception is the first argument of instance and
class methods. If it is not annotated, then it is assumed to have the
type of the containing class for instance methods, and a type object
type corresponding to the containing class object for class methods.
For example, in class ``A`` the first argument of an instance method
has the implicit type ``A``. In a class method, the precise type of
the first argument cannot be represented using the available type
notation.
is ``Any``. An exception to the above is the first argument of
instance and class methods (conventionally named ``self`` or ``cls``),
which type checkers should assume to have an appropriate type, as per
:ref:`annotating-methods`.

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

In most cases the first argument of class and instance methods
does not need to be annotated, and it is assumed to have the
type of the containing class for instance methods, and a type object
type corresponding to the containing class object for class methods.
(conventionally named ``self`` or ``cls``) does not need to be annotated.

If the argument is not annotated, then for instance methods it is
assumed to have the type of the containing class or :ref:`Self
<self>`, and for class methods the type object type corresponding to
the containing class object or ``type[Self]``. For example, in class
``A`` the first argument of an instance method has the implicit type
``A``. In a class method, the precise type of the first argument
cannot be represented using the available type notation.
Copy link
Contributor

@stroxler stroxler Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure what this last sentence means.

I think type[Self] is a fairly precise type - it's a type wrapping a type variable bound above by type[<current-class>] but can be narrowed on callsites where we know more.

That seems as precise as I can imagine being? And I don't think it's any less precise than the type of self, which behaves similarly.

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sentence is from PEP 484, it confused me as well. I thought it was maybe talking about the usual thing where args to cls(...) are hopeless, but I don't really know. Maybe we just remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sentence makes no sense to me either, I think we should remove it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for the last sentence, I think it looks reasonable.

I do think the spec should eventually be updated to mandate that self has the implied type Self and cls has the implied type type[Self]. Many important use cases involving the Self type break if this isn't the case. In retrospect, PEP 673 (which introduced the Self type) should have specified this IMO. But if you'd prefer to hold off on that change, then your proposed wording seems fine.


In addition, the first argument in an instance method can be annotated
with a type variable. In this case the return type may use the same
type variable, thus making that method a generic function. For example::
Expand Down