8000 Fix crash on unpack call special-casing (#16381) · python/mypy@681e54c · GitHub
[go: up one dir, main page]

Skip to content

Commit 681e54c

Browse files
ilevkivskyiJelleZijlstra
authored andcommitted
Fix crash on unpack call special-casing (#16381)
Fixes #16380 Fix is quite straightforward, what was an `assert` really needs to be an `if`. --------- Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent f68f463 commit 681e54c

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

mypy/checkexpr.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,34 +2440,28 @@ def check_argument_types(
24402440
# the suffices to the tuple, e.g. a single actual like
24412441
# Tuple[Unpack[Ts], int]
24422442
expanded_tuple = False
2443< 8000 code class="diff-text syntax-highlighted-line addition">+
actual_kinds = [arg_kinds[a] for a in actuals]
24432444
if len(actuals) > 1:
2444-
first_actual_arg_type = get_proper_type(arg_types[actuals[0]])
2445+
p_actual_type = get_proper_type(arg_types[actuals[0]])
24452446
if (
2446-
isinstance(first_actual_arg_type, TupleType)
2447-
and len(first_actual_arg_type.items) == 1
2448-
and isinstance(first_actual_arg_type.items[0], UnpackType)
2447+
isinstance(p_actual_type, TupleType)
2448+
and len(p_actual_type.items) == 1
2449+
and isinstance(p_actual_type.items[0], UnpackType)
2450+
and actual_kinds == [nodes.ARG_STAR] + [nodes.ARG_POS] * (len(actuals) - 1)
24492451
):
2450-
# TODO: use walrus operator
2451-
actual_types = [first_actual_arg_type.items[0]] + [
2452-
arg_types[a] for a in actuals[1:]
2453-
]
2454-
actual_kinds = [nodes.ARG_STAR] + [nodes.ARG_POS] * (len(actuals) - 1)
2455-
2456-
# If we got here, the callee was previously inferred to have a suffix.
2457-
assert isinstance(orig_callee_arg_type, UnpackType)
2458-
assert isinstance(orig_callee_arg_type.type, ProperType) and isinstance(
2459-
orig_callee_arg_type.type, TupleType
2460-
)
2461-
assert orig_callee_arg_type.type.items
2462-
callee_arg_types = orig_callee_arg_type.type.items
2463-
callee_arg_kinds = [nodes.ARG_STAR] + [nodes.ARG_POS] * (
2464-
len(orig_callee_arg_type.type.items) - 1
2465-
)
2466-
expanded_tuple = True
2452+
actual_types = [p_actual_type.items[0]] + [arg_types[a] for a in actuals[1:]]
2453+
if isinstance(orig_callee_arg_type, UnpackType):
2454+
p_callee_type = get_proper_type(orig_callee_arg_type.type)
2455+
if isinstance(p_callee_type, TupleType):
2456+
assert p_callee_type.items
2457+
callee_arg_types = p_callee_type.items
2458+
callee_arg_kinds = [nodes.ARG_STAR] + [nodes.ARG_POS] * (
2459+
len(p_callee_type.items) - 1
2460+
)
2461+
expanded_tuple = True
24672462

24682463
if not expanded_tuple:
24692464
actual_types = [arg_types[a] for a in actuals]
2470-
actual_kinds = [arg_kinds[a] for a in actuals]
24712465
if isinstance(orig_callee_arg_type, UnpackType):
24722466
unpacked_type = get_proper_type(orig_callee_arg_type.type)
24732467
if isinstance(unpacked_type, TupleType):

test-data/unit/check-typevar-tuple.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,3 +2185,25 @@ def test2(
21852185
# E: Missing named argument "b"
21862186
return func(*args, **kwargs)
21872187
[builtins fixtures/tuple.pyi]
2188+
2189+
[case testUnpackTupleSpecialCaseNoCrash]
2190+
from typing import Tuple, TypeVar
2191+
from typing_extensions import Unpack
2192+
2193+
T = TypeVar("T")
2194+
2195+
def foo(*x: object) -> None: ...
2196+
def bar(*x: int) -> None: ...
2197+
def baz(*x: T) -> T: ...
2198+
2199+
keys: Tuple[Unpack[Tuple[int, ...]]]
2200+
2201+
foo(keys, 1)
2202+
foo(*keys, 1)
2203+
2204+
bar(keys, 1) # E: Argument 1 to "bar" has incompatible type "Tuple[Unpack[Tuple[int, ...]]]"; expected "int"
2205+
bar(*keys, 1) # OK
2206+
2207+
reveal_type(baz(keys, 1)) # N: Revealed type is "builtins.object"
2208+
reveal_type(baz(*keys, 1)) # N: Revealed type is "builtins.int"
2209+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)
0