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
JukkaL
left a comment
There was a problem hiding this comment.
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.
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.
Done! Added basic usage to several tests and also a testcase with py3.13 defaults syntax.
This comment has been minimized.
This comment has been minimized.
A5rocks
left a comment
There was a problem hiding this comment.
This makes sense to me!
| 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.
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.
Yes, I wasn't able to get rid of one of these checks, but made a note to myself to try harder later:)
…8862-typevar-default-alias
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #18862. Fixes #17686.