-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pylance does not seem to understand customized descriptor __get__ method. #6472
Comments
The problem here is that the type variable You can solve this problem by swapping the order of your overloads. from typing import Any, ClassVar, Self, overload
class ClassAttr[AttrType]:
def __init__(self): ...
@overload
def __get__(self, owner_obj: None, owner_type: type[Any]) -> AttrType: ...
@overload
def __get__[T](self, owner_obj: T, owner_type: type[T] | None = None) -> Self: ...
def __get__[T](self, owner_obj: T | None, owner_type: type[T] | None = None) -> AttrType | Self: ...
class T:
name: ClassVar[ClassAttr[str]]
x1 = T.name
reveal_type(x1) # str
x2 = T().name
reveal_type(x2) # ClassAttr[str] I made a couple of other changes in the code sample that you might find instructive:
|
Absolutly astonishing! Thanks for your detailed and complete answer! never thought that the order of typing.overload would matter, TIL. Other tips are very helpful too! |
The doc string should explain what this class does well,
this 'ClassAttr' is expected to return generic 'AttrType' when it is accessed via class object, and return itself when accesed via instance object.
however, pylance seems to think that it would always return ClassAttr either way.
The text was updated successfully, but these errors were encountered: