Skip to content
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

bpo-35178: Ensure custom formatwarning function can receive line as positional argument #12033

Merged
merged 3 commits into from
Mar 1, 2019

Conversation

tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented Feb 25, 2019

Custom warnings.formatwarning function can receive line as positional argument.

Co-Authored-By: Tashrif Billah tashrifbillah@gmail.com
Co-Authored-By: Xtreak tir.karthi@gmail.com

https://bugs.python.org/issue35178

…rgument.

Co-Authored-By: Tashrif Billah <tashrifbillah@gmail.com>
Co-Authored-By: Xtreak <tir.karthi@gmail.com>
@tirkarthi
Copy link
Member Author

I have added a test with the fix adopted from #10343. Please review.

cc @tashrifbillah @vstinner

@@ -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
Copy link
Member

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}'

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed it.


file_object = StringIO()
original = self.module.formatwarning
self.module.formatwarning = formatwarning
Copy link
Member

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.

Copy link
Member Author

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

@@ -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):
Copy link
Member

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"

@@ -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
Copy link
Member

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().

Copy link
Member Author

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 .

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

Co-Authored-By: Tashrif Billah <tashrifbillah@gmail.com>
Co-Authored-By: Xtreak <tir.karthi@gmail.com>
@tirkarthi
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

@@ -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
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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)

Copy link
Member Author

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

Copy link
Member

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 ;-)

Copy link
Member Author

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.

@miss-islington
Copy link
Contributor

Thanks @tirkarthi for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 1, 2019
Ensure custom formatwarning function can receive line as positional argument.

Co-Authored-By: Tashrif Billah <tashrifbillah@gmail.com>
(cherry picked from commit be7c460)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
@bedevere-bot
Copy link

GH-12130 is a backport of this pull request to the 3.7 branch.

miss-islington added a commit that referenced this pull request Mar 1, 2019
Ensure custom formatwarning function can receive line as positional argument.

Co-Authored-By: Tashrif Billah <tashrifbillah@gmail.com>
(cherry picked from commit be7c460)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants