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-20524: adds better error message for .format() #28310

Merged
merged 5 commits into from Sep 24, 2021

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Sep 13, 2021

This is most likely can be considered as WIP, because I'm still not very familiar with C-API.
Would be happy to learn / address review comments though 🙂

https://bugs.python.org/issue20524

https://bugs.python.org/issue20524

Python/formatter_unicode.c Outdated Show resolved Hide resolved
Python/formatter_unicode.c Outdated Show resolved Hide resolved
Lib/test/test_format.py Outdated Show resolved Hide resolved
Lib/test/test_format.py Outdated Show resolved Hide resolved
Lib/test/test_format.py Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

bedevere-bot commented Sep 13, 2021

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.

@sobolevn
Copy link
Member Author

sobolevn commented Sep 13, 2021

@ericvsmith Thanks a lot! I've addressed almost all your comments, except ones that you clearly wanted to have some extra research on your side. Let me know if you need my help!

@sobolevn
Copy link
Member Author

sobolevn commented Sep 13, 2021

P.S. To make @bedevere-bot happy: I have made the requested changes; please review again

@bedevere-bot
Copy link

bedevere-bot commented Sep 13, 2021

Thanks for making the requested changes!

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

@bedevere-bot bedevere-bot requested a review from ericvsmith Sep 13, 2021
@ericvsmith
Copy link
Member

ericvsmith commented Sep 13, 2021

Thanks! I'll see if I can figure out why it's printing out more than just the format spec.

I'm also going to mark this as back-portable to 3.10 and 3.9, since changing exception text is okay in a dot release.

@ericvsmith
Copy link
Member

ericvsmith commented Sep 19, 2021

Here's what I don't understand:

>>> "{a:%k}".format(a='a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier '{a:%k}' for object of type 'str'
>>> format('a', '%k')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier '%k' for object of type 'str'
>>> f'{"a":%k}'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier '%k' for object of type 'str'

Why does the .format() error message say '{a:%k}', but the other two day %k?

For reference, this is what it looks like in 3.8 (the newest version I have around, but I don't think it makes a difference):

>>> "{a:%k}".format(a='a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
>>> format('a', '%k')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
>>> f'{"a":%k}'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier

@sobolevn
Copy link
Member Author

sobolevn commented Sep 23, 2021

In the meantime I will add more tests for format() function, % and f''

Python/formatter_unicode.c Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

bedevere-bot commented Sep 23, 2021

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.

@sobolevn
Copy link
Member Author

sobolevn commented Sep 24, 2021

@ericvsmith Thanks! I've checked all other usages of PyUnicode_FromKindAndData, all other cases has explici NULL compare. So, I've modified it.

To make bot happy: I have made the requested changes; please review again.

@bedevere-bot
Copy link

bedevere-bot commented Sep 24, 2021

Thanks for making the requested changes!

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

@bedevere-bot bedevere-bot requested a review from ericvsmith Sep 24, 2021
Copy link
Member

@ericvsmith ericvsmith left a comment

A small tweak to the tests, and I think this is finished. Thanks for indulging me with the changes!

Lib/test/test_format.py Show resolved Hide resolved
with self.assertRaisesRegex(ValueError, str_err):
"{a:%%k}".format(a='a')

def test_unicode_in_error_message(self):
Copy link
Member

@ericvsmith ericvsmith Sep 24, 2021

Choose a reason for hiding this comment

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

I hadn't noticed this test before. It's great, especially because of the work in using "kind" to compute the actual error message.

@bedevere-bot
Copy link

bedevere-bot commented Sep 24, 2021

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.

@ericvsmith
Copy link
Member

ericvsmith commented Sep 24, 2021

I'm also going to mark this as back-portable to 3.10 and 3.9, since changing exception text is okay in a dot release.

I'm going to ask on python-dev and see how people feel about this.

@ericvsmith
Copy link
Member

ericvsmith commented Sep 24, 2021

I'm just waiting on the backport decision to hear if anyone comments on https://mail.python.org/archives/list/python-dev@python.org/message/G6H3M7USSQ2YCFMATMU24J5BJMDJPDVW/

@ericvsmith
Copy link
Member

ericvsmith commented Sep 24, 2021

The feedback on python-dev is to not backport this. Which is a bummer, but it's the right decision.

[Edit: there already is a blurb. Nevermind.]

@ericvsmith ericvsmith merged commit 8d87291 into python:main Sep 24, 2021
12 checks passed
@sobolevn
Copy link
Member Author

sobolevn commented Sep 24, 2021

@ericvsmith No worries!

could you create a what's new (blurb) entry for it?

You mean something like this? https://github.com/sobolevn/cpython/blob/4a11a03e8922af636c3ff0b3d6d7d6e255304c2b/Misc/NEWS.d/next/Library/2021-09-13-14-59-01.[bpo-20524](https://bugs.python.org/issue20524).PMQ1Fh.rst If yes, it is already included. If not, then I need more help - I am not quite familiar with the process - where should I put it? 🙂

@ericvsmith
Copy link
Member

ericvsmith commented Sep 24, 2021

@sobolevn: After I wrote that message, I realized you already had a well written blurb that I skipped over.

Thanks for all of your work on this issue. I think it's a great improvement. I just looked at the issue: 7.5 years in the making. Time flies!

@sobolevn
Copy link
Member Author

sobolevn commented Sep 24, 2021

Thanks a lot for your help, @ericvsmith! It was a pleasure 🤝

P.S. I have some other unreviewed PRs in case you are interested https://github.com/python/cpython/pulls/sobolevn?q=is:open 🙂

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.

None yet

4 participants