Skip to content

Commit

Permalink
Don't consider comparing True and False as a dangerous comparison (#9021
Browse files Browse the repository at this point in the history
)

Fixes #9011.
  • Loading branch information
JukkaL authored and Ivan Levkivskyi committed Jun 22, 2020
1 parent f956142 commit e90d5f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,10 @@ def dangerous_comparison(self, left: Type, right: Type,
left = map_instance_to_supertype(left, abstract_set)
right = map_instance_to_supertype(right, abstract_set)
return not is_overlapping_types(left.args[0], right.args[0])
if isinstance(left, LiteralType) and isinstance(right, LiteralType):
if isinstance(left.value, bool) and isinstance(right.value, bool):
# Comparing different booleans is not dangerous.
return False
return not is_overlapping_types(left, right, ignore_promotions=False)

def get_operator_method(self, op: str) -> str:
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -3228,3 +3228,18 @@ y: Literal[F.A]
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
[builtins fixtures/tuple.pyi]

[case testStrictEqualityLiteralTrueVsFalse]
# mypy: strict-equality

class C:
a = True

def update(self) -> None:
self.a = False

c = C()
assert c.a is True
c.update()
assert c.a is False
[builtins fixtures/bool.pyi]

0 comments on commit e90d5f5

Please sign in to comment.