From edb82bfb7654b882802446e20eac90a44e7bd991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Hor=C3=A1=C4=8Dek?= Date: Sat, 7 May 2022 22:21:33 +0200 Subject: [PATCH] Make ValuePattern non-exhaustive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes behaviour of PatternChecker so that ValuePattern does not cover the entire type since it can catch only a single value. Signed-off-by: Štěpán Horáček --- mypy/checkpattern.py | 4 +++- test-data/unit/check-python310.test | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index 6a8a0196306c..978b03b342f5 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -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 @@ -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: diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index cb39a519a146..818981238b3b 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -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"