Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Apr 5, 2025

Fixes #18862. Fixes #17686.

This comment has been minimized.

@sterliakov sterliakov marked this pull request as ready for review April 5, 2025 01:55
@bzoracler
Copy link
Contributor

Thank you, I tried your changes locally and I believe it also fixes #17686.

@sterliakov
Copy link
Collaborator Author

Missed that one, thanks!

This comment has been minimized.

@sterliakov sterliakov changed the title Support type aliases in TypeVar defaults Support type aliases, NamedTuple and TypedDict in constrained TypeVar defaults Apr 6, 2025
Copy link
Collaborator

@JukkaL JukkaL left a 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)
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@A5rocks A5rocks left a 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)
Copy link
Collaborator

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?

Copy link
Collaborator Author

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:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants