We have multiple APIs related to callables and signatures in
inspect. Some of them even documented to exist only to match Python2 behavior. Plus, using some of them can lead to potential errors.
Separate issues: I suspect the same would be true of any stdlib code using either getargs or getfullargspec. We should probably deprecate and remove getargs and maybe eventually getfullargspec.
Originally suggested by @tjreedy
While working on `turtle.teleport` has incorrect-ish signature · Issue #107805 · python/cpython · GitHub I’ve noticed that
inspect.getargs does not correctly handle:
- pos-only params
- kw-only params
- some defaults
This was leading to a bug in callable generation, because some kw-only params were implicitly translated into regular ones.
So, I’ve strated looking into
inspect’s API closer. Here’s a list of things that I find problematic:
getargs()undocumented helper used in
getargvalues. It works with
__code__objects. Can it be replaced with modern tooling? Not yet: we have an internal helper to get signatures from
__code__objects: https://github.com/python/cpython/blob/39ef93edb9802dccdb6555d4209ac2e60875a011/Lib/inspect.py#L2423-L2488 But, some refactoring is required
getargvalues()only works with
frameobjects, but it does not work correctly with pos-only and kw-only parameters. Bug: `inspect.getargvalues` does not work correctly for pos-only and kw-only arguments · Issue #107833 · python/cpython · GitHub Can it be replaced with modern tooling? No, I propose adding
formatargvaluesshould also be deprecated, because the only way to use is together with
There was a reverted attempt to depracate them in 3.5
getcallargs()was documented as deprecated for a long time, it also has known bugs with pos-only params (`inspect.getcallargs` does not raise `TypeError` for pos-only passed as keywords · Issue #107831 · python/cpython · GitHub). Can it be replaced with modern tooling? Yes:
getfullargspec()is documented as
signature()and Signature Object provide the recommended API for callable introspection, and support additional behaviours (like positional-only arguments) that are sometimes encountered in extension module APIs. This function is retained primarily for use in code that needs to maintain compatibility with the Python 2
It has a rich history of deprecation / undeprecation:
- Deprecated: inspect: Deprecate getfullargspec? · Issue #64637 · python/cpython · GitHub
- Undeprecated: Undeprecate inspect.getfullargspec() · Issue #71359 · python/cpython · GitHub
- Suggested to be deprecated again: Deprecate legacy introspection APIs in the inspect module · Issue #76371 · python/cpython · GitHub
It is broken in a sense that it does not differentiate pos-only from pos-or-keyword parameters.
Can it be replaced with modern tooling? Partially:
inspect.signaturehas some differences. But,
getargs(): only in
turtle.py, will be fixed in `turtle.teleport` has incorrect-ish signature · Issue #107805 · python/cpython · GitHub
getargvalues(): not used
getcallargs(): not used
getfullargspec(): not used
All of these functions are used in the 3rd party projects:
I propose to:
- Provide modern alternatives to
- Deprecating these 4 functions above with a
3.13with no particular removal date
I can send PRs with new functions and deprecations, if we agree on this.
Please, share your feedback.