You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorporate typing_extensions into type checkers #19
Type annotations in some cases are limited and thus it would be good to enhance them with what the typing-extensions package has to offer.
One example of this would be @classmethod, which always uses cls argument as the first argument. Ideally, we would annotate it with the Self type from the typing-extensions package.
Proposed solution
Use typing-extensions package to enhance type annotations.
Use cases
This can be applied to @classmethods and perhaps other functions used as decorators. One apparent case where we could use better type checking is the @actor decorator, which decorates classes at runtime making it impossible for pylint to verify if attributes / methods are defined on the decorated class.
Alternatives and workarounds
In case of @classmethod, we an omit the type annotation for the cls argument and it still passes type checking.
Otherwise, we have to explicitly tell type checkers pylint and/or mypy that a particular line should be ignored, because there is no correct type annotation available.
Using Self is particularly useful for @classmethods acting as alternative constructors, because the cls argument should a special case in type checkers, as it is known that it always have to be the type of the class defining the @classmethod.
But for @classmethods acting as alternative constructors, you also need to return the type of the class defining the @classmethod, and this has to work too for subclasses not re-defining the alternative constructor.
As a workaround we can use the class name explicitly.
typing-extensions is not a random package, is an official Python package maintained by the Python organization and only includes features backported from newer Python versions so they are available for older versions. Thus the use of this package is by definition temporary (until we can depend on a new enough Python minimal version)
Activity
leandro-lucarella-frequenz commentedon Oct 6, 2022
One important clarification:
Using
Selfis particularly useful for@classmethods acting as alternative constructors, because theclsargument should a special case in type checkers, as it is known that it always have to be the type of the class defining the@classmethod.But for
@classmethods acting as alternative constructors, you also need to return the type of the class defining the@classmethod, and this has to work too for subclasses not re-defining the alternative constructor.As a workaround we can use the class name explicitly.
For example:
This should fail a type checking, because
T.make()is marked to return aS, not aT. But this should work:Note: this is untested because
mypydoesn't supportSelfyet, but is what I assume it should happen :)leandro-lucarella-frequenz commentedon Oct 7, 2022
Some other important clarifications:
Selfis introduced in Python 3.11.typing-extensionsis not a random package, is an official Python package maintained by the Python organization and only includes features backported from newer Python versions so they are available for older versions. Thus the use of this package is by definition temporary (until we can depend on a new enough Python minimal version)mypydoesn't supportSelfyet, this is why we didn't go for it. There is a PR, so support should come fairly soon. Implementation of PEP 673 (typing.Self) python/mypy#11666leandro-lucarella-frequenz commentedon Apr 21, 2023
We already have
typing_extensionsas a dependency and mypy already supportsSelf, we just need to start using it.