Skip to content

Commit c30573e

Browse files
ilevkivskyiwesleywright
authored andcommitted
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 23d862d commit c30573e

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
@@ -4372,7 +4372,7 @@ def check_simple_assignment(
43724372
if (
43734373
isinstance(get_proper_type(lvalue_type), UnionType)
43744374
# Skip literal types, as they have special logic (for better errors).
4375-
and not isinstance(get_proper_type(rvalue_type), LiteralType)
4375+
and not is_literal_type_like(rvalue_type)
43764376
and not self.simple_rvalue(rvalue)
43774377
):
43784378
# 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
@@ -2984,7 +2984,7 @@ class C(Base):
29842984
sep = "a" if int() else "b"
29852985
reveal_type(sep) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
29862986
return super().feed_data(sep)
2987-
[builtins fixtures/tuple.pyi]
2987+
[builtins fixtures/primitives.pyi]
29882988

29892989
[case testLiteralInsideAType]
29902990
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)