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
Because of the circumstances under which __set_name__ is invoked, it should be possible to perform type inference on its owner argument, in a way which could be used to statically typecheck other components of a descriptor.
As a brief motivating example, below is a sketch of descriptor for a validated mutable attribute of integer type:
In Pylance with strict typechecking rules, the definition of the x descriptor raises the following errors:
reportUnknownMemberType
Type of "validate" is unknown
reportUnknownLambdaType
Return type of lambda is unknown
reportGeneralTypeIssues
Cannot access member "validate" for type "object*"
Member "validate" is unknown
Currently, Mypy doesn't raise any errors about the lack of lambda typing (not even for incorrect implementations, such as self.validatez(value) or self.validate(str(value))). Mypy also doesn't raise errors about incorrect lambda implementations once an explicit type hint is provided (see below), while Pylance does.
I opened a separate issue about this: #16797
Explicitly providing a hint for the InstanceT type removes the errors for Pylance:
Performing inference on the owner argument to IntAttr.__set_name__ in the context of class C would ideally result in C being inferred as a value for InstanceT, allowing for static typecheking of the validator lambda function without the need for an explicit type hint.
Because of the circumstances under which
__set_name__
is invoked, it should be possible to perform type inference on itsowner
argument, in a way which could be used to statically typecheck other components of a descriptor.As a brief motivating example, below is a sketch of descriptor for a validated mutable attribute of integer type:
In Pylance with strict typechecking rules, the definition of the
x
descriptor raises the following errors:Currently, Mypy doesn't raise any errors about the lack of lambda typing (not even for incorrect implementations, such as
self.validatez(value)
orself.validate(str(value))
). Mypy also doesn't raise errors about incorrect lambda implementations once an explicit type hint is provided (see below), while Pylance does.I opened a separate issue about this: #16797
Explicitly providing a hint for the
InstanceT
type removes the errors for Pylance:Performing inference on the
owner
argument toIntAttr.__set_name__
in the context ofclass C
would ideally result inC
being inferred as a value forInstanceT
, allowing for static typecheking of thevalidator
lambda function without the need for an explicit type hint.Related issues for Mypy:
Sister issue for Pylance/Pyright: microsoft/pyright#7039
The text was updated successfully, but these errors were encountered: