Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
e0df986b434fbcbbe3229729056176ae2efbfe30fe26b5291File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| --------- | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| class TestParameterObject(unittest.TestCase): | ||
| def test_signature_parameter_kinds(self): | ||
| P = inspect.Parameter | ||
| 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. |
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.