FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-108901: Add `Signature.from_code` method by sobolevn · Pull Request #108902 · python/cpython · GitHub

/ cpython Public

gh-108901: Add Signature.from_code method - #108902

Closed
sobolevn wants to merge 7 commits into
python:mainfrom
sobolevn:issue-108901-getargs
Closed

gh-108901: Add Signature.from_code method#108902
sobolevn wants to merge 7 commits into
python:mainfrom
sobolevn:issue-108901-getargs

Conversation

sobolevn commented Sep 5, 2023
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Member

sobolevn commented Sep 5, 2023

Copy link
Copy Markdown
Member Author

Failures are not related: #108888

Comment thread Lib/inspect.py Outdated

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please restrict this PR to add from_code(). Once it's merged, you can write a PR to deprecate.

sobolevn force-pushed the issue-108901-getargs branch from 95c2219 to 604f25b Compare September 5, 2023 16:57
sobolevn changed the title gh-108901: Deprecate inspect.getargs, use Signature.from_code instead gh-108901: Add Signature.from_code method Sep 5, 2023

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I like your overall change, here is a first review.

Comment thread Lib/inspect.py Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Doc/library/inspect.rst Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Lib/test/test_inspect.py Outdated
Comment thread Lib/test/test_inspect.py Outdated
Comment thread Doc/library/inspect.rst Outdated

sobolevn commented Sep 5, 2023
edited
Loading

Copy link
Copy Markdown
Member Author

I moved my code, so there should be no unrelated diffs now :)

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM, but I would prefer a second core dev review, since I don't know well this module.

Comment thread Doc/library/inspect.rst Outdated

vstinner commented Sep 5, 2023

Copy link
Copy Markdown
Member

@pablogsal @AlexWaygood @1st1: Would you be interested to review this change?

AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

A few nits:

Comment thread Doc/library/inspect.rst Outdated
Comment thread Doc/library/inspect.rst Outdated
Comment thread Doc/library/inspect.rst Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Lib/inspect.py Outdated
Comment thread Lib/inspect.py
Comment thread Lib/inspect.py
Comment thread Lib/inspect.py Outdated
Comment thread Lib/test/test_inspect.py
Comment on lines +3693 to +3699
known_sigs = {
func1: '(a, b, /, c, d, *args, e, f, **kwargs)',
func2: '(a, b, /)',
func3: '()',
func4: '(*a, **k)',
func5: '(*, kw)',
}

AlexWaygood Sep 6, 2023
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Well, I worry that the reason this wasn't implemented in the original PEP-362 implementation is because the signatures produced for func1, func2 and func5 here are all incorrect, due to the fact that certain parameters have default values but this isn't reflected in the produced signatures :/

inspect.getargs() is used a fair bit in third-party projects. In the vast majority of these uses, they already have access to the function object, so they should probably just use Signature.from_callable() instead of this new method. But there are a few uses where they don't have access to the function object -- so I agree that something like this new method is probably needed, as a replacement for those uses:

AlexWaygood Sep 6, 2023
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

the signatures produced for func1, func2 and func5 here are all incorrect

Hmmm... well, they are actually valid signatures for the code object, since if you pass the code object to exec(), it doesn't take any notice of whether the original function had a default value or not:

>>> def foo(x=42): print(x)
...
>>> foo()
42
>>> c = foo.__code__
>>> exec(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() missing 1 required positional argument: 'x'

Why anybody would care what the signature of the code object itself is, though, I'm not sure -- ISTM that everybody trying to get the signature from a code object is just using the code object as the next-best-thing for the function the code object belongs to. I don't think many people are passing code objects directly to exec() It's not even really possible to pass a parameter to a code object you're calling via exec() AFAIK.

AlexWaygood Sep 6, 2023
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe we just need to flag more forcefully in the docs the limitations of this new constructor method. I can offer some suggestions once you've addressed the review comments I've already posted 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I added a .. note:: notation to highlight it even better.

AlexWaygood Sep 8, 2023
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The more I think about this, the more concerned I am :/

The existing getargs() function that we have in inspect gives accurate information. It doesn't claim to be able to tell you everything about the signature: it only claims to be able to tell you some information about the signature:

  • It gives you a list of argument names
  • It tells you the name of the *vararg argument, if there is one
  • It tells you the name of the **kwarg argument, if there is one

This new function, however, claims to be able to give you a complete and accurate signature from a code object (in that it constructs a Signature object from a code object). That's impossible, since code objects don't have information about which arguments have default values, and so the function gives you inaccurate information.

I'm not sure having a prominent note in the docs is sufficient to fix this issue: adding a new constructor that we know gives inaccurate results seems very troubling to me. Perhaps a better solution would be to document getargs and promote it to public API?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

True. This new API would provide helpful information that is not currently offered by getargs.

However. Does it make sense to think of code objects even having a "signature"? Functions have signatures, and you can construct functions from code objects. But as your snippet above demonstrates, you can't directly call code objects (except via exec(), but nobody does that really) -- you have to create a new function from the code object in order to get something that can be called.

Which "signature" are we trying to get here? The signature of the function from which the code object originally came (if it came from a code object), or the signature a new function object would have if we created one from the code object? Getting a "signature from a code object" feels like quite a confusing concept to me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Another option: we could document that if people want to get "signature information" from a code object co, they just need to do this:

inspect.signature(types.FunctionType(co, {}))

That way, they are forced to be explicit that code objects don't really have a "signature": you need to construct a new function from the code object in order to get a code object that actually has a signature

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
>>> import types, inspect
>>> def a(x: int, /, *, y: str = '') -> None: ...
... 
>>> inspect.signature(types.FunctionType(a.__code__, {}))
<Signature (x, /, *, y)>

It is up to you to decide whether or not we should do it. I consider this PR done from my side.

AlexWaygood Sep 8, 2023
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Currently, I'm leaning towards not adding this alternative constructor, and just documenting that people should call inspect.signature(types.FunctionType(co, {})) if they need to get signature information from a code object co, and the function object is not available for whatever reason.

One of the third-party projects that uses getargs is Quora: https://github.com/quora/qcore/blob/e12a4929c8e75ec2dd878c06270cedcac72a643e/qcore/inspection.py#L130-L148. I'd be curious to know if @JelleZijlstra has any thoughts on whether getargs should be documented or deprecated and, if the latter, whether we should add this Signature.from_code() method or just add documentation along the lines of my sugestion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Any code using that qcore function should really be migrated to use inspect.signature directly. As the docstring notes, the function doesn't deal correctly with kwonly arguments.

I think we don't need Signature.from_code. My experience is that it is rare that you have only a code object without a function it came from, and code that manipulates code objects directly should generally be migrated to a more modern inspect API.

sobolevn requested a review from AlexWaygood September 8, 2023 11:10

Copy link
Copy Markdown
Member

Thanks for the PR, @sobolevn -- it was well done. But I think adding a constructor that implies you can construct a complete Signature from a code object is too misleading. Let's go with documenting that you should call inspect.signature(types.FunctionType(co, {})) in the rare cases that you don't have access to the original function object.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL