Skip to content
Merged
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
2 changes: 2 additions & 0 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ def visit_literal_type(self, t: LiteralType) -> ProperType:
if self.s.fallback.type.is_enum and t.fallback.type.is_enum:
return mypy.typeops.make_simplified_union([self.s, t])
return join_types(self.s.fallback, t.fallback)
elif isinstance(self.s, Instance) and self.s.last_known_value == t:
return t
else:
return join_types(self.s, t.fallback)

Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2976,3 +2976,22 @@ x: Type[Literal[1]] # E: Type[...] can't contain "Literal[...]"
y: Type[Union[Literal[1], Literal[2]]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]"
z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]"
[builtins fixtures/tuple.pyi]

[case testJoinLiteralAndInstance]
from typing import Generic, TypeVar, Literal

T = TypeVar("T")

class A(Generic[T]): ...

def f(a: A[T], t: T) -> T: ...
def g(a: T, t: A[T]) -> T: ...

def check(obj: A[Literal[1]]) -> None:
reveal_type(f(obj, 1)) # N: Revealed type is "Literal[1]"
reveal_type(f(obj, '')) # E: Cannot infer type argument 1 of "f" \
# N: Revealed type is "Any"
reveal_type(g(1, obj)) # N: Revealed type is "Literal[1]"
reveal_type(g('', obj)) # E: Cannot infer type argument 1 of "g" \
# N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]