Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDisplay the correct signature for a decorated function in Python 3 #765
Conversation
This patch changes it so that `inspect.signature` is used instead of `inspect.getfullargspec` to get a function's signature, when using Python 3. Python 3.3 introduced the `inspect.signature` function as a new way to get the signature of a function (as an alternative to `inspect.getargspec` and `inspect.getfullargspec`). `inspect.signature` has the advantage that it preserves the signature of a decorated function if `functools.wraps` is used to decorated the wrapper function. Having a function's signature available is very hepful, especially when testing things out in a REPL.
Instead of referencing parameter kinds as class attributes on the private `_ParameterKind` class we reference them on the public `Parameter` class. This has two advantages: 1) We don't use a private interface. 2) The class attributes on `_ParameterKind` have only been added in Python 3.5, but on `Parameter` they have existed since Python 3.3.
Some built-in functions (e.g. `map`) can't be inspected with `inspect.getargspec`, `inspect.getfullargspec` or `inspect.signature`. The exceptions from `inspect.getargspec` and `inspect.getfullargspec` are all caught in the code, but `inspect.signature` raises a `ValueError` instead of a `TypeError`. This exception is now also caught.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
bfrascher commentedFeb 16, 2019
This patch changes it so that
inspect.signatureis used instead ofinspect.getfullargspecto get a function's signature, when using Python 3.Python 3.3 introduced the
inspect.signaturefunction as a new way to get thesignature of a function (as an alternative to
inspect.getargspecandinspect.getfullargspec).inspect.signaturehas the advantage that itpreserves the signature of a decorated function if
functools.wrapsis used todecorated the wrapper function. Having a function's signature available is very
hepful, especially when testing things out in a REPL.
The below images show the change in action (first the old behavior, then the new one):
I was only able to test the changes on Python 3.7.2, but since all used functionality is available since Python 3.3 (and I only touch code paths used on Python 3) I don't think anything should break on other versions.