-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144325: Improve error messages for {}-style formatters for int, float, str, and complex
#144326
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
Lil-Ran
wants to merge
7
commits into
python:main
Choose a base branch
from
Lil-Ran:formatter-error-msg
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
157454d
Improve Unicode handling
Lil-Ran af8045f
Check type code before thousands separator; limit fractional part gro…
Lil-Ran b6b0e29
Clarify which part is wrong
Lil-Ran 80673fb
Unify the type code switch
Lil-Ran e4d8bc9
Pass current tests
Lil-Ran 943d129
Add new tests
Lil-Ran 78a7677
blurb
Lil-Ran 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -661,55 +661,190 @@ def test_g_format_has_no_trailing_zeros(self): | |||||
| self.assertEqual(format(12300050.0, "#.6g"), "1.23000e+07") | ||||||
|
|
||||||
| def test_with_two_commas_in_format_specifier(self): | ||||||
| error_msg = re.escape("Cannot specify ',' with ','.") | ||||||
| error_msg = re.escape( | ||||||
| "Cannot specify grouping character ',' more than once") | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:,,}'.format(1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:.,,}'.format(1.1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:.,,f}'.format(1.1) | ||||||
|
|
||||||
| def test_with_two_underscore_in_format_specifier(self): | ||||||
| error_msg = re.escape("Cannot specify '_' with '_'.") | ||||||
| error_msg = re.escape( | ||||||
| "Cannot specify grouping character '_' more than once") | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:__}'.format(1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:.__}'.format(1.1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:.__f}'.format(1.1) | ||||||
|
|
||||||
| def test_with_a_commas_and_an_underscore_in_format_specifier(self): | ||||||
| error_msg = re.escape("Cannot specify both ',' and '_'.") | ||||||
| def test_with_a_comma_and_an_underscore_in_format_specifier(self): | ||||||
| error_msg = re.escape("Cannot specify both ',' and '_'") | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:,_}'.format(1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:.,_}'.format(1.1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:.,_f}'.format(1.1) | ||||||
|
|
||||||
| def test_with_an_underscore_and_a_comma_in_format_specifier(self): | ||||||
| error_msg = re.escape("Cannot specify both ',' and '_'.") | ||||||
| error_msg = re.escape("Cannot specify both ',' and '_'") | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:_,}'.format(1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:._,}'.format(1.1) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| '{:._,f}'.format(1.1) | ||||||
|
|
||||||
| def test_better_error_message_format(self): | ||||||
| def test_invalid_format_specifier_error_message(self): | ||||||
| # https://bugs.python.org/issue20524 | ||||||
| for value in [12j, 12, 12.0, "12"]: | ||||||
| with self.subTest(value=value): | ||||||
| for bad_spec in ["%M", "ЫйXЯЧ", "\n'\\"]: | ||||||
| # 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): | ||||||
| str_err = re.escape( | ||||||
| "Invalid format specifier '%ЫйЯЧ' for object of type 'str'") | ||||||
| with self.assertRaisesRegex(ValueError, str_err): | ||||||
| "{a:%ЫйЯЧ}".format(a='a') | ||||||
| with self.subTest(value=value, bad_spec=bad_spec): | ||||||
| err = re.escape("Invalid format specifier " | ||||||
| f"{bad_spec!r} for object of type " | ||||||
| f"'{type(value).__name__}'") | ||||||
| with self.assertRaisesRegex(ValueError, err): | ||||||
| f"xx{{value:{bad_spec}}}yy".format(value=value) | ||||||
|
|
||||||
| # Also test the builtin format() function. | ||||||
| with self.assertRaisesRegex(ValueError, err): | ||||||
| format(value, bad_spec) | ||||||
|
|
||||||
| # Also test f-strings. | ||||||
| with self.assertRaisesRegex(ValueError, err): | ||||||
| eval("f'xx{value:{bad_spec}}yy'") | ||||||
|
|
||||||
| def test_invalid_specifier_type_error_message(self): | ||||||
| for value in [12j, 12, 12.0, "12"]: | ||||||
| for bad_spec, repr in [ | ||||||
| ("M", "'M'"), | ||||||
| ("10$", "'$'"), | ||||||
| ("\t", "U+0009"), | ||||||
| (",\x7f", "U+007F"), | ||||||
| ("о", "'о' (U+043E)"), | ||||||
| ("+#020,🐍", "'🐍' (U+1F40D)") | ||||||
| ]: | ||||||
| with self.subTest(value=value, bad_spec=bad_spec): | ||||||
| err = re.escape("Unknown format code " | ||||||
| f"{repr} for object of type " | ||||||
|
Contributor
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.
Suggested change
|
||||||
| f"'{type(value).__name__}'") | ||||||
| with self.assertRaisesRegex(ValueError, err): | ||||||
| f"xx{{value:{bad_spec}}}yy".format(value=value) | ||||||
|
|
||||||
| # Also test the builtin format() function. | ||||||
| with self.assertRaisesRegex(ValueError, err): | ||||||
| format(value, bad_spec) | ||||||
|
|
||||||
| # Also test f-strings. | ||||||
| with self.assertRaisesRegex(ValueError, err): | ||||||
| eval("f'xx{value:{bad_spec}}yy'") | ||||||
|
|
||||||
| def test_specifier_grouping_with_types(self): | ||||||
| def assertEqualGroup(spec, value, expected): | ||||||
| with self.subTest(spec=spec, value=value): | ||||||
| self.assertEqual(("{:%s}" % spec).format(value), expected) | ||||||
| self.assertEqual(format(value, spec), expected) | ||||||
| self.assertEqual(f"{value:{spec}}", expected) | ||||||
|
|
||||||
| def assertRaisesGroup(spec, value, error_msg): | ||||||
| with self.subTest(spec=spec, value=value): | ||||||
| error_msg = re.escape(error_msg) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| ("{:%s}" % spec).format(value) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| format(value, spec) | ||||||
| with self.assertRaisesRegex(ValueError, error_msg): | ||||||
| f"{value:{spec}}" | ||||||
|
|
||||||
| value = 1234567 | ||||||
| assertEqualGroup(",", value, "1,234,567") | ||||||
| assertRaisesGroup("._", value, | ||||||
| "Cannot specify '_' in fractional part with 'd'") | ||||||
| assertEqualGroup(",d", value, "1,234,567") | ||||||
| assertRaisesGroup("._d", value, | ||||||
| "Cannot specify '_' in fractional part with 'd'") | ||||||
| assertEqualGroup(",e", value, "1.234567e+06") | ||||||
| assertEqualGroup("._e", value, "1.234_567e+06") | ||||||
| assertRaisesGroup(",b", value, "Cannot specify ',' with 'b'") | ||||||
| assertEqualGroup("_b", 1234, "100_1101_0010") | ||||||
| assertRaisesGroup("._b", value, | ||||||
| "Cannot specify '_' in fractional part with 'b'") | ||||||
| assertRaisesGroup(",s", value, "Cannot specify ',' with 's'") | ||||||
| assertRaisesGroup("._s", value, | ||||||
| "Cannot specify '_' in fractional part with 's'") | ||||||
| assertRaisesGroup(",n", value, "Cannot specify ',' with 'n'") | ||||||
| assertRaisesGroup("._n", value, | ||||||
| "Cannot specify '_' in fractional part with 'n'") | ||||||
|
|
||||||
| value = 1234567.1234567 | ||||||
| assertEqualGroup(",", value, "1,234,567.1234567") | ||||||
| assertEqualGroup("._", value, "1234567.123_456_7") | ||||||
| assertRaisesGroup(",d", value, | ||||||
| "Unknown format code 'd' for object of type 'float'") | ||||||
| assertRaisesGroup("._d", value, | ||||||
| "Cannot specify '_' in fractional part with 'd'") | ||||||
| assertEqualGroup(",e", value, "1.234567e+06") | ||||||
| assertEqualGroup("._e", value, "1.234_567e+06") | ||||||
| assertRaisesGroup(",b", value, "Cannot specify ',' with 'b'") | ||||||
| assertRaisesGroup("_b", value, | ||||||
| "Unknown format code 'b' for object of type 'float'") | ||||||
| assertRaisesGroup("._b", value, | ||||||
| "Cannot specify '_' in fractional part with 'b'") | ||||||
| assertRaisesGroup(",s", value, "Cannot specify ',' with 's'") | ||||||
| assertRaisesGroup("._s", value, | ||||||
| "Cannot specify '_' in fractional part with 's'") | ||||||
| assertRaisesGroup(",n", value, "Cannot specify ',' with 'n'") | ||||||
| assertRaisesGroup("._n", value, | ||||||
| "Cannot specify '_' in fractional part with 'n'") | ||||||
|
|
||||||
| value = 1234567.1234567+1234567.1234567j | ||||||
| assertEqualGroup(",", value, "(1,234,567.1234567+1,234,567.1234567j)") | ||||||
| assertEqualGroup("._", value, "(1234567.123_456_7+1234567.123_456_7j)") | ||||||
| assertRaisesGroup(",d", value, | ||||||
| "Unknown format code 'd' for object of type 'complex'") | ||||||
| assertRaisesGroup("._d", value, | ||||||
| "Cannot specify '_' in fractional part with 'd'") | ||||||
| assertEqualGroup(",e", value, "1.234567e+06+1.234567e+06j") | ||||||
| assertEqualGroup("._e", value, "1.234_567e+06+1.234_567e+06j") | ||||||
| assertRaisesGroup(",b", value, "Cannot specify ',' with 'b'") | ||||||
| assertRaisesGroup("_b", value, | ||||||
| "Unknown format code 'b' for object of type 'complex'") | ||||||
| assertRaisesGroup("._b", value, | ||||||
| "Cannot specify '_' in fractional part with 'b'") | ||||||
| assertRaisesGroup(",s", value, "Cannot specify ',' with 's'") | ||||||
| assertRaisesGroup("._s", value, | ||||||
| "Cannot specify '_' in fractional part with 's'") | ||||||
| assertRaisesGroup(",n", value, "Cannot specify ',' with 'n'") | ||||||
| assertRaisesGroup("._n", value, | ||||||
| "Cannot specify '_' in fractional part with 'n'") | ||||||
|
|
||||||
| value = "1234567" | ||||||
| assertRaisesGroup(",", value, "Cannot specify ',' with 's'") | ||||||
| assertRaisesGroup("._", value, | ||||||
| "Cannot specify '_' in fractional part with 's'") | ||||||
| assertRaisesGroup(",d", value, | ||||||
| "Unknown format code 'd' for object of type 'str'") | ||||||
| assertRaisesGroup("._d", value, | ||||||
| "Cannot specify '_' in fractional part with 'd'") | ||||||
| assertRaisesGroup(",e", value, | ||||||
| "Unknown format code 'e' for object of type 'str'") | ||||||
| assertRaisesGroup("._e", value, | ||||||
| "Unknown format code 'e' for object of type 'str'") | ||||||
| assertRaisesGroup(",b", value, "Cannot specify ',' with 'b'") | ||||||
| assertRaisesGroup("_b", value, | ||||||
| "Unknown format code 'b' for object of type 'str'") | ||||||
| assertRaisesGroup("._b", value, | ||||||
| "Cannot specify '_' in fractional part with 'b'") | ||||||
| assertRaisesGroup(",s", value, "Cannot specify ',' with 's'") | ||||||
| assertRaisesGroup("._s", value, | ||||||
| "Cannot specify '_' in fractional part with 's'") | ||||||
| assertRaisesGroup(",n", value, "Cannot specify ',' with 'n'") | ||||||
| assertRaisesGroup("._n", value, | ||||||
| "Cannot specify '_' in fractional part with 'n'") | ||||||
|
|
||||||
| def test_negative_zero(self): | ||||||
| ## default behavior | ||||||
|
|
||||||
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
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/Core_and_Builtins/2026-01-30-02-08-34.gh-issue-144325.O3GZ80.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,5 @@ | ||
| Improves error messages for ``{}``-style formatters for ``str``, ``float``, | ||
| ``int``, and ``complex``. Make error messages handle Unicode characters | ||
| properly. Make grouping characters ``,`` and ``_`` in fractional part only | ||
| allowed for floating-point presentation types (``e``, ``f``, ``g``, ``E``, | ||
| ``G``, ``%``, and ``F``). |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Nit:
repris a built-in.