From dc2c392138ebe85fa106b2896f4532b75809ab30 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 18 Jun 2020 17:02:42 +0100 Subject: [PATCH] Don't consider comparing True and False as a dangerous comparison (#9021) Fixes #9011. --- mypy/checkexpr.py | 4 ++++ test-data/unit/check-literal.test | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 450993a90c4d..5af114767357 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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: diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index 781f7a6378dc..3ff8b17f90b7 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -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]