Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite69af83

Browse files
authored
Allowtypes.NoneType in match cases (#20383)
Fixes#20367
1 parent488f3c7 commite69af83

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+
elifisinstance(p_typ,TypeType)andisinstance(p_typ.item,NoneType):
561+
typ=p_typ.item
559562
elifnotisinstance(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)

[8]ページ先頭

©2009-2025 Movatter.jp