-
-
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?
Support type aliases, NamedTuple
and TypedDict
in constrained TypeVar defaults
#18884
Conversation
This comment has been minimized.
This comment has been minimized.
Thank you, I tried your changes locally and I believe it also fixes #17686. |
Missed that one, thanks! |
This comment has been minimized.
This comment has been minimized.
NamedTuple
and TypedDict
in constrained TypeVar defaults
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.
Thanks for the PR! Left a suggestion about additional testing, otherwise looks good.
|
||
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 comment
The 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 comment
The 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.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
This makes sense to me!
@@ -6210,7 +6210,7 @@ def visit_type_var_expr(self, e: TypeVarExpr) -> Type: | |||
): | |||
if not is_subtype(p_default, e.upper_bound): | |||
self.chk.fail("TypeVar default must be a subtype of the bound type", e) | |||
if e.values and not any(p_default == value for value in e.values): | |||
if e.values and not any(is_same_type(p_default, value) for value in e.values): | |||
self.chk.fail("TypeVar default must be one of the constraint types", e) |
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:)
Fixes #18862. Fixes #17686.