Skip to content

Commit

Permalink
bpo-45320: Remove long-deprecated inspect methods (GH-28618)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 20, 2021
1 parent d8e1819 commit d89fb9a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 260 deletions.
3 changes: 0 additions & 3 deletions Doc/howto/clinic.rst
Expand Up @@ -567,9 +567,6 @@ expression. Currently the following are explicitly supported:
* Simple symbolic constants like ``sys.maxsize``, which must
start with the name of the module

In case you're curious, this is implemented in ``from_builtin()``
in ``Lib/inspect.py``.

(In the future, this may need to get even more elaborate,
to allow full expressions like ``CONSTANT - 1``.)

Expand Down
47 changes: 0 additions & 47 deletions Doc/library/inspect.rst
Expand Up @@ -935,26 +935,6 @@ Classes and functions
times.


.. function:: getargspec(func)

Get the names and default values of a Python function's parameters. A
:term:`named tuple` ``ArgSpec(args, varargs, keywords, defaults)`` is
returned. *args* is a list of the parameter names. *varargs* and *keywords*
are the names of the ``*`` and ``**`` parameters 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:`getfullargspec` for an updated API that is usually a drop-in
replacement, but also correctly handles function annotations and
keyword-only parameters.

Alternatively, use :func:`signature` and
:ref:`Signature Object <inspect-signature-object>`, which provide a
more structured introspection API for callables.


.. function:: getfullargspec(func)

Get the names and default values of a Python function's parameters. A
Expand Down Expand Up @@ -1015,33 +995,6 @@ Classes and functions
This function was inadvertently marked as deprecated in Python 3.5.


.. function:: formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations[, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations]])

Format a pretty argument spec from the values returned by
:func:`getfullargspec`.

The first seven arguments are (``args``, ``varargs``, ``varkw``,
``defaults``, ``kwonlyargs``, ``kwonlydefaults``, ``annotations``).

The other six arguments are functions that are called to turn argument names,
``*`` argument name, ``**`` argument name, default values, return annotation
and individual annotations into strings, respectively.

For example:

>>> from inspect import formatargspec, getfullargspec
>>> def f(a: int, b: float):
... pass
...
>>> formatargspec(*getfullargspec(f))
'(a: int, b: float)'

.. deprecated:: 3.5
Use :func:`signature` and
:ref:`Signature Object <inspect-signature-object>`, which provide a
better introspecting API for callables.


.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])

Format a pretty argument spec from the four values returned by
Expand Down
22 changes: 19 additions & 3 deletions Doc/whatsnew/3.11.rst
Expand Up @@ -363,7 +363,7 @@ Removed
``SO_REUSEADDR`` in UDP.
(Contributed by Hugo van Kemenade in :issue:`45129`.)

* Remove :meth:`__getitem__` methods of
* Removed :meth:`__getitem__` methods of
:class:`xml.dom.pulldom.DOMEventStream`, :class:`wsgiref.util.FileWrapper`
and :class:`fileinput.FileInput`, deprecated since Python 3.9.
(Contributed by Hugo van Kemenade in :issue:`45132`.)
Expand Down Expand Up @@ -402,7 +402,7 @@ Removed
the ``l*gettext()`` functions.
(Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)

* Remove from the :mod:`configparser` module:
* Removed from the :mod:`configparser` module:
the :class:`SafeConfigParser` class,
the :attr:`filename` property of the :class:`ParsingError` class,
the :meth:`readfp` method of the :class:`ConfigParser` class,
Expand All @@ -419,9 +419,25 @@ Removed
generator-based coroutine objects in the debug mode.
(Contributed by Illia Volochii in :issue:`43216`.)

* Remove the deprecated ``split()`` method of :class:`_tkinter.TkappType`.
* Removed the deprecated ``split()`` method of :class:`_tkinter.TkappType`.
(Contributed by Erlend E. Aasland in :issue:`38371`.)

* Removed from the :mod:`inspect` module:

* the ``getargspec`` function, deprecated since Python 3.0;
use :func:`inspect.signature` or :func:`inspect.getfullargspec` instead.

* the ``formatargspec`` function, deprecated since Python 3.5;
use the :func:`inspect.signature` function and :class:`Signature` object
directly.

* the undocumented ``Signature.from_callable`` and ``Signature.from_function``
functions, deprecated since Python 3.5; use the
:meth:`Signature.from_callable() <inspect.Signature.from_callable>` method
instead.

(Contributed by Hugo van Kemenade in :issue:`45320`.)


Porting to Python 3.11
======================
Expand Down
113 changes: 0 additions & 113 deletions Lib/inspect.py
Expand Up @@ -47,7 +47,6 @@
import tokenize
import token
import types
import warnings
import functools
import builtins
from operator import attrgetter
Expand Down Expand Up @@ -1214,37 +1213,6 @@ def getargs(co):
varkw = co.co_varnames[nargs]
return Arguments(args + kwonlyargs, varargs, varkw)

ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')

def getargspec(func):
"""Get the names and default values of a function's parameters.
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 ** parameters or None.
'defaults' is an n-tuple of the default values of the last n parameters.
This function is deprecated, as it does not support annotations or
keyword-only parameters and will raise ValueError if either is present
on the supplied callable.
For a more structured introspection API, use inspect.signature() instead.
Alternatively, use getfullargspec() for an API with a similar namedtuple
based interface, but full support for annotations and keyword-only
parameters.
Deprecated since Python 3.5, use `inspect.getfullargspec()`.
"""
warnings.warn("inspect.getargspec() is deprecated since Python 3.0, "
"use inspect.signature() or inspect.getfullargspec()",
DeprecationWarning, stacklevel=2)
args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
getfullargspec(func)
if kwonlyargs or ann:
raise ValueError("Function has keyword-only parameters or annotations"
", use inspect.signature() API which can support them")
return ArgSpec(args, varargs, varkw, defaults)

FullArgSpec = namedtuple('FullArgSpec',
'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Expand Down Expand Up @@ -1369,63 +1337,6 @@ def _formatannotation(annotation):
return formatannotation(annotation, module)
return _formatannotation

def formatargspec(args, varargs=None, varkw=None, defaults=None,
kwonlyargs=(), kwonlydefaults={}, annotations={},
formatarg=str,
formatvarargs=lambda name: '*' + name,
formatvarkw=lambda name: '**' + name,
formatvalue=lambda value: '=' + repr(value),
formatreturns=lambda text: ' -> ' + text,
formatannotation=formatannotation):
"""Format an argument spec from the values returned by getfullargspec.
The first seven arguments are (args, varargs, varkw, defaults,
kwonlyargs, kwonlydefaults, annotations). The other five arguments
are the corresponding optional formatting functions that are called to
turn names and values into strings. The last argument is an optional
function to format the sequence of arguments.
Deprecated since Python 3.5: use the `signature` function and `Signature`
objects.
"""

from warnings import warn

warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
"the `Signature` object directly",
DeprecationWarning,
stacklevel=2)

def formatargandannotation(arg):
result = formatarg(arg)
if arg in annotations:
result += ': ' + formatannotation(annotations[arg])
return result
specs = []
if defaults:
firstdefault = len(args) - len(defaults)
for i, arg in enumerate(args):
spec = formatargandannotation(arg)
if defaults and i >= firstdefault:
spec = spec + formatvalue(defaults[i - firstdefault])
specs.append(spec)
if varargs is not None:
specs.append(formatvarargs(formatargandannotation(varargs)))
else:
if kwonlyargs:
specs.append('*')
if kwonlyargs:
for kwonlyarg in kwonlyargs:
spec = formatargandannotation(kwonlyarg)
if kwonlydefaults and kwonlyarg in kwonlydefaults:
spec += formatvalue(kwonlydefaults[kwonlyarg])
specs.append(spec)
if varkw is not None:
specs.append(formatvarkw(formatargandannotation(varkw)))
result = '(' + ', '.join(specs) + ')'
if 'return' in annotations:
result += formatreturns(formatannotation(annotations['return']))
return result

def formatargvalues(args, varargs, varkw, locals,
formatarg=str,
Expand Down Expand Up @@ -2932,30 +2843,6 @@ def __init__(self, parameters=None, *, return_annotation=_empty,
self._parameters = types.MappingProxyType(params)
self._return_annotation = return_annotation

@classmethod
def from_function(cls, func):
"""Constructs Signature for the given python function.
Deprecated since Python 3.5, use `Signature.from_callable()`.
"""

warnings.warn("inspect.Signature.from_function() is deprecated since "
"Python 3.5, use Signature.from_callable()",
DeprecationWarning, stacklevel=2)
return _signature_from_function(cls, func)

@classmethod
def from_builtin(cls, func):
"""Constructs Signature for the given builtin function.
Deprecated since Python 3.5, use `Signature.from_callable()`.
"""

warnings.warn("inspect.Signature.from_builtin() is deprecated since "
"Python 3.5, use Signature.from_callable()",
DeprecationWarning, stacklevel=2)
return _signature_from_builtin(cls, func)

@classmethod
def from_callable(cls, obj, *,
follow_wrapped=True, globals=None, locals=None, eval_str=False):
Expand Down

0 comments on commit d89fb9a

Please sign in to comment.