Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
address review: format-spec & more tests
  • Loading branch information
skirpichev committed Feb 8, 2025
commit 893421da8912cfa4ddadfe087da4562297cef486
9 changes: 7 additions & 2 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,19 @@ non-empty format specification typically modifies the result.
The general form of a *standard format specifier* is:

.. productionlist:: format-spec
format_spec: [[`fill`]`align`][`sign`]["z"]["#"]["0"][`width`][`grouping_option`]["." `precision` [`grouping_option`]][`type`]
format_spec: [`options`][`width_and_precision`][`type`]
options: [[`fill`]`align`][`sign`]["z"]["#"]["0"]
fill: <any character>
align: "<" | ">" | "=" | "^"
sign: "+" | "-" | " "
width_and_precision: [`width_with_grouping`][`precision_with_grouping`]
width_with_grouping: [`width`][`grouping_option`]
precision_with_grouping: "." [`precision`]`grouping_option`
width: `~python-grammar:digit`+
grouping_option: "_" | ","
precision: `~python-grammar:digit`+
type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g"
: | "G" | "n" | "o" | "s" | "x" | "X" | "%"

If a valid *align* value is specified, it can be preceded by a *fill*
character that can be any character and defaults to a space if omitted.
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,9 @@ def test_format(self):
self.assertEqual(format(x, '+.11,e'), '+1.234,561,234,56e+05')

self.assertRaises(ValueError, format, x, '._6f')
self.assertRaises(ValueError, format, x, '.,_6f')
self.assertRaises(ValueError, format, x, '._,6f')
self.assertRaises(ValueError, format, x, '.,_f')
self.assertRaises(ValueError, format, x, '.6,_f')
self.assertRaises(ValueError, format, x, '.6_,f')
self.assertRaises(ValueError, format, x, '.6_n')
self.assertRaises(ValueError, format, x, '.6,n')

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,15 @@ def test_with_a_commas_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):
'{:.,_f}'.format(1.1)

def test_with_an_underscore_and_a_comma_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):
'{:._,f}'.format(1.1)

def test_better_error_message_format(self):
# https://bugs.python.org/issue20524
Expand Down