-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-108901: Deprecate inspect.getargvalues
and inspect.formatargvalues
, provide modern alternative
#112639
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
6
commits into
python:main
Choose a base branch
from
sobolevn:issue-108901-getargsvalues
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.
Open
gh-108901: Deprecate inspect.getargvalues
and inspect.formatargvalues
, provide modern alternative
#112639
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad7389b
wip
sobolevn d339bdd
Merge branch 'main' into issue-108901-getargsvalues
sobolevn 0edd93a
Simplify the implementation
sobolevn b8d651c
Add news
sobolevn 2d8fb97
Merge branch 'main' into issue-108901-getargsvalues
sobolevn 1c47526
Ready to be reviewed
sobolevn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
|
@@ -472,6 +472,16 @@ def __init__(self, *args, **kwargs): | |
|
||
git.abuse(7, 8, 9) | ||
|
||
def assertDeprecated(self, name): | ||
import re | ||
return self.assertWarnsRegex( | ||
DeprecationWarning, | ||
re.escape( | ||
f"{name!r} is deprecated and slated " | ||
"for removal in Python 3.15", | ||
), | ||
) | ||
|
||
def test_abuse_done(self): | ||
self.istest(inspect.istraceback, 'git.ex.__traceback__') | ||
self.istest(inspect.isframe, 'mod.fr') | ||
|
@@ -518,21 +528,45 @@ def test_trace(self): | |
self.assertEqual(frame3.positions, dis.Positions(18, 18, 8, 13)) | ||
|
||
def test_frame(self): | ||
args, varargs, varkw, locals = inspect.getargvalues(mod.fr) | ||
with self.assertDeprecated('getargvalues'): | ||
args, varargs, varkw, locals = inspect.getargvalues(mod.fr) | ||
self.assertEqual(args, ['x', 'y']) | ||
self.assertEqual(varargs, None) | ||
self.assertEqual(varkw, None) | ||
self.assertEqual(locals, {'x': 11, 'p': 11, 'y': 14}) | ||
self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), | ||
'(x=11, y=14)') | ||
with self.assertDeprecated('formatargvalues'): | ||
format = inspect.formatargvalues(args, varargs, varkw, locals) | ||
self.assertEqual(format, '(x=11, y=14)') | ||
|
||
def test_previous_frame(self): | ||
args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) | ||
with self.assertDeprecated('getargvalues'): | ||
args, varargs, varkw, locals = inspect.getargvalues(mod.fr.f_back) | ||
self.assertEqual(args, ['a', 'b', 'c', 'd', 'e', 'f']) | ||
self.assertEqual(varargs, 'g') | ||
self.assertEqual(varkw, 'h') | ||
self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), | ||
'(a=7, b=8, c=9, d=3, e=4, f=5, *g=(), **h={})') | ||
with self.assertDeprecated('formatargvalues'): | ||
format = inspect.formatargvalues(args, varargs, varkw, locals) | ||
self.assertEqual(format, | ||
'(a=7, b=8, c=9, d=3, e=4, f=5, *g=(), **h={})') | ||
|
||
def test_frame_with_argument_override(self): | ||
# This tests shows that the current implementation of `getargvalues`: | ||
# 1. Does not render `/` correctly | ||
# 2. Uses not real default values, but can also show redefined values | ||
def inner(a=1, /, c=5, *, b=2): | ||
global fr | ||
a = 3 | ||
fr = inspect.currentframe() | ||
b = 4 | ||
|
||
inner() | ||
with self.assertDeprecated('getargvalues'): | ||
args, varargs, varkw, locals = inspect.getargvalues(fr) | ||
with self.assertDeprecated('formatargvalues'): | ||
format = inspect.formatargvalues(args, varargs, varkw, locals) | ||
self.assertEqual(format, | ||
'(a=3, c=5, b=4)') | ||
|
||
|
||
class GetSourceBase(unittest.TestCase): | ||
# Subclasses must override. | ||
|
@@ -4623,6 +4657,42 @@ class D2(D1): | |
self.assertEqual(inspect.signature(D2), inspect.signature(D1)) | ||
|
||
|
||
class TestSignatureFromFrame(unittest.TestCase): | ||
def test_signature_from_frame(self): | ||
def inner(a=1, /, b=2, *e, c: int = 3, d, **f) -> None: | ||
global fr | ||
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. Using a global sounds like a bad idea. You can use a "nonlocal" instead. Sometimes, I use a mutable type instead, which is more or less the same:
|
||
fr = inspect.currentframe() | ||
|
||
inner(d=4) | ||
self.assertEqual(str(inspect.Signature.from_frame(fr)), | ||
'(a=1, /, b=2, *e, c=3, d=4, **f)') | ||
|
||
def inner(a, /, b, *e, c: int = 3, d, **f) -> None: | ||
global fr | ||
fr = inspect.currentframe() | ||
|
||
inner(1, 2, d=4) | ||
self.assertEqual(str(inspect.Signature.from_frame(fr)), | ||
'(a=1, /, b=2, *e, c=3, d=4, **f)') | ||
|
||
def test_signature_from_frame_defaults_change(self): | ||
def inner(a=1, /, c=5, *, b=2): | ||
global fr | ||
a = 3 | ||
fr = inspect.currentframe() | ||
b = 4 | ||
|
||
inner() | ||
self.assertEqual(str(inspect.Signature.from_frame(fr)), | ||
'(a=3, /, c=5, *, b=4)') | ||
|
||
def test_signature_from_frame_mod(self): | ||
self.assertEqual(str(inspect.Signature.from_frame(mod.fr)), | ||
'(x=11, y=14)') | ||
self.assertEqual(str(inspect.Signature.from_frame(mod.fr.f_back)), | ||
'(a=7, /, b=8, c=9, d=3, e=4, f=5, *g, **h)') | ||
|
||
|
||
class TestParameterObject(unittest.TestCase): | ||
def test_signature_parameter_kinds(self): | ||
P = inspect.Parameter | ||
|
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2023-12-03-10-46-38.gh-issue-108901.lJujFe.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,3 @@ | ||
Deprecate :func:`inspect.getargvalues` and :func:`inspect.formatargvalues`, | ||
slate it for removal in 3.15; instead use | ||
:meth:`inspect.Signature.from_frame`. |
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.
It seems like you can move
if frame.f_locals:
out of this loop and the one below.