From b1d4acc9735fc69f820ce3166d0336517b6a5aea Mon Sep 17 00:00:00 2001 From: pei711 Date: Thu, 19 Nov 2026 01:54:34 +0000 Subject: [PATCH 1/2] Preserve enum literals in tuple union contexts --- mypy/checkexpr.py | 23 +++++++++++++++++++++++ test-data/unit/check-inference.test | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85..107572f78488 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -5512,8 +5512,31 @@ def visit_tuple_expr(self, e: TupleExpr) -> Type: if (isinstance(t, TupleType) and self.tuple_context_matches(e, t)) or is_named_instance(t, TUPLE_LIKE_INSTANCE_NAMES) ] + tuple_contexts = [t for t in tuples_in_context if isinstance(t, TupleType)] if len(tuples_in_context) == 1: type_context = tuples_in_context[0] + elif ( + len(tuple_contexts) == len(tuples_in_context) + and len(tuple_contexts) > 1 + and all( + len(t.items) == len(tuple_contexts[0].items) + and find_unpack_in_list(t.items) is None + for t in tuple_contexts + ) + ): + # Preserve literal types when a tuple expression can match multiple + # alternatives in a union. The final tuple is still checked against + # the original union, so this only supplies item-level context. + type_context = TupleType( + [ + UnionType.make_union([t.items[i] for t in tuple_contexts]) + for i in range(len(tuple_contexts[0].items)) + ], + tuple_contexts[0].partial_fallback, + e.line, + e.column, + implicit=True, + ) 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. diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 08a70d1a6efe..bd093d3438c9 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -4426,3 +4426,23 @@ def test() -> list[int]: d: dict[str, int] = {"hits": a, "misses": "42"} # E: Dict entry 1 has incompatible type "str": "str"; expected "str": "int" [builtins fixtures/dict.pyi] + +[case testEnumLiteralTuplesInSequenceContext] +from enum import Enum +from typing import Literal, Sequence + +class Color(Enum): + RED = 1 + BLUE = 2 + +class Shape(Enum): + CIRCLE = 1 + TRIANGLE = 2 + +Pair = tuple[Literal[Color.RED], Literal[Shape.CIRCLE]] | tuple[Literal[Color.BLUE], Literal[Shape.TRIANGLE]] + +def consume(pairs: Sequence[Pair]) -> None: ... + +consume([(Color.BLUE, Shape.TRIANGLE)]) +consume([(Color.RED, Shape.TRIANGLE)]) # E: List item 0 has incompatible type "tuple[Literal[Color.RED], Literal[Shape.TRIANGLE]]"; expected "tuple[Literal[Color.RED], Literal[Shape.CIRCLE]] | tuple[Literal[Color.BLUE], Literal[Shape.TRIANGLE]]" +[builtins fixtures/list.pyi] From e55038a6512e52c76f36c0aaa235958fa76a0e45 Mon Sep 17 00:00:00 2001 From: pei711 Date: Sat, 21 Nov 2026 11:44:05 +0000 Subject: [PATCH 2/2] Update literal tuple error expectations --- test-data/unit/check-literal.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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: