Skip to content

Commit 5d1857c

Browse files
troglotitjreback
authored andcommitted
CLN: change getargspec -> signature
Change signature in compat to be able to return defaults, varargs, kwargs Closes #12171 Author: alex argunov <[email protected]> Closes #12325 from troglotit/getargspec_deprecation and squashes the following commits: e9153d9 [alex argunov] Add reference to Django; import to the top 522df01 [alex argunov] CLN: change getargspec -> signature
1 parent aeb2c82 commit 5d1857c

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

pandas/compat/__init__.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from unicodedata import east_asian_width
3737
import struct
3838
import inspect
39+
from collections import namedtuple
3940

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

74+
# The signature version below is directly copied from Django,
75+
# https://github.com/django/django/pull/4846
7376
def signature(f):
74-
return list(inspect.signature(f).parameters.keys())
77+
sig = inspect.signature(f)
78+
args = [
79+
p.name for p in sig.parameters.values()
80+
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
81+
]
82+
varargs = [
83+
p.name for p in sig.parameters.values()
84+
if p.kind == inspect.Parameter.VAR_POSITIONAL
85+
]
86+
varargs = varargs[0] if varargs else None
87+
keywords = [
88+
p.name for p in sig.parameters.values()
89+
if p.kind == inspect.Parameter.VAR_KEYWORD
90+
]
91+
keywords = keywords[0] if keywords else None
92+
defaults = [
93+
p.default for p in sig.parameters.values()
94+
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
95+
and p.default is not p.empty
96+
] or None
97+
argspec = namedtuple('Signature',['args','defaults',
98+
'varargs','keywords'])
99+
return argspec(args,defaults,varargs,keywords)
75100

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

112137
def signature(f):
113-
return inspect.getargspec(f).args
138+
return inspect.getargspec(f)
114139

115140
# import iterator versions of these functions
116141
range = xrange

pandas/tests/series/test_analytics.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding=utf-8
22
# pylint: disable-msg=E1101,W0612
33

4-
from inspect import getargspec
54
from itertools import product
65
from distutils.version import LooseVersion
76

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

467466
# Unimplemented numeric_only parameter.
468-
if 'numeric_only' in getargspec(f).args:
467+
if 'numeric_only' in compat.signature(f).args:
469468
self.assertRaisesRegexp(NotImplementedError, name, f,
470469
self.series, numeric_only=True)
471470

pandas/tests/test_panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def wrapper(x):
201201
self.assertRaises(Exception, f, axis=obj.ndim)
202202

203203
# Unimplemented numeric_only parameter.
204-
if 'numeric_only' in signature(f):
204+
if 'numeric_only' in signature(f).args:
205205
self.assertRaisesRegexp(NotImplementedError, name, f,
206206
numeric_only=True)
207207

pandas/util/decorators.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pandas.compat import StringIO, callable
1+
from pandas.compat import StringIO, callable, signature
22
from pandas.lib import cache_readonly # noqa
33
import sys
44
import warnings
@@ -275,8 +275,7 @@ def make_signature(func):
275275
>>> print(_make_signature(f))
276276
a,b,c=2
277277
"""
278-
from inspect import getargspec
279-
spec = getargspec(func)
278+
spec = signature(func)
280279
if spec.defaults is None:
281280
n_wo_defaults = len(spec.args)
282281
defaults = ('',) * n_wo_defaults

0 commit comments

Comments
 (0)