Skip to content
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

Fix for type narrowing of negative integer literals #17256

Merged
merged 1 commit into from
May 17, 2024
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/plugins/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def int_neg_callback(ctx: MethodContext, multiplier: int = -1) -> Type:
return ctx.type.copy_modified(
last_known_value=LiteralType(
value=multiplier * value,
fallback=ctx.type,
fallback=fallback,
line=ctx.type.line,
column=ctx.type.column,
)
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -2089,3 +2089,28 @@ if isinstance(x, (Z, NoneType)): # E: Subclass of "X" and "Z" cannot exist: "Z"
reveal_type(x) # E: Statement is unreachable

[builtins fixtures/isinstance.pyi]

[case testTypeNarrowingReachableNegative]
# flags: --warn-unreachable
from typing import Literal

x: Literal[-1]

if x == -1:
assert True

[typing fixtures/typing-medium.pyi]
[builtins fixtures/ops.pyi]

[case testTypeNarrowingReachableNegativeUnion]
from typing import Literal

x: Literal[-1, 1]

if x == -1:
reveal_type(x) # N: Revealed type is "Literal[-1]"
else:
reveal_type(x) # N: Revealed type is "Literal[1]"

[typing fixtures/typing-medium.pyi]
[builtins fixtures/ops.pyi]
Loading