Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ValuePattern non-exhaustive #12751

Merged
merged 1 commit into from
May 9, 2022
Merged
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
4 changes: 3 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from mypy.typeops import try_getting_str_literals_from_type, make_simplified_union, \
coerce_to_literal
from mypy.types import (
ProperType, AnyType, TypeOfAny, Instance, Type, UninhabitedType, get_proper_type,
LiteralType, ProperType, AnyType, TypeOfAny, Instance, Type, UninhabitedType, get_proper_type,
TypedDictType, TupleType, NoneType, UnionType
)
from mypy.typevars import fill_typevars
Expand Down Expand Up @@ -183,6 +183,8 @@ def visit_value_pattern(self, o: ValuePattern) -> PatternType:
o,
default=current_type
)
if not isinstance(get_proper_type(narrowed_type), (LiteralType, UninhabitedType)):
return PatternType(narrowed_type, UnionType.make_union([narrowed_type, rest_type]), {})
return PatternType(narrowed_type, rest_type, {})

def visit_singleton_pattern(self, o: SingletonPattern) -> PatternType:
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1576,3 +1576,18 @@ def f(x: AST) -> None:
reveal_type(b) # N: Revealed type is "builtins.int"
reveal_type(c) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]

[case testMatchReachableDottedNames]
# flags: --warn-unreachable
class Consts:
BLANK = ""
SPECIAL = "asdf"

def test_func(test_str: str) -> str:
match test_str:
case Consts.BLANK:
return "blank"
case Consts.SPECIAL:
return "special"
case _:
return "other"