-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support type aliases, NamedTuple
and TypedDict
in constrained TypeVar defaults
#18884
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
1fed961
c7b9670
a871b87
57799f1
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 |
---|---|---|
|
@@ -2042,3 +2042,27 @@ tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one Unpack in a type i | |
b: tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one Unpack in a type is not allowed | ||
[builtins fixtures/tuple.pyi] | ||
[typing fixtures/typing-full.pyi] | ||
|
||
[case testPEP695TypeVarConstraintsDefaultAliases] | ||
from typing import Generic | ||
from typing_extensions import TypeVar | ||
|
||
type K = int | ||
type V = int | ||
type L = list[int] | ||
|
||
T1 = TypeVar("T1", str, K, default=K) | ||
T2 = TypeVar("T2", str, K, default=V) | ||
T3 = TypeVar("T3", str, L, default=L) | ||
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 using the type variable? E.g. define generic class using one of the type variables and check how the default works. Not sure if it's important to test it in every test case though. 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. Done! Added basic usage to several tests and also a testcase with py3.13 defaults syntax. |
||
|
||
class A1(Generic[T1]): | ||
x: T1 | ||
class A2(Generic[T2]): | ||
x: T2 | ||
class A3(Generic[T3]): | ||
x: T3 | ||
|
||
reveal_type(A1().x) # N: Revealed type is "builtins.int" | ||
reveal_type(A2().x) # N: Revealed type is "builtins.int" | ||
reveal_type(A3().x) # N: Revealed type is "builtins.list[builtins.int]" | ||
[builtins fixtures/tuple.pyi] |
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.
It's a bit strange to have this logic duplicated! Is this because PEP 695 doesn't use
TypeVarExpr
so checkexpr will never hit this?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.
Yes, I wasn't able to get rid of one of these checks, but made a note to myself to try harder later:)