Skip to content

Commit

Permalink
Expand TypeVarTuple default (PEP 696) (#16851)
Browse files Browse the repository at this point in the history
Small change to fix an error when expanding a TypeVarTuple default.

```
RuntimeError: Invalid type replacement to expand: Unpack[tuple[builtins.int, builtins.str]]
```

Ref: #14851
  • Loading branch information
cdce8p committed Feb 3, 2024
1 parent 3f58c2d commit 3804f7e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
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

@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 @@ -289,8 +289,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 @@ -305,8 +305,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 @@ -323,8 +323,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

0 comments on commit 3804f7e

Please sign in to comment.