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
__get__ should only be understood to be invoked on descriptors stored in a type's dictionary. But mypy type-checks as if there is a default present. This makes any infrastructure that wraps a decorator impossible to type correctly.
fromdataclassesimportdataclassclassA:
passclassB:
def__get__(self, instance: A, owner: None=None) ->int:
return3def__set__(self, instance: A, value: int) ->None:
print("set:", value)
@dataclassclassW:
b: Bdefset_b(self, b: B) ->None:
self.b=bw=W(B())
w.set_b(B())
print(w.b)
Expected Behavior
It should type-check without errors, as Python runs it fine.
Actual Behavior
main.py:19: error: Argument 1 to "__get__" of "B" has incompatible type "W"; expected "A" [arg-type]
main.py:19: error: Argument 2 to "__get__" of "B" has incompatible type "type[W]"; expected "None" [arg-type]
main.py:19: error: Argument 1 to "__set__" of "B" has incompatible type "W"; expected "A" [arg-type]
main.py:19: error: Incompatible types in assignment (expression has type "B", variable has type "int") [assignment]
Found 4 errors in 1 file (checked 1 source file)
Your Environment
Mypy version used: 1.4.1
Mypy command-line flags: --strict
Python version used: 3.11
The text was updated successfully, but these errors were encountered:
IMO, Mypy should invoke the descriptor protocol only when the variable is a ClassVar (implicit or explicit):
classFoo:
a: ClassVar[Descriptor] # Variable type is the return type of Descriptor.__get__b=Descriptor() # Implicit ClassVar descriptorc: Descriptor# The type is just `Descriptor`
Bug Report
__get__
should only be understood to be invoked on descriptors stored in a type's dictionary. But mypy type-checks as if there is a default present. This makes any infrastructure that wraps a decorator impossible to type correctly.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=9483880764b49d2ce31d4156c9f4190a
Expected Behavior
It should type-check without errors, as Python runs it fine.
Actual Behavior
Your Environment
--strict
The text was updated successfully, but these errors were encountered: