Skip to content

CLN: change getargspec -> signature #12325

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 27 additions & 2 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from unicodedata import east_asian_width
import struct
import inspect
from collections import namedtuple

PY2 = sys.version_info[0] == 2
PY3 = (sys.version_info[0] >= 3)
Expand Down Expand Up @@ -70,8 +71,32 @@ def str_to_bytes(s, encoding=None):
def bytes_to_str(b, encoding=None):
return b.decode(encoding or 'utf-8')

# The signature version below is directly copied from Django,
# https://github.com/django/django/pull/4846
def signature(f):
return list(inspect.signature(f).parameters.keys())
sig = inspect.signature(f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where did you get all this from? is it already defined somewhere we can just import it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've borrowed it from django/django#4846

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a refernce to that django issue here (and comment that we copied directly)

args = [
p.name for p in sig.parameters.values()
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
]
varargs = [
p.name for p in sig.parameters.values()
if p.kind == inspect.Parameter.VAR_POSITIONAL
]
varargs = varargs[0] if varargs else None
keywords = [
p.name for p in sig.parameters.values()
if p.kind == inspect.Parameter.VAR_KEYWORD
]
keywords = keywords[0] if keywords else None
defaults = [
p.default for p in sig.parameters.values()
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
and p.default is not p.empty
] or None
argspec = namedtuple('Signature',['args','defaults',
'varargs','keywords'])
return argspec(args,defaults,varargs,keywords)

# have to explicitly put builtins into the namespace
range = range
Expand Down Expand Up @@ -110,7 +135,7 @@ def bytes_to_str(b, encoding='ascii'):
return b

def signature(f):
return inspect.getargspec(f).args
return inspect.getargspec(f)

# import iterator versions of these functions
range = xrange
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding=utf-8
# pylint: disable-msg=E1101,W0612

from inspect import getargspec
from itertools import product
from distutils.version import LooseVersion

Expand Down Expand Up @@ -465,7 +464,7 @@ def testit():
self.assertRaises(ValueError, f, self.series, axis=1)

# Unimplemented numeric_only parameter.
if 'numeric_only' in getargspec(f).args:
if 'numeric_only' in compat.signature(f).args:
self.assertRaisesRegexp(NotImplementedError, name, f,
self.series, numeric_only=True)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def wrapper(x):
self.assertRaises(Exception, f, axis=obj.ndim)

# Unimplemented numeric_only parameter.
if 'numeric_only' in signature(f):
if 'numeric_only' in signature(f).args:
self.assertRaisesRegexp(NotImplementedError, name, f,
numeric_only=True)

Expand Down
5 changes: 2 additions & 3 deletions pandas/util/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pandas.compat import StringIO, callable
from pandas.compat import StringIO, callable, signature
from pandas.lib import cache_readonly # noqa
import sys
import warnings
Expand Down Expand Up @@ -275,8 +275,7 @@ def make_signature(func):
>>> print(_make_signature(f))
a,b,c=2
"""
from inspect import getargspec
spec = getargspec(func)
spec = signature(func)
if spec.defaults is None:
n_wo_defaults = len(spec.args)
defaults = ('',) * n_wo_defaults
Expand Down