Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

bfrascher
Copy link
Contributor

This patch changes it so thatinspect.signature is used instead of
inspect.getfullargspec to get a function's signature, when using Python 3.

Python 3.3 introduced theinspect.signature function as a new way to get the
signature of a function (as an alternative toinspect.getargspec and
inspect.getfullargspec).inspect.signature has the advantage that it
preserves the signature of a decorated function iffunctools.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 thesignature of a function (as an alternative to `inspect.getargspec` and`inspect.getfullargspec`).  `inspect.signature` has the advantage that itpreserves the signature of a decorated function if `functools.wraps` is used todecorated the wrapper function.  Having a function's signature available is veryhepful, 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.  Thishas 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`.  Theexceptions from `inspect.getargspec` and `inspect.getfullargspec` are all caughtin the code, but `inspect.signature` raises a `ValueError` instead of a`TypeError`.  This exception is now also caught.
@sebastinassebastinas merged commitcd6886a intobpython:masterApr 2, 2019
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@bfrascher@sebastinas

[8]ページ先頭

©2009-2025 Movatter.jp