Skip to content

Fix literal context for ternary expressions (for real) #18545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
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: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4469,7 +4469,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 @@ -2980,7 +2980,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
Loading