Skip to content
Open
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
17 changes: 16 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5514,9 +5514,24 @@ def visit_tuple_expr(self, e: TupleExpr) -> Type:
]
if len(tuples_in_context) == 1:
type_context = tuples_in_context[0]
elif (
len(tuples_in_context) > 1
and not any(isinstance(item, StarExpr) for item in e.items)
and all(
isinstance(t, TupleType)
and len(t.items) == len(e.items)
and find_unpack_in_list(t.items) is None
for t in tuples_in_context
)
):
tuple_types = [t for t in tuples_in_context if isinstance(t, TupleType)]
type_context_items = [
UnionType.make_union([t.items[k] for t in tuple_types])
for k in range(len(e.items))
]
else:
# There are either no relevant tuples in the Union, or there is
# more than one. Either way, we can't decide on a context.
# more than one with incompatible structure. Either way, we can't decide on a context.
pass

if isinstance(type_context, TupleType) and self.tuple_context_matches(e, type_context):
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2895,9 +2895,9 @@ def invalid_literal_type() -> Tuple[Literal[1]]:

def incorrect_return1() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]:
if x:
return (False, 5) # E: Incompatible return value type (got "tuple[bool, int]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]")
return (False, 5) # E: Incompatible return value type (got "tuple[Literal[False], int]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]")
else:
return (True, 'oops') # E: Incompatible return value type (got "tuple[bool, str]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]")
return (True, 'oops') # E: Incompatible return value type (got "tuple[Literal[True], str]", expected "tuple[Literal[True], int] | tuple[Literal[False], str]")

def incorrect_return2() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]:
if x:
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1873,3 +1873,29 @@ from typing_extensions import Concatenate
def c(t: tuple[Concatenate[int, ...]]) -> None: # E: Cannot use "[int, VarArg(Any), KwArg(Any)]" for tuple, only for ParamSpec
reveal_type(t) # N: Revealed type is "tuple[Any]"
[builtins fixtures/tuple.pyi]

[case testTupleUnionContextInSequence]
from enum import Enum
from typing import Literal, Sequence, Tuple, Union

class Color(Enum):
RED = 1
BLUE = 2

class Shape(Enum):
CIRCLE = 1
TRIANGLE = 2
SQUARE = 3

Pair = Union[
Tuple[Literal[Color.RED], Literal[Shape.CIRCLE]],
Tuple[Literal[Color.BLUE], Literal[Shape.TRIANGLE]],
]

def consume(pairs: Sequence[Pair]) -> None:
pass

consume([(Color.BLUE, Shape.TRIANGLE)])
consume([(Color.RED, Shape.CIRCLE), (Color.BLUE, Shape.TRIANGLE)])
consume([(Color.RED, Shape.SQUARE)]) # E: List item 0 has incompatible type "tuple[Literal[Color.RED], Literal[Shape.SQUARE]]"; expected "tuple[Literal[Color.RED], Literal[Shape.CIRCLE]] | tuple[Literal[Color.BLUE], Literal[Shape.TRIANGLE]]"
[builtins fixtures/tuple.pyi]
Loading