Skip to content
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
Allow NoneType in match cases
  • Loading branch information
A5rocks committed Dec 9, 2025
commit ef53130b0fcc786804a9a7e3d6211a98828b62e6
3 changes: 3 additions & 0 deletions mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
Type,
TypedDictType,
TypeOfAny,
TypeType,
TypeVarTupleType,
TypeVarType,
UninhabitedType,
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]