-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
comparison-overlap
error not triggered for booleans
#11364
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
Comments
I think that this is by design. Bools are often used for things like debug flags that can typically be false, but it's still okay to write
|
from typing import Literal
foo: Literal["foo"]
# error: Non-overlapping equality check (left operand type: "Literal['foo']", right operand type: "Literal['bar']") [comparison-overlap]
foo == "bar"
foo != "bar"
# no error
foo != "foo"
foo == "foo"
bar: Literal[True]
# no error
bar == True
bar == False
bar != True
bar != False
if bar: print(1)
# error: Statement is unreachable [unreachable]
if not bar: print(1) (Gist)
@JukkaL Do you mean debug flags that are used with This feature(fix?) would heavily clash with # mypy: always-false=FOO
from typing import TYPE_CHECKING
Foo = True
if TYPE_CHECKING: # error: condition always true
...
if FOO: # error: condition always false
... If this truly is intended then it is quite non-obvious, and not documented. I propose that the current rules regarding ignoring I think that as it stands it's an inconsistent feature, as debug options can also be integers or strings, and the benefits of catching true |
Researching this further, I have discovered that mypy already special cases artificially literal bools: # mypy: always-true=foo
from typing import Literal
foo: bool
if foo: print(1) # no error
bar: Literal[True]
if not bar: print(1) # error: Statement is unreachable |
https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-context%2Cshow-column-numbers%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=4d291b48d0f5858f4b1ec54a1a67e5f6
The text was updated successfully, but these errors were encountered: