Skip to content

Commit 16e19f8

Browse files
authored
Fix literal context for ternary expressions (for real) (#18545)
I am not waiting for review as the fix is obvious. The only annoying thing is that we had an exact test as in the repro but it passed accidentally because we use builtins fixtures.
1 parent 7e8213f commit 16e19f8

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

mypy/checker.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4469,7 +4469,7 @@ def check_simple_assignment(
44694469
if (
44704470
isinstance(get_proper_type(lvalue_type), UnionType)
44714471
# Skip literal types, as they have special logic (for better errors).
4472-
and not isinstance(get_proper_type(rvalue_type), LiteralType)
4472+
and not is_literal_type_like(rvalue_type)
44734473
and not self.simple_rvalue(rvalue)
44744474
):
44754475
# Try re-inferring r.h.s. in empty context, and use that if it

test-data/unit/check-literal.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,7 @@ class C(Base):
29802980
sep = "a" if int() else "b"
29812981
reveal_type(sep) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
29822982
return super().feed_data(sep)
2983-
[builtins fixtures/tuple.pyi]
2983+
[builtins fixtures/primitives.pyi]
29842984

29852985
[case testLiteralInsideAType]
29862986
from typing_extensions import Literal

test-data/unit/fixtures/primitives.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class int:
1919
def __init__(self, x: object = ..., base: int = ...) -> None: pass
2020
def __add__(self, i: int) -> int: pass
2121
def __rmul__(self, x: int) -> int: pass
22+
def __bool__(self) -> bool: pass
2223
class float:
2324
def __float__(self) -> float: pass
2425
def __add__(self, x: float) -> float: pass

0 commit comments

Comments
 (0)