Skip to content
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
6 changes: 4 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4409,8 +4409,10 @@ def check_multi_assignment_from_tuple(
# inferred return type for an overloaded function
# to be ambiguous.
return
assert isinstance(reinferred_rvalue_type, TupleType)
rvalue_type = reinferred_rvalue_type
if isinstance(reinferred_rvalue_type, TupleType):
# This branch will usually be taken, but in some cases context can
# e.g. select a different overload
rvalue_type = reinferred_rvalue_type

left_rv_types, star_rv_types, right_rv_types = self.split_around_star(
rvalue_type.items, star_index, len(lvalues)
Expand Down
36 changes: 36 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,42 @@ class A: pass
class B: pass
[builtins fixtures/tuple.pyi]

[case testMultipleAssignmentWithOverloadReinferredAsHomogeneousTuple]
from typing import Any, TypeVar, overload

T = TypeVar("T")

@overload
def f(value: T) -> tuple[T, int]: ...
@overload
def f(*values: object) -> tuple[Any, ...]: ...
def f(*values: object, **kwargs: object) -> object: ...

def g() -> None:
first: str
first, second = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
reveal_type(second) # N: Revealed type is "builtins.int"

last: str
_, last = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/tuple.pyi]

[case testMultipleAssignmentWithOverloadReinferredAsNonTuple]
from typing import TypeVar, overload

T = TypeVar("T")

@overload
def f(value: T) -> tuple[T]: ...
@overload
def f(*values: object) -> int: ...
def f(*values: object, **kwargs: object) -> object: ...

def g() -> None:
value: str
(value,) = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/tuple.pyi]

[case testMultipleAssignmentWithSquareBracketTuples]
# flags: --no-strict-optional
from typing import Tuple
Expand Down
Loading