Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-108901: Add inspect.Signature.from_frame#116537
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
Open
sobolevn wants to merge 7 commits into python:mainChoose a base branch from sobolevn:issue-108901-signature-from_frame
base:main
Could not load branches
Branch not found: {{refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+163 −4
Open
Changes from all commits
Commits
Show all changes
7 commits Select commit Hold shift + click to select a range
e0df986 gh-108901: Add `inspect.Signature.from_frame`
sobolevn b434fbc Improve docs
sobolevn bbe3229 Improve docs
sobolevn 7290561 Address review
sobolevn 76ae2ef Typos!
sobolevn bfe30fe Add tests for cleared frame
sobolevn 26b5291 Do not show defaults for parameters
sobolevn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -361,6 +361,11 @@ and only logged in :ref:`Python Development Mode <devmode>` or on :ref:`Python | ||
| built on debug mode <debug-build>`. | ||
| (Contributed by Victor Stinner in :gh:`62948`.) | ||
| inspect | ||
| ------- | ||
| * Add :meth:`inspect.Signature.from_frame` to get signatures from frame objects. | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need the "Contributed by" part? (to have the issue number) | ||
| ipaddress | ||
| --------- | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4623,6 +4623,104 @@ class D2(D1): | ||
| self.assertEqual(inspect.signature(D2), inspect.signature(D1)) | ||
| class TestSignatureFromFrame(unittest.TestCase): | ||
| def test_from_frame(self): | ||
| ns ={} | ||
| def inner(a, /, b, *e, c: int = 3, d, **f) -> None: | ||
| ns['fr'] = inspect.currentframe() | ||
| inner(1, 2, d=4) | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(a, /, b, *e, c, d, **f)') | ||
| def test_from_frame_with_pos_only_defaults(self): | ||
| ns ={} | ||
| def inner(a=1, /, b=2, *e, c: int = 3, d, **f) -> None: | ||
| ns['fr'] = inspect.currentframe() | ||
| inner(d=4) | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(a, /, b, *e, c, d, **f)') | ||
| def test_from_frame_no_locals(self): | ||
| ns ={} | ||
| def inner(): | ||
| ns['fr'] = inspect.currentframe() | ||
| inner() | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '()') | ||
| def test_from_frame_no_pos(self): | ||
| ns ={} | ||
| def inner(*, a, b=2, **c): | ||
| ns['fr'] = inspect.currentframe() | ||
| inner(a=1) | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(*, a, b, **c)') | ||
| def test_from_frame_no_kw(self): | ||
| ns ={} | ||
| def inner(a, /, b, *c): | ||
| ns['fr'] = inspect.currentframe() | ||
| inner(1, 2) | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(a, /, b, *c)') | ||
| def test_from_frame_with_nonlocal(self): | ||
| fr = None | ||
| def inner(a, /, b, *c): | ||
| nonlocal fr | ||
| fr = inspect.currentframe() | ||
| inner(1, 2) | ||
| self.assertEqual(str(inspect.Signature.from_frame(fr)), | ||
| '(a, /, b, *c)') | ||
| def test_clear_frame(self): | ||
| ns ={} | ||
| def inner(a=1, /, c=5, *, b=2): | ||
| ns['fr'] = inspect.currentframe() | ||
| inner() | ||
| ns['fr'].clear() | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(a, /, c, *, b)') | ||
| def test_from_method_frame(self): | ||
| ns ={} | ||
| class _A: | ||
| def inner(self, a, *, b): | ||
| ns['fr'] = inspect.currentframe() | ||
| _A().inner(1, b=2) | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(self, a, *, b)') | ||
| def test_from_frame_defaults_change(self): | ||
| ns ={} | ||
| def inner(a=1, /, c=5, *, b=2): | ||
| a = 3 | ||
| ns['fr'] = inspect.currentframe() | ||
| b = 4 | ||
| inner() | ||
| self.assertEqual(str(inspect.Signature.from_frame(ns['fr'])), | ||
| '(a, /, c, *, b)') | ||
| def test_from_frame_mod(self): | ||
| self.assertEqual(str(inspect.Signature.from_frame(mod.fr)), | ||
| '(x, y)') | ||
| self.assertEqual(str(inspect.Signature.from_frame(mod.fr.f_back)), | ||
| '(a, /, b, c, d, e, f, *g, **h)') | ||
| def test_from_not_frame(self): | ||
| with self.assertRaisesRegex(TypeError, 'Frame object expected'): | ||
| inspect.Signature.from_frame(lambda: ...) | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| class TestParameterObject(unittest.TestCase): | ||
| def test_signature_parameter_kinds(self): | ||
| P = inspect.Parameter | ||
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2024-03-09-12-55-44.gh-issue-108901.s9VClL.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :meth:`inspect.Signature.from_frame` to get a signature | ||
| from a frame object. |
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.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.