Skip to content

Commit 46da423

Browse files
fixed match-case against typing.Callable
1 parent ca738e5 commit 46da423

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mypy/checkpattern.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
UninhabitedType,
5151
UnionType,
5252
UnpackType,
53+
callable_with_ellipsis,
5354
find_unpack_in_list,
5455
get_proper_type,
5556
split_with_prefix_and_suffix,
@@ -553,6 +554,15 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
553554
and isinstance(get_proper_type(type_info.type), AnyType)
554555
):
555556
typ = type_info.type
557+
elif (
558+
isinstance(type_info, Var)
559+
and type_info.type is not None
560+
and type_info.fullname == "typing.Callable"
561+
):
562+
# Consider as `Callable[..., Any]`
563+
fallback = self.chk.named_type("builtins.function")
564+
any_type = AnyType(TypeOfAny.unannotated)
565+
typ = callable_with_ellipsis(any_type, any_type, fallback)
556566
else:
557567
if isinstance(type_info, Var) and type_info.type is not None:
558568
name = type_info.type.str_with_options(self.options)

test-data/unit/check-python310.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,15 @@ match m:
10691069
case Foo():
10701070
pass
10711071

1072+
[case testMatchClassPatternCallable]
1073+
from typing import Callable
1074+
1075+
x: object
1076+
1077+
match x:
1078+
case Callable() as fn:
1079+
reveal_type(fn) # N: Revealed type is "def (*Any, **Any) -> Any"
1080+
10721081
[case testMatchClassPatternNestedGenerics]
10731082
# From cpython test_patma.py
10741083
x = [[{0: 0}]]

0 commit comments

Comments
 (0)