Skip to content

Commit

Permalink
Fix literal context for ternary expressions (for real) (#18545)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi authored and wesleywright committed Jan 27, 2025
1 parent 23d862d commit c30573e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4372,7 +4372,7 @@ def check_simple_assignment(
if (
isinstance(get_proper_type(lvalue_type), UnionType)
# Skip literal types, as they have special logic (for better errors).
and not isinstance(get_proper_type(rvalue_type), LiteralType)
and not is_literal_type_like(rvalue_type)
and not self.simple_rvalue(rvalue)
):
# Try re-inferring r.h.s. in empty context, and use that if it
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2984,7 +2984,7 @@ class C(Base):
sep = "a" if int() else "b"
reveal_type(sep) # N: Revealed type is "Union[Literal['a'], Literal['b']]"
return super().feed_data(sep)
[builtins fixtures/tuple.pyi]
[builtins fixtures/primitives.pyi]

[case testLiteralInsideAType]
from typing_extensions import Literal
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/fixtures/primitives.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class int:
def __init__(self, x: object = ..., base: int = ...) -> None: pass
def __add__(self, i: int) -> int: pass
def __rmul__(self, x: int) -> int: pass
def __bool__(self) -> bool: pass
class float:
def __float__(self) -> float: pass
def __add__(self, x: float) -> float: pass
Expand Down

0 comments on commit c30573e

Please sign in to comment.