Skip to content

Commit ef53130

Browse files
committed
Allow NoneType in match cases
1 parent f2db975 commit ef53130

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mypy/checkpattern.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
Type,
4747
TypedDictType,
4848
TypeOfAny,
49+
TypeType,
4950
TypeVarTupleType,
5051
TypeVarType,
5152
UninhabitedType,
@@ -556,6 +557,8 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
556557
fallback = self.chk.named_type("builtins.function")
557558
any_type = AnyType(TypeOfAny.unannotated)
558559
typ = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback)
560+
elif isinstance(p_typ, TypeType) and isinstance(p_typ.item, NoneType):
561+
typ = p_typ.item
559562
elif not isinstance(p_typ, AnyType):
560563
self.msg.fail(
561564
message_registry.CLASS_PATTERN_TYPE_REQUIRED.format(

test-data/unit/check-python310.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,3 +3178,19 @@ match 5:
31783178
reveal_type(b) # N: Revealed type is "Any"
31793179
case BlahBlah(c=c): # E: Name "BlahBlah" is not defined
31803180
reveal_type(c) # N: Revealed type is "Any"
3181+
3182+
[case testMatchAllowsNoneTypeAsClass]
3183+
import types
3184+
3185+
class V:
3186+
X = types.NoneType
3187+
3188+
def fun(val: str | None):
3189+
match val:
3190+
case V.X():
3191+
reveal_type(val) # N: Revealed type is "None"
3192+
3193+
match val:
3194+
case types.NoneType():
3195+
reveal_type(val) # N: Revealed type is "None"
3196+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)