diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85..ce2142daf320 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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): diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index b54db1f33fa5..f6e2390b93da 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -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: diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 6d523d375dd2..3dda80752668 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -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]