diff --git a/mypy/typeops.py b/mypy/typeops.py index 3035a15e2119..d18a04882b28 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -1145,6 +1145,8 @@ class Status(Enum): target_fullname is None or typ.type.fullname == target_fullname ): if typ.type.fullname == "builtins.bool": + if typ.last_known_value is not None: + return typ.last_known_value return UnionType([LiteralType(True, typ), LiteralType(False, typ)]) if typ.type.is_enum: diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 33b952f48f49..a896f87e1cd6 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -1979,6 +1979,32 @@ def b(value: bool) -> int: case False: return 3 +[case testMatchBoolLiteralSubjectExhaustive] +# flags: --strict-equality --warn-unreachable +def f() -> int: + match False: + case True: + return 1 # E: Statement is unreachable + case False: + return 0 + +def g(c: bool) -> int: + match True if c else False: + case True: + return 1 + case False: + return 0 + +def h() -> int: + match False: + case True: + return 1 # E: Statement is unreachable + case False: + return 0 + case other: + reveal_type(other) # E: Statement is unreachable + return 2 + [case testMatchMiscNonExhaustiveReturn] # flags: --strict-equality --warn-unreachable class C: