Skip to content
Closed
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
23 changes: 23 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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
Loading