-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
cached_property self type #9464
Comments
Could someone from the pylance team please transfer this to the pyright project? Thanks! |
Let's take a closer look at what's going on here. I'm going to simplify your example and copy a simplified version of from typing import Any, Callable, Self
class cached_property[T]:
def __init__(self, func: Callable[[Any], T]) -> None: ...
def __get__(self, instance: object, owner: type[Any] | None = None) -> T: ...
class CachedBug:
@cached_property
def z(self) -> Self:
return self.__class__()
bug = CachedBug()
reveal_type(bug.z) # Type is "Any" The class CachedBug:
@cached_property
def z[S: CachedBug](self: S) -> S:
return self.__class__() This replacement makes it clearer what's happening. When the function S <= Any
T >= S The solution to this set of constraints is Interestingly, mypy (another Python type checker) appears to treat the type variable from typing import Any, Callable, Self
class cached_property[T]:
def __init__(self, func: Callable[[Any], T]) -> None: ...
def __get__(self, instance: object, owner: type[Any] | None = None) -> T: ...
class CachedBug:
@cached_property
def z1[S: CachedBug](self: S) -> S:
return self.__class__()
@cached_property
def z2[S: CachedBug](self) -> Self:
return self.__class__()
bug = CachedBug()
reveal_type(bug.z1) # Mypy says: "Any", pyright says: "Any"
reveal_type(bug.z2) # Mypy says: "CachedBug", pyright says: "Any" In summary, I think pyright is doing the right thing here — a behavior that is consistent with the Python typing spec. I understand that the resulting behavior is less than satisfying in this particular use case, but I don't think I can justify deviating from the typing spec to change the behavior. I'm therefore going to close this issue. |
Environment data
Code Snippet
Repro Steps
Expected behavior
pylance can know "z" is CachedBug type
Actual behavior
"z" is Any type
Logs
The text was updated successfully, but these errors were encountered: