diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index 3c51c4106909..cafc69490e09 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -46,6 +46,7 @@ Type, TypedDictType, TypeOfAny, + TypeType, TypeVarTupleType, TypeVarType, UninhabitedType, @@ -556,6 +557,8 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType: fallback = self.chk.named_type("builtins.function") any_type = AnyType(TypeOfAny.unannotated) typ = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback) + elif isinstance(p_typ, TypeType) and isinstance(p_typ.item, NoneType): + typ = p_typ.item elif not isinstance(p_typ, AnyType): self.msg.fail( message_registry.CLASS_PATTERN_TYPE_REQUIRED.format( diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 1e27e30d4b04..8bc781d091c3 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -3178,3 +3178,19 @@ match 5: reveal_type(b) # N: Revealed type is "Any" case BlahBlah(c=c): # E: Name "BlahBlah" is not defined reveal_type(c) # N: Revealed type is "Any" + +[case testMatchAllowsNoneTypeAsClass] +import types + +class V: + X = types.NoneType + +def fun(val: str | None): + match val: + case V.X(): + reveal_type(val) # N: Revealed type is "None" + + match val: + case types.NoneType(): + reveal_type(val) # N: Revealed type is "None" +[builtins fixtures/tuple.pyi]