Skip to content

Expand TypeVarTuple default (PEP 696) #16851

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 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ def visit_unpack_type(self, t: UnpackType) -> Type:
def expand_unpack(self, t: UnpackType) -> list[Type]:
assert isinstance(t.type, TypeVarTupleType)
repl = get_proper_type(self.variables.get(t.type.id, t.type))
if isinstance(repl, UnpackType):
repl = get_proper_type(repl.type)
if isinstance(repl, TupleType):
return repl.items
elif (
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,7 @@ def make_call(*items: tuple[str, str | None]) -> CallExpr:
class TestExpandTypeLimitGetProperType(TestCase):
# WARNING: do not increase this number unless absolutely necessary,
# and you understand what you are doing.
ALLOWED_GET_PROPER_TYPES = 8
ALLOWED_GET_PROPER_TYPES = 9
Comment on lines 1542 to +1544
Copy link
Collaborator Author

@cdce8p cdce8p Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelleZijlstra The test here just counts all get_proper_type calls inside expandtype. I believe the change still makes sense and is necessary to be able to compare the type inside Unpack. Furthermore, it's guarded by the isinstance check so the performance penalty should probably be minimal. Let me know what you think.

No other test case so far would reach that, only the new TypeVarTuple default ones.


@skipUnless(mypy.expandtype.__file__.endswith(".py"), "Skip for compiled mypy")
def test_count_get_proper_type(self) -> None:
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-typevar-defaults.test
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ def func_c1(
# reveal_type(a) # Revealed type is "__main__.ClassC1[builtins.int, builtins.str]" # TODO
reveal_type(b) # N: Revealed type is "__main__.ClassC1[builtins.float]"

# k = ClassC1() # TODO
# reveal_type(k) # Revealed type is "__main__.ClassC1[builtins.int, builtins.str]" # TODO
k = ClassC1()
reveal_type(k) # N: Revealed type is "__main__.ClassC1[builtins.int, builtins.str]"
l = ClassC1[float]()
reveal_type(l) # N: Revealed type is "__main__.ClassC1[builtins.float]"

Expand All @@ -290,8 +290,8 @@ def func_c2(
# reveal_type(b) # Revealed type is "__main__.ClassC2[builtins.int, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
reveal_type(c) # N: Revealed type is "__main__.ClassC2[builtins.int]"

# k = ClassC2() # TODO
# reveal_type(k) # Revealed type is "__main__.ClassC2[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
k = ClassC2()
reveal_type(k) # N: Revealed type is "__main__.ClassC2[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]"
l = ClassC2[int]()
# reveal_type(l) # Revealed type is "__main__.ClassC2[builtins.int, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
m = ClassC2[int, Unpack[Tuple[()]]]()
Expand All @@ -308,8 +308,8 @@ def func_c3(
reveal_type(b) # N: Revealed type is "__main__.ClassC3[builtins.int]"
reveal_type(c) # N: Revealed type is "__main__.ClassC3[builtins.int, builtins.float]"

# k = ClassC3() # TODO
# reveal_type(k) # Revealed type is "__main__.ClassC3[builtins.str]" # TODO
k = ClassC3()
reveal_type(k) # N: Revealed type is "__main__.ClassC3[builtins.str]"
l = ClassC3[int]()
reveal_type(l) # N: Revealed type is "__main__.ClassC3[builtins.int]"
m = ClassC3[int, Unpack[Tuple[float]]]()
Expand Down