-
-
Notifications
You must be signed in to change notification settings - Fork 11k
MAINT: Remove internal uses of _inspect module #16787
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
Changes from all commits
b3bc0fb
82f94cd
0f84e10
31e8068
c1eeb82
6e35c2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,10 @@ | |
import functools | ||
import os | ||
import textwrap | ||
import inspect | ||
|
||
from numpy.core._multiarray_umath import ( | ||
add_docstring, implement_array_function, _get_implementing_args) | ||
from numpy.compat._inspect import getargspec | ||
|
||
|
||
ARRAY_FUNCTION_ENABLED = bool( | ||
|
@@ -66,29 +66,36 @@ | |
""") | ||
|
||
|
||
ArgSpec = collections.namedtuple('ArgSpec', 'args varargs keywords defaults') | ||
# Avoid extra getattr calls in verify_matching_signatures | ||
_Parameter_empty = inspect.Parameter.empty | ||
|
||
|
||
def verify_matching_signatures(implementation, dispatcher): | ||
"""Verify that a dispatcher function has the right signature.""" | ||
implementation_spec = ArgSpec(*getargspec(implementation)) | ||
dispatcher_spec = ArgSpec(*getargspec(dispatcher)) | ||
|
||
if (implementation_spec.args != dispatcher_spec.args or | ||
implementation_spec.varargs != dispatcher_spec.varargs or | ||
implementation_spec.keywords != dispatcher_spec.keywords or | ||
(bool(implementation_spec.defaults) != | ||
bool(dispatcher_spec.defaults)) or | ||
(implementation_spec.defaults is not None and | ||
len(implementation_spec.defaults) != | ||
len(dispatcher_spec.defaults))): | ||
raise RuntimeError('implementation and dispatcher for %s have ' | ||
'different function signatures' % implementation) | ||
|
||
if implementation_spec.defaults is not None: | ||
if dispatcher_spec.defaults != (None,) * len(dispatcher_spec.defaults): | ||
raise RuntimeError('dispatcher functions can only use None for ' | ||
'default argument values') | ||
implementation_sig = inspect.signature(implementation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can only assume the timings are bad because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, some (very rough) profiling indicates that this is the case. Using the following setup: from numpy.core.overrides import verify_matching_signatures
def foo(
a, b, m, n, k, copy=False, color="red", normed=False, weights=None,
bins=10, density=None, size=1024, shape=(3, 4)
):
pass
def _foo_dispatcher(
a, b, m, n, k, copy=None, color=None, normed=None, weights=None,
bins=None, density=None, size=None, shape=None
):
pass
On 6e35c2a (this PR):
On 2283e26 (root of branch):
So the implementation based on One idea I had was to try to re-implement this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is implemented in terms of Perhaps the comparison is the slow part. |
||
dispatcher_sig = inspect.signature(dispatcher) | ||
|
||
implementation_sig_with_none_defaults = inspect.Signature( | ||
parameters=[ | ||
rossbar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
p.replace( | ||
annotation=_Parameter_empty, | ||
default=( | ||
_Parameter_empty | ||
if p.default is _Parameter_empty | ||
else None | ||
), | ||
) | ||
for p in implementation_sig.parameters.values() | ||
], | ||
) | ||
|
||
if dispatcher_sig != implementation_sig_with_none_defaults: | ||
raise RuntimeError( | ||
f"dispatcher for {implementation} has the wrong function " | ||
"signature:\n" | ||
" expected: {implementation_sig_with_none_defaults}\n" | ||
" got: {dispatcher_sig}" | ||
) | ||
|
||
|
||
def set_module(module): | ||
|
Uh oh!
There was an error while loading. Please reload this page.