Skip to content

Commit 5fbcbed

Browse files
more precise type narrowing
1 parent 46da423 commit 5fbcbed

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

mypy/checkpattern.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,12 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
559559
and type_info.type is not None
560560
and type_info.fullname == "typing.Callable"
561561
):
562-
# Consider as `Callable[..., Any]`
562+
# Create a `Callable[..., Any]`
563563
fallback = self.chk.named_type("builtins.function")
564564
any_type = AnyType(TypeOfAny.unannotated)
565-
typ = callable_with_ellipsis(any_type, any_type, fallback)
565+
fn_type = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback)
566+
# if typ is itself callable, use its own type, otherwise Callable[..,, Any]
567+
typ = current_type if is_subtype(current_type, fn_type) else fn_type
566568
else:
567569
if isinstance(type_info, Var) and type_info.type is not None:
568570
name = type_info.type.str_with_options(self.options)

test-data/unit/check-python310.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,13 @@ match x:
10781078
case Callable() as fn:
10791079
reveal_type(fn) # N: Revealed type is "def (*Any, **Any) -> Any"
10801080

1081+
def int_fun(x: int) -> int: return x+1
1082+
1083+
match int_fun:
1084+
case Callable() as fn:
1085+
reveal_type(fn) # N: Revealed type is "def (x: builtins.int) -> builtins.int"
1086+
1087+
10811088
[case testMatchClassPatternNestedGenerics]
10821089
# From cpython test_patma.py
10831090
x = [[{0: 0}]]

0 commit comments

Comments
 (0)