-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Cleanup generic class variable access #19292
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
base: master
Are you sure you want to change the base?
Changes from all commits
0f61182
6f8b2d2
6cbfd18
0a3a081
3d56dd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2220,6 +2220,28 @@ class C(A, B): # OK: both methods take Self | |
pass | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testSelfTypeClassMethodNotSilentlyErased] | ||
from typing import Self, Optional | ||
|
||
class X: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test subclassing and accessing |
||
_inst: Optional[Self] = None | ||
@classmethod | ||
def default(cls) -> Self: | ||
reveal_type(cls._inst) # N: Revealed type is "Union[Self`0, None]" | ||
if cls._inst is None: | ||
cls._inst = cls() | ||
return cls._inst | ||
|
||
reveal_type(X._inst) # E: Access to generic instance variables via class is ambiguous \ | ||
# N: Revealed type is "Union[__main__.X, None]" | ||
reveal_type(X()._inst) # N: Revealed type is "Union[__main__.X, None]" | ||
|
||
class Y(X): ... | ||
reveal_type(Y._inst) # E: Access to generic instance variables via class is ambiguous \ | ||
# N: Revealed type is "Union[__main__.Y, None]" | ||
reveal_type(Y()._inst) # N: Revealed type is "Union[__main__.Y, None]" | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testSelfInFuncDecoratedClassmethod] | ||
from collections.abc import Callable | ||
from typing import Self, TypeVar | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2629,6 +2629,28 @@ def test(*args: Unpack[tuple[T]]) -> int: ... | |
reveal_type(fn(test)) # N: Revealed type is "def [T] (T`1) -> builtins.int" | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testNoGenericTypeVarTupleClassVarAccess] | ||
from typing import Generic, Tuple, TypeVarTuple, Unpack | ||
|
||
Ts = TypeVarTuple("Ts") | ||
class C(Generic[Unpack[Ts]]): | ||
x: Tuple[Unpack[Ts]] | ||
|
||
reveal_type(C.x) # E: Access to generic instance variables via class is ambiguous \ | ||
# N: Revealed type is "builtins.tuple[Any, ...]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test also defining a subclass and then accessing the attribute via the subclass, and an instance of the subclass? |
||
|
||
class Bad(C[int, int]): | ||
pass | ||
reveal_type(Bad.x) # E: Access to generic instance variables via class is ambiguous \ | ||
# N: Revealed type is "tuple[builtins.int, builtins.int]" | ||
reveal_type(Bad().x) # N: Revealed type is "tuple[builtins.int, builtins.int]" | ||
|
||
class Good(C[int, int]): | ||
x = (1, 1) | ||
reveal_type(Good.x) # N: Revealed type is "tuple[builtins.int, builtins.int]" | ||
reveal_type(Good().x) # N: Revealed type is "tuple[builtins.int, builtins.int]" | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testConstraintsIncludeTupleFallback] | ||
from typing import Generic, TypeVar | ||
from typing_extensions import TypeVarTuple, Unpack | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to also test attribute access in cases like
class D(B[int]): ...
. ShouldD.y
have typeint
then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some tests (here and above). As I mentioned in the PR description the way we consider some use cases safe, and some unsafe is a bit arbitrary (but maybe there are reasons for this and I just don't remember). Here I am not changing any of that, just making sure that we don't leak type variables, don't silently erase them, etc.