-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-35178: Ensure custom formatwarning function can receive line as positional argument #12033
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
Conversation
…rgument. Co-Authored-By: Tashrif Billah <[email protected]> Co-Authored-By: Xtreak <[email protected]>
I have added a test with the fix adopted from #10343. Please review. |
Lib/test/test_warnings/__init__.py
Outdated
@@ -877,6 +877,18 @@ def test_showwarning(self): | |||
file_object, expected_file_line) | |||
self.assertEqual(expect, file_object.getvalue()) | |||
|
|||
# bpo-35178: Test custom formatwarning can receive line as positional | |||
def formatwarning(message, category, filename, lineno, text): | |||
return text |
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.
I would prefer to use all arguments in the result, to check that all arguments are passed and in the correct order.
For example: return f'm={message}:c={category}:f={filename}:l={lineno}:t={text}'
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.
Changed it.
Lib/test/test_warnings/__init__.py
Outdated
|
||
file_object = StringIO() | ||
original = self.module.formatwarning | ||
self.module.formatwarning = formatwarning |
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.
I would prefer to use "with mock.patch.object(self.module, 'formatwarning', formatwarning): ..." or "with support.swap_attr(self.module, 'formatwarning', formatwarning): ..." here, to ensure that the attribute is restored on error.
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.
I initially had self.addCleanup
to restore it but your suggestion looks more compact and robust. I used support.swap_attr
since support
was already imported. Thanks
Lib/test/test_warnings/__init__.py
Outdated
@@ -877,6 +877,18 @@ def test_showwarning(self): | |||
file_object, expected_file_line) | |||
self.assertEqual(expect, file_object.getvalue()) | |||
|
|||
# bpo-35178: Test custom formatwarning can receive line as positional | |||
def formatwarning(message, category, filename, lineno, text): |
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.
nitpick: use a different function name to avoid confusion, ex: "def myformatwarning"
Lib/test/test_warnings/__init__.py
Outdated
@@ -877,6 +877,18 @@ def test_showwarning(self): | |||
file_object, expected_file_line) | |||
self.assertEqual(expect, file_object.getvalue()) | |||
|
|||
# bpo-35178: Test custom formatwarning can receive line as positional |
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.
I would prefer to put the new test in a new method:
def test_formatwarning_override(self):
And maybe move the new test inside test_formatwarning().
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.
I initially had it in test_showwarning
since the exception was triggered by showwarning
. I moved this into a new test test_formatwarning_override
. Let me know if I need to move the test into test_formatwarning
.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be put in the comfy chair! |
Co-Authored-By: Tashrif Billah <[email protected]> Co-Authored-By: Xtreak <[email protected]>
I have made the requested changes; please review again |
Thanks for making the requested changes! @vstinner: please review the changes made to this pull request. |
Lib/test/test_warnings/__init__.py
Outdated
@@ -877,6 +877,24 @@ def test_showwarning(self): | |||
file_object, expected_file_line) | |||
self.assertEqual(expect, file_object.getvalue()) | |||
|
|||
def test_formatwarning_override(self): | |||
# bpo-35178: Test custom formatwarning can receive line as positional |
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 isn't obvious that the last parameter of myformatwarning() is usually called line, but here it's called "text" to test that the argument is passed as a position argument and not as a keyword argument. Try to rephrase the comment.
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.
How about indicating line could be keyword or positional argument inferring line=line
like "Test custom formatwarning can receive line as keyword or positional argument" ? Since line is a common word and also indicates the warning line it's little difficult for me to convey this. Any suggestions would be helpful. Thanks.
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.
Something like this?
# bpo-35178: Test that a custom formatwarning function gets
# the 'line' argument as a positional argument,
# and not as a keyword argument
(I let you format it properly)
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.
I would make a minor edit of "not only as a keyword argument" since this reads a little like line as a keyword argument is not allowed. How about below ?
bpo-35178: Test that a custom formatwarning function gets the 'line'
argument as a positional argument, and not only as a keyword argument
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.
Yeah, that's more explicit than the current comment ;-)
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.
Rephrased the comment as noted.
Thanks @tirkarthi for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
Ensure custom formatwarning function can receive line as positional argument. Co-Authored-By: Tashrif Billah <[email protected]> (cherry picked from commit be7c460) Co-authored-by: Xtreak <[email protected]>
GH-12130 is a backport of this pull request to the 3.7 branch. |
Ensure custom formatwarning function can receive line as positional argument. Co-Authored-By: Tashrif Billah <[email protected]> (cherry picked from commit be7c460) Co-authored-by: Xtreak <[email protected]>
Custom
warnings.formatwarning
function can receiveline
as positional argument.Co-Authored-By: Tashrif Billah [email protected]
Co-Authored-By: Xtreak [email protected]
https://bugs.python.org/issue35178