-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
support pep 673 Self type #11871
Copy link
Copy link
Closed
Labels
affects-typeshedAnything that blocks a typeshed changeAnything that blocks a typeshed changefeaturemetaIssues tracking a broad area of workIssues tracking a broad area of worktopic-self-typesTypes for selfTypes for self
Metadata
Metadata
Assignees
Labels
affects-typeshedAnything that blocks a typeshed changeAnything that blocks a typeshed changefeaturemetaIssues tracking a broad area of workIssues tracking a broad area of worktopic-self-typesTypes for selfTypes for self
Feature
Support the pep-673 Self type
pep is at: https://www.python.org/dev/peps/pep-0673/
a rough idea of it is that this code:
is quasi-equivalent to:
Pitch
the appeal of the pep is that for the very common pattern of method-chained object construction, methods can easily indicate they return an object of the same type upon which the method is being invoked. For example if I made
class SubWidget(Widget), theSubWidget.add_thing()method reportsSubWidgetas the return type automatically. The pattern using TypeVar seems to be more or less equivlant, but is more verbose requiring the declaration of TypeVar objects per class hierarchy as well as that it has to be explicitly present on the "self" parameter in methods.We are looking for this feature to make our job of integrating typing into SQLAlchemy an easier job. it seems to be a fairly straightforward translation between two idioms.
cc @CaselIT