Skip to content

Display the correct signature for a decorated function in Python 3 #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

bfrascher
Copy link
Contributor

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.

The below images show the change in action (first the old behavior, then the new one):

old

new

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.

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.
@sebastinas sebastinas merged commit cd6886a into bpython:master Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants