Skip to content

Adds get_function_signature_hook #9102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 18, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
generalized it
  • Loading branch information
msullivan committed Oct 18, 2020
commit 909e115bc628246a3cc1076cdff4bd8bebf008db
56 changes: 26 additions & 30 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,15 @@ def apply_function_plugin(self,
callee.arg_names, formal_arg_names,
callee.ret_type, formal_arg_exprs, context, self.chk))

def apply_function_signature_hook(
def apply_signature_hook(
self, callee: FunctionLike, args: List[Expression],
arg_kinds: List[int], context: Context,
arg_kinds: List[int],
arg_names: Optional[Sequence[Optional[str]]],
signature_hook: Callable[[FunctionSigContext], CallableType]) -> FunctionLike:
"""Apply a plugin hook that may infer a more precise signature for a function."""
hook: Callable[
[List[List[Expression]], CallableType],
CallableType,
]) -> FunctionLike:
"""Helper to apply a signature hook for either a function or method"""
if isinstance(callee, CallableType):
num_formals = len(callee.arg_kinds)
formal_to_actual = map_actuals_to_formals(
Expand All @@ -750,46 +753,39 @@ def apply_function_signature_hook(
for formal, actuals in enumerate(formal_to_actual):
for actual in actuals:
formal_arg_exprs[formal].append(args[actual])
return signature_hook(
FunctionSigContext(formal_arg_exprs, callee, context, self.chk))
return hook(formal_arg_exprs, callee)
else:
assert isinstance(callee, Overloaded)
items = []
for item in callee.items():
adjusted = self.apply_function_signature_hook(
item, args, arg_kinds, context, arg_names, signature_hook)
adjusted = self.apply_signature_hook(
item, args, arg_kinds, arg_names, hook)
assert isinstance(adjusted, CallableType)
items.append(adjusted)
return Overloaded(items)

def apply_function_signature_hook(
self, callee: FunctionLike, args: List[Expression],
arg_kinds: List[int], context: Context,
arg_names: Optional[Sequence[Optional[str]]],
signature_hook: Callable[[FunctionSigContext], CallableType]) -> FunctionLike:
"""Apply a plugin hook that may infer a more precise signature for a function."""
return self.apply_signature_hook(
callee, args, arg_kinds, arg_names,
(lambda args, sig:
signature_hook(FunctionSigContext(args, sig, context, self.chk))))

def apply_method_signature_hook(
self, callee: FunctionLike, args: List[Expression],
arg_kinds: List[int], context: Context,
arg_names: Optional[Sequence[Optional[str]]], object_type: Type,
signature_hook: Callable[[MethodSigContext], CallableType]) -> FunctionLike:
"""Apply a plugin hook that may infer a more precise signature for a method."""
if isinstance(callee, CallableType):
num_formals = len(callee.arg_kinds)
formal_to_actual = map_actuals_to_formals(
arg_kinds, arg_names,
callee.arg_kinds, callee.arg_names,
lambda i: self.accept(args[i]))
formal_arg_exprs = [[] for _ in range(num_formals)] # type: List[List[Expression]]
for formal, actuals in enumerate(formal_to_actual):
for actual in actuals:
formal_arg_exprs[formal].append(args[actual])
object_type = get_proper_type(object_type)
return signature_hook(
MethodSigContext(object_type, formal_arg_exprs, callee, context, self.chk))
else:
assert isinstance(callee, Overloaded)
items = []
for item in callee.items():
adjusted = self.apply_method_signature_hook(
item, args, arg_kinds, context, arg_names, object_type, signature_hook)
assert isinstance(adjusted, CallableType)
items.append(adjusted)
return Overloaded(items)
pobject_type = get_proper_type(object_type)
return self.apply_signature_hook(
callee, args, arg_kinds, arg_names,
(lambda args, sig:
signature_hook(MethodSigContext(pobject_type, args, sig, context, self.chk))))

def transform_callee_type(
self, callable_name: Optional[str], callee: Type, args: List[Expression],
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy