Skip to content

Commit d2f9228

Browse files
mroeschkejreback
authored andcommitted
CLN: PY3 compat signature (#26016)
1 parent 6c613c8 commit d2f9228

File tree

5 files changed

+11
-43
lines changed

5 files changed

+11
-43
lines changed

pandas/compat/__init__.py

-31
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import platform
2525
import types
2626
import struct
27-
import inspect
28-
from collections import namedtuple
2927

3028
PY2 = sys.version_info[0] == 2
3129
PY3 = sys.version_info[0] >= 3
@@ -64,32 +62,6 @@ def str_to_bytes(s, encoding=None):
6462
def bytes_to_str(b, encoding=None):
6563
return b.decode(encoding or 'utf-8')
6664

67-
# The signature version below is directly copied from Django,
68-
# https://github.com/django/django/pull/4846
69-
def signature(f):
70-
sig = inspect.signature(f)
71-
args = [
72-
p.name for p in sig.parameters.values()
73-
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
74-
]
75-
varargs = [
76-
p.name for p in sig.parameters.values()
77-
if p.kind == inspect.Parameter.VAR_POSITIONAL
78-
]
79-
varargs = varargs[0] if varargs else None
80-
keywords = [
81-
p.name for p in sig.parameters.values()
82-
if p.kind == inspect.Parameter.VAR_KEYWORD
83-
]
84-
keywords = keywords[0] if keywords else None
85-
defaults = [
86-
p.default for p in sig.parameters.values()
87-
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
88-
and p.default is not p.empty
89-
] or None
90-
argspec = namedtuple('Signature', ['args', 'defaults',
91-
'varargs', 'keywords'])
92-
return argspec(args, defaults, varargs, keywords)
9365
else:
9466
# Python 2
9567
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
@@ -103,9 +75,6 @@ def str_to_bytes(s, encoding='ascii'):
10375
def bytes_to_str(b, encoding='ascii'):
10476
return b
10577

106-
def signature(f):
107-
return inspect.getargspec(f)
108-
10978
if PY2:
11079
def iteritems(obj, **kw):
11180
return obj.iteritems(**kw)

pandas/core/apply.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import inspect
12
import warnings
23

34
import numpy as np
45

56
from pandas._libs import reduction
6-
import pandas.compat as compat
77
from pandas.util._decorators import cache_readonly
88

99
from pandas.core.dtypes.common import (
@@ -123,7 +123,7 @@ def get_result(self):
123123
# Some methods (shift, etc.) require the axis argument, others
124124
# don't, so inspect and insert if necessary.
125125
func = getattr(self.obj, self.f)
126-
sig = compat.signature(func)
126+
sig = inspect.getfullargspec(func)
127127
if 'axis' in sig.args:
128128
self.kwds['axis'] = self.axis
129129
return func(*self.args, **self.kwds)

pandas/tests/reductions/test_stat_reductions.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
Tests for statistical reductions of 2nd moment or higher: var, skew, kurt, ...
44
"""
5+
import inspect
56

67
import numpy as np
78
import pytest
@@ -10,7 +11,7 @@
1011
import pandas.util._test_decorators as td
1112

1213
import pandas as pd
13-
from pandas import DataFrame, Series, compat
14+
from pandas import DataFrame, Series
1415
import pandas.util.testing as tm
1516

1617

@@ -75,7 +76,7 @@ def _check_stat_op(self, name, alternate, string_series_,
7576
f(string_series_, axis=1)
7677

7778
# Unimplemented numeric_only parameter.
78-
if 'numeric_only' in compat.signature(f).args:
79+
if 'numeric_only' in inspect.getfullargspec(f).args:
7980
with pytest.raises(NotImplementedError, match=name):
8081
f(string_series_, numeric_only=True)
8182

pandas/util/_decorators.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import warnings
55

66
from pandas._libs.properties import cache_readonly # noqa
7-
from pandas.compat import signature
87

98

109
def deprecate(name, alternative, version, alt_name=None,
@@ -335,7 +334,7 @@ def make_signature(func):
335334
(['a', 'b', 'c=2'], ['a', 'b', 'c'])
336335
"""
337336

338-
spec = signature(func)
337+
spec = inspect.getfullargspec(func)
339338
if spec.defaults is None:
340339
n_wo_defaults = len(spec.args)
341340
defaults = ('',) * n_wo_defaults
@@ -347,6 +346,6 @@ def make_signature(func):
347346
args.append(var if default == '' else var + '=' + repr(default))
348347
if spec.varargs:
349348
args.append('*' + spec.varargs)
350-
if spec.keywords:
351-
args.append('**' + spec.keywords)
349+
if spec.varkw:
350+
args.append('**' + spec.varkw)
352351
return args, spec.args

scripts/validate_docstrings.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050

5151
sys.path.insert(0, os.path.join(BASE_PATH))
5252
import pandas
53-
from pandas.compat import signature
5453

5554
sys.path.insert(1, os.path.join(BASE_PATH, 'doc', 'sphinxext'))
5655
from numpydoc.docscrape import NumpyDocString
@@ -420,16 +419,16 @@ def signature_parameters(self):
420419
# accessor classes have a signature but don't want to show this
421420
return tuple()
422421
try:
423-
sig = signature(self.obj)
422+
sig = inspect.getfullargspec(self.obj)
424423
except (TypeError, ValueError):
425424
# Some objects, mainly in C extensions do not support introspection
426425
# of the signature
427426
return tuple()
428427
params = sig.args
429428
if sig.varargs:
430429
params.append("*" + sig.varargs)
431-
if sig.keywords:
432-
params.append("**" + sig.keywords)
430+
if sig.varkw:
431+
params.append("**" + sig.varkw)
433432
params = tuple(params)
434433
if params and params[0] in ('self', 'cls'):
435434
return params[1:]

0 commit comments

Comments
 (0)