Skip to content

Commit e90d5f5

Browse files
JukkaLIvan Levkivskyi
authored andcommitted
Don't consider comparing True and False as a dangerous comparison (#9021)
Fixes #9011.
1 parent f956142 commit e90d5f5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,10 @@ def dangerous_comparison(self, left: Type, right: Type,
23062306
left = map_instance_to_supertype(left, abstract_set)
23072307
right = map_instance_to_supertype(right, abstract_set)
23082308
return not is_overlapping_types(left.args[0], right.args[0])
2309+
if isinstance(left, LiteralType) and isinstance(right, LiteralType):
2310+
if isinstance(left.value, bool) and isinstance(right.value, bool):
2311+
# Comparing different booleans is not dangerous.
2312+
return False
23092313
return not is_overlapping_types(left, right, ignore_promotions=False)
23102314

23112315
def get_operator_method(self, op: str) -> str:

test-data/unit/check-literal.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,3 +3228,18 @@ y: Literal[F.A]
32283228
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
32293229
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
32303230
[builtins fixtures/tuple.pyi]
3231+
3232+
[case testStrictEqualityLiteralTrueVsFalse]
3233+
# mypy: strict-equality
3234+
3235+
class C:
3236+
a = True
3237+
3238+
def update(self) -> None:
3239+
self.a = False
3240+
3241+
c = C()
3242+
assert c.a is True
3243+
c.update()
3244+
assert c.a is False
3245+
[builtins fixtures/bool.pyi]

0 commit comments

Comments
 (0)