-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-20524: adds better error message for .format()
#28310
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da09019
bpo-20524: adds better error message for `.format()`
sobolevn a4db2fd
Addresses review
sobolevn 602f200
Adds an extra test for unicode in error message
sobolevn 022fd1a
Addresses review
sobolevn 4a11a03
Update test_format.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -519,5 +519,33 @@ def test_with_an_underscore_and_a_comma_in_format_specifier(self): | |
with self.assertRaisesRegex(ValueError, error_msg): | ||
'{:_,}'.format(1) | ||
|
||
def test_better_error_message_format(self): | ||
# https://bugs.python.org/issue20524 | ||
for value in [12j, 12, 12.0, "12"]: | ||
with self.subTest(value=value): | ||
# The format spec must be invalid for all types we're testing. | ||
# '%M' will suffice. | ||
bad_format_spec = '%M' | ||
err = re.escape("Invalid format specifier " | ||
f"'{bad_format_spec}' for object of type " | ||
f"'{type(value).__name__}'") | ||
with self.assertRaisesRegex(ValueError, err): | ||
f"xx{{value:{bad_format_spec}}}yy".format(value=value) | ||
|
||
# Also test the builtin format() function. | ||
with self.assertRaisesRegex(ValueError, err): | ||
format(value, bad_format_spec) | ||
|
||
# Also test f-strings. | ||
with self.assertRaisesRegex(ValueError, err): | ||
eval("f'xx{value:{bad_format_spec}}yy'") | ||
|
||
def test_unicode_in_error_message(self): | ||
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. I hadn't noticed this test before. It's great, especially because of the work in using "kind" to compute the actual error message. |
||
str_err = re.escape( | ||
"Invalid format specifier '%ЫйЯЧ' for object of type 'str'") | ||
with self.assertRaisesRegex(ValueError, str_err): | ||
"{a:%ЫйЯЧ}".format(a='a') | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2021-09-13-14-59-01.bpo-20524.PMQ1Fh.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 @@ | ||
Improves error messages on ``.format()`` operation for ``str``, ``float``, | ||
``int``, and ``complex``. New format now shows the problematic pattern and | ||
the object type. |
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
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.
Uh oh!
There was an error while loading. Please reload this page.