-
- Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
0f61182 Cleanup generic class variable access
ilevkivskyi 6f8b2d2 Merge remote-tracking branch 'upstream/master' into fix-generic-classvar
ilevkivskyi 6cbfd18 Merge remote-tracking branch 'upstream/master' into fix-generic-classvar
ilevkivskyi 0a3a081 Merge remote-tracking branch 'upstream/master' into fix-generic-classvar
ilevkivskyi 3d56dd4 Address CR (more tests)
ilevkivskyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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: | ||
| Collaborator 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 | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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, ...]" | ||
| Collaborator 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 | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.yhave typeintthen?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.