Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,12 @@ class Status(Enum):
if isinstance(typ, Instance) and (
target_fullname is None or typ.type.fullname == target_fullname
):
if typ.last_known_value is not None:
# The expression is already known to hold this specific value, so keep it: expanding
# to all members of the sum type would forget it and break narrowing, e.g. matching a
# ``False`` literal subject against ``case False`` would not be exhaustive.
return typ.last_known_value

if typ.type.fullname == "builtins.bool":
return UnionType([LiteralType(True, typ), LiteralType(False, typ)])

Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ def foo(m: bool, x: int):
reveal_type(m) # N: Revealed type is "builtins.bool"
[builtins fixtures/primitives.pyi]

[case testMatchBoolLiteralSubjectIsExhaustive]
# flags: --strict-equality --warn-unreachable

def f() -> int:
match False:
case True:
return 1 # E: Statement is unreachable
case False:
return 0

def g(b: bool) -> int:
match b:
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
[builtins fixtures/primitives.pyi]

[case testMatchLiteralPatternUnreachable]
# flags: --strict-equality --warn-unreachable
m: int
Expand Down
Loading