From 32201c67a545473fe42f29753f94bcb19c02d2f4 Mon Sep 17 00:00:00 2001 From: DrSkyle <86089508+DrSkyle@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:41:19 +0000 Subject: [PATCH 1/2] fix(checkexpr): infer per-element union context for tuple expressions --- mypy/checkexpr.py | 17 ++++++++++++++++- test-data/unit/check-literal.test | 4 ++-- test-data/unit/check-tuples.test | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85e..ce2142daf3207 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 b54db1f33fa5e..f6e2390b93da2 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 6d523d375dd2a..2a30596c135ef 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -1873,3 +1873,30 @@ 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] + From c1cc80ccc78bd5c564e32c6f1302a5f9ff584136 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:58:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test-data/unit/check-tuples.test | 1 - 1 file changed, 1 deletion(-) diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 2a30596c135ef..3dda80752668e 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -1899,4 +1899,3 @@ 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] -