mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +00:00
Issue #13248: Remove inspect.getargspec from 3.6 (deprecated from 3.0)
This commit is contained in:
parent
28cff18ded
commit
f1b5ccb993
4 changed files with 7 additions and 79 deletions
|
|
@ -805,24 +805,6 @@ Classes and functions
|
|||
classes using multiple inheritance and their descendants will appear multiple
|
||||
times.
|
||||
|
||||
|
||||
.. function:: getargspec(func)
|
||||
|
||||
Get the names and default values of a Python function's arguments. A
|
||||
:term:`named tuple` ``ArgSpec(args, varargs, keywords, defaults)`` is
|
||||
returned. *args* is a list of the argument names. *varargs* and *keywords*
|
||||
are the names of the ``*`` and ``**`` arguments or ``None``. *defaults* is a
|
||||
tuple of default argument values or ``None`` if there are no default
|
||||
arguments; if this tuple has *n* elements, they correspond to the last
|
||||
*n* elements listed in *args*.
|
||||
|
||||
.. deprecated:: 3.0
|
||||
Use :func:`signature` and
|
||||
:ref:`Signature Object <inspect-signature-object>`, which provide a
|
||||
better introspecting API for callables. This function will be removed
|
||||
in Python 3.6.
|
||||
|
||||
|
||||
.. function:: getfullargspec(func)
|
||||
|
||||
Get the names and default values of a Python function's arguments. A
|
||||
|
|
|
|||
|
|
@ -145,7 +145,8 @@ Removed
|
|||
API and Feature Removals
|
||||
------------------------
|
||||
|
||||
* None yet.
|
||||
* ``inspect.getargspec()`` was removed (was deprecated since CPython 3.0).
|
||||
:func:`inspect.getfullargspec` is an almost drop in replacement.
|
||||
|
||||
|
||||
Porting to Python 3.6
|
||||
|
|
|
|||
|
|
@ -1002,31 +1002,6 @@ def _getfullargs(co):
|
|||
varkw = co.co_varnames[nargs]
|
||||
return args, varargs, kwonlyargs, varkw
|
||||
|
||||
|
||||
ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
|
||||
|
||||
def getargspec(func):
|
||||
"""Get the names and default values of a function's arguments.
|
||||
|
||||
A tuple of four things is returned: (args, varargs, keywords, defaults).
|
||||
'args' is a list of the argument names, including keyword-only argument names.
|
||||
'varargs' and 'keywords' are the names of the * and ** arguments or None.
|
||||
'defaults' is an n-tuple of the default values of the last n arguments.
|
||||
|
||||
Use the getfullargspec() API for Python 3 code, as annotations
|
||||
and keyword arguments are supported. getargspec() will raise ValueError
|
||||
if the func has either annotations or keyword arguments.
|
||||
"""
|
||||
warnings.warn("inspect.getargspec() is deprecated, "
|
||||
"use inspect.signature() instead", DeprecationWarning,
|
||||
stacklevel=2)
|
||||
args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
|
||||
getfullargspec(func)
|
||||
if kwonlyargs or ann:
|
||||
raise ValueError("Function has keyword-only arguments or annotations"
|
||||
", use getfullargspec() API which can support them")
|
||||
return ArgSpec(args, varargs, varkw, defaults)
|
||||
|
||||
FullArgSpec = namedtuple('FullArgSpec',
|
||||
'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
|
||||
|
||||
|
|
|
|||
|
|
@ -629,18 +629,6 @@ class TestClassesAndFunctions(unittest.TestCase):
|
|||
got = inspect.getmro(D)
|
||||
self.assertEqual(expected, got)
|
||||
|
||||
def assertArgSpecEquals(self, routine, args_e, varargs_e=None,
|
||||
varkw_e=None, defaults_e=None, formatted=None):
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
args, varargs, varkw, defaults = inspect.getargspec(routine)
|
||||
self.assertEqual(args, args_e)
|
||||
self.assertEqual(varargs, varargs_e)
|
||||
self.assertEqual(varkw, varkw_e)
|
||||
self.assertEqual(defaults, defaults_e)
|
||||
if formatted is not None:
|
||||
self.assertEqual(inspect.formatargspec(args, varargs, varkw, defaults),
|
||||
formatted)
|
||||
|
||||
def assertFullArgSpecEquals(self, routine, args_e, varargs_e=None,
|
||||
varkw_e=None, defaults_e=None,
|
||||
kwonlyargs_e=[], kwonlydefaults_e=None,
|
||||
|
|
@ -659,23 +647,6 @@ class TestClassesAndFunctions(unittest.TestCase):
|
|||
kwonlyargs, kwonlydefaults, ann),
|
||||
formatted)
|
||||
|
||||
def test_getargspec(self):
|
||||
self.assertArgSpecEquals(mod.eggs, ['x', 'y'], formatted='(x, y)')
|
||||
|
||||
self.assertArgSpecEquals(mod.spam,
|
||||
['a', 'b', 'c', 'd', 'e', 'f'],
|
||||
'g', 'h', (3, 4, 5),
|
||||
'(a, b, c, d=3, e=4, f=5, *g, **h)')
|
||||
|
||||
self.assertRaises(ValueError, self.assertArgSpecEquals,
|
||||
mod2.keyworded, [])
|
||||
|
||||
self.assertRaises(ValueError, self.assertArgSpecEquals,
|
||||
mod2.annotated, [])
|
||||
self.assertRaises(ValueError, self.assertArgSpecEquals,
|
||||
mod2.keyword_only_arg, [])
|
||||
|
||||
|
||||
def test_getfullargspec(self):
|
||||
self.assertFullArgSpecEquals(mod2.keyworded, [], varargs_e='arg1',
|
||||
kwonlyargs_e=['arg2'],
|
||||
|
|
@ -689,20 +660,19 @@ class TestClassesAndFunctions(unittest.TestCase):
|
|||
kwonlyargs_e=['arg'],
|
||||
formatted='(*, arg)')
|
||||
|
||||
def test_argspec_api_ignores_wrapped(self):
|
||||
def test_fullargspec_api_ignores_wrapped(self):
|
||||
# Issue 20684: low level introspection API must ignore __wrapped__
|
||||
@functools.wraps(mod.spam)
|
||||
def ham(x, y):
|
||||
pass
|
||||
# Basic check
|
||||
self.assertArgSpecEquals(ham, ['x', 'y'], formatted='(x, y)')
|
||||
self.assertFullArgSpecEquals(ham, ['x', 'y'], formatted='(x, y)')
|
||||
self.assertFullArgSpecEquals(functools.partial(ham),
|
||||
['x', 'y'], formatted='(x, y)')
|
||||
# Other variants
|
||||
def check_method(f):
|
||||
self.assertArgSpecEquals(f, ['self', 'x', 'y'],
|
||||
formatted='(self, x, y)')
|
||||
self.assertFullArgSpecEquals(f, ['self', 'x', 'y'],
|
||||
formatted='(self, x, y)')
|
||||
class C:
|
||||
@functools.wraps(mod.spam)
|
||||
def ham(self, x, y):
|
||||
|
|
@ -780,11 +750,11 @@ class TestClassesAndFunctions(unittest.TestCase):
|
|||
with self.assertRaises(TypeError):
|
||||
inspect.getfullargspec(builtin)
|
||||
|
||||
def test_getargspec_method(self):
|
||||
def test_getfullargspec_method(self):
|
||||
class A(object):
|
||||
def m(self):
|
||||
pass
|
||||
self.assertArgSpecEquals(A.m, ['self'])
|
||||
self.assertFullArgSpecEquals(A.m, ['self'])
|
||||
|
||||
def test_classify_newstyle(self):
|
||||
class A(object):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue