Skip to content

Commit f4c6548

Browse files
committed
Fix inference when unpacking union type
1 parent 186515f commit f4c6548

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

mypy/argmap.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TypeOfAny,
1818
TypeVarTupleType,
1919
UnpackType,
20+
UnionType,
2021
get_proper_type,
2122
)
2223

@@ -211,6 +212,18 @@ def expand_actual_type(
211212
# Just return `Any`, other parts of code would raise
212213
# a different error for improper use.
213214
return AnyType(TypeOfAny.from_error)
215+
elif isinstance(actual_type, UnionType):
216+
item_types = [
217+
self.expand_actual_type(
218+
item,
219+
actual_kind=actual_kind,
220+
formal_name=formal_name,
221+
formal_kind=formal_kind,
222+
allow_unpack=allow_unpack,
223+
)
224+
for item in actual_type.items
225+
]
226+
return UnionType.make_union(item_types)
214227
elif isinstance(actual_type, TupleType):
215228
# Get the next tuple item of a tuple *arg.
216229
if self.tuple_index >= len(actual_type.items):

test-data/unit/check-functions.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,3 +3708,20 @@ foo(*args) # E: Argument 1 to "foo" has incompatible type "*list[object]"; expe
37083708
kwargs: dict[str, object]
37093709
foo(**kwargs) # E: Argument 1 to "foo" has incompatible type "**dict[str, object]"; expected "P"
37103710
[builtins fixtures/dict.pyi]
3711+
3712+
[case testUnpackUnionStarArgs]
3713+
from __future__ import annotations
3714+
from typing import TypeVar
3715+
T = TypeVar("T")
3716+
3717+
def f(*args: T) -> T: ...
3718+
3719+
def star_union_list(x: list[str | None] | list[str]):
3720+
reveal_type([*x]) # N: Revealed type is "builtins.list[Union[builtins.str, None]]"
3721+
reveal_type(f(*x)) # N: Revealed type is "Union[builtins.str, None]"
3722+
3723+
def star_union_list_tuple(x: list[str | None] | tuple[int, int]):
3724+
reveal_type([*x]) # N: Revealed type is "builtins.list[Union[builtins.str, None, builtins.int]]"
3725+
reveal_type(f(*x)) # N: Revealed type is "Union[builtins.str, None, builtins.int]"
3726+
3727+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)