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
We need to understand the descriptor protocol in order to infer proper types for attribute accesses like in this simple example:
fromtypingimportLiteralclassTen:
def__get__(self, instance: object, owner: type|None=None) ->Literal[10]:
return10def__set__(self, instance: object, value: Literal[10]) ->None:
passclassC:
ten=Ten()
c=C()
# TODO: this should be `Literal[10]`reveal_type(c.ten) # revealed: Unknown | Ten# TODO: This should `Literal[10]`reveal_type(C.ten) # revealed: Unknown | Ten# These are fine:c.ten=10C.ten=10# TODO: Both of these should be errorsc.ten=11C.ten=11
An important case to consider here that isn't shown in the example in summary is where the value argument to __set__ does not have the same type as the return type of __get__, meaning we need to use a different type for type-checking assignments to the attribute than we use for inferring the type of an attribute access.
## Summary
This is a first step towards creating a test suite for
[descriptors](https://docs.python.org/3/howto/descriptor.html). It does
not (yet) aim to be exhaustive.
relevant ticket: #15966
## Test Plan
Compared desired behavior with the runtime behavior and the behavior of
existing type checkers.
---------
Co-authored-by: Mike Perlov <[email protected]>
We need to understand the descriptor protocol in order to infer proper types for attribute accesses like in this simple example:
References: Python Documentation: Descriptor Guide
part of: #14164
The text was updated successfully, but these errors were encountered: