Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87a3503

Browse files
authored
Allow plugin signature hooks to return FunctionLike (#10717)
Change the type of the get_method_signature_hook and get_function_signature_hook plugin hooks to allow them to return any FunctionLike type, instead of just CallableType. This allows plugins to return Overloaded for the type of a function.
1 parent 8054d9c commit 87a3503

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

mypy/checkexpr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ def apply_signature_hook(
747747
arg_names: Optional[Sequence[Optional[str]]],
748748
hook: Callable[
749749
[List[List[Expression]], CallableType],
750-
CallableType,
750+
FunctionLike,
751751
]) -> FunctionLike:
752752
"""Helper to apply a signature hook for either a function or method"""
753753
if isinstance(callee, CallableType):
@@ -775,7 +775,7 @@ def apply_function_signature_hook(
775775
self, callee: FunctionLike, args: List[Expression],
776776
arg_kinds: List[int], context: Context,
777777
arg_names: Optional[Sequence[Optional[str]]],
778-
signature_hook: Callable[[FunctionSigContext], CallableType]) -> FunctionLike:
778+
signature_hook: Callable[[FunctionSigContext], FunctionLike]) -> FunctionLike:
779779
"""Apply a plugin hook that may infer a more precise signature for a function."""
780780
return self.apply_signature_hook(
781781
callee, args, arg_kinds, arg_names,
@@ -786,7 +786,7 @@ def apply_method_signature_hook(
786786
self, callee: FunctionLike, args: List[Expression],
787787
arg_kinds: List[int], context: Context,
788788
arg_names: Optional[Sequence[Optional[str]]], object_type: Type,
789-
signature_hook: Callable[[MethodSigContext], CallableType]) -> FunctionLike:
789+
signature_hook: Callable[[MethodSigContext], FunctionLike]) -> FunctionLike:
790790
"""Apply a plugin hook that may infer a more precise signature for a method."""
791791
pobject_type = get_proper_type(object_type)
792792
return self.apply_signature_hook(

mypy/plugin.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ class C: pass
127127
Expression, Context, ClassDef, SymbolTableNode, MypyFile, CallExpr
128128
)
129129
from mypy.tvar_scope import TypeVarLikeScope
130-
from mypy.types import Type, Instance, CallableType, TypeList, UnboundType, ProperType
130+
from mypy.types import (
131+
Type, Instance, CallableType, TypeList, UnboundType, ProperType, FunctionLike
132+
)
131133
from mypy.messages import MessageBuilder
132134
from mypy.options import Options
133135
from mypy.lookup import lookup_fully_qualified
@@ -544,7 +546,7 @@ def func(x: Other[int]) -> None:
544546
return None
545547

546548
def get_function_signature_hook(self, fullname: str
547-
) -> Optional[Callable[[FunctionSigContext], CallableType]]:
549+
) -> Optional[Callable[[FunctionSigContext], FunctionLike]]:
548550
"""Adjust the signature of a function.
549551
550552
This method is called before type checking a function call. Plugin
@@ -577,7 +579,7 @@ def get_function_hook(self, fullname: str
577579
return None
578580

579581
def get_method_signature_hook(self, fullname: str
580-
) -> Optional[Callable[[MethodSigContext], CallableType]]:
582+
) -> Optional[Callable[[MethodSigContext], FunctionLike]]:
581583
"""Adjust the signature of a method.
582584
583585
This method is called before type checking a method call. Plugin
@@ -748,15 +750,15 @@ def get_type_analyze_hook(self, fullname: str
748750
return self._find_hook(lambda plugin: plugin.get_type_analyze_hook(fullname))
749751

750752
def get_function_signature_hook(self, fullname: str
751-
) -> Optional[Callable[[FunctionSigContext], CallableType]]:
753+
) -> Optional[Callable[[FunctionSigContext], FunctionLike]]:
752754
return self._find_hook(lambda plugin: plugin.get_function_signature_hook(fullname))
753755

754756
def get_function_hook(self, fullname: str
755757
) -> Optional[Callable[[FunctionContext], Type]]:
756758
return self._find_hook(lambda plugin: plugin.get_function_hook(fullname))
757759

758760
def get_method_signature_hook(self, fullname: str
759-
) -> Optional[Callable[[MethodSigContext], CallableType]]:
761+
) -> Optional[Callable[[MethodSigContext], FunctionLike]]:
760762
return self._find_hook(lambda plugin: plugin.get_method_signature_hook(fullname))
761763

762764
def get_method_hook(self, fullname: str

0 commit comments

Comments
 (0)