Skip to content

Unreachable expressions: treat non-zero ints as truthy #10562

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
Jun 1, 2021
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 @@ -5096,7 +5096,7 @@ def gen_unique_name(base: str, table: SymbolTable) -> str:
def is_true_literal(n: Expression) -> bool:
"""Returns true if this expression is the 'True' literal/keyword."""
return (refers_to_fullname(n, 'builtins.True')
or isinstance(n, IntExpr) and n.value == 1)
or isinstance(n, IntExpr) and n.value != 0)


def is_false_literal(n: Expression) -> bool:
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ def foo() -> bool: ...
lst = [1, 2, 3, 4]

a = True or foo() # E: Right operand of "or" is never evaluated
b = 42 or False # E: Right operand of "or" is never evaluated
d = False and foo() # E: Right operand of "and" is never evaluated
e = True or (True or (True or foo())) # E: Right operand of "or" is never evaluated
f = (True or foo()) or (True or foo()) # E: Right operand of "or" is never evaluated
Expand Down