Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-61449: handle 0-padding option for Fractions like for floats#145765

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
skirpichev wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromskirpichev:support-0-padding-in-fractions/61449
Open
Show file tree
Hide file tree
Changes fromall commits
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
3 changes: 1 addition & 2 deletionsDoc/library/fractions.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -233,8 +233,7 @@ a single number, or from a string.
:ref:`format specification mini-language<formatspec>`. The "alternate
form" flag ``'#'`` is supported: if present, it forces the output string
to always include an explicit denominator, even when the value being
formatted is an exact integer. The zero-fill flag ``'0'`` is not
supported.
formatted is an exact integer.

If the ``format_spec`` format specification string ends with one of
the presentation types ``'e'``, ``'E'``, ``'f'``, ``'F'``, ``'g'``,
Expand Down
14 changes: 10 additions & 4 deletionsLib/fractions.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -500,6 +500,15 @@ def _format_float_style(self, match):
no_neg_zero = bool(match["no_neg_zero"])
alternate_form = bool(match["alt"])
zeropad = bool(match["zeropad"])

# Support zeropad handling like built-in types (see gh-61449).
if match["zeropad"] is not None:
if match["fill"] is not None and match["align"] is not None:
zeropad = False
elif match["align"] is not None:
zeropad = False
fill = "0"

minimumwidth = int(match["minimumwidth"] or "0")
thousands_sep = match["thousands_sep"]
precision = int(match["precision"] or "6")
Expand DownExpand Up@@ -603,10 +612,7 @@ def __format__(self, format_spec, /):
return self._format_general(match)

if match := _FLOAT_FORMAT_SPECIFICATION_MATCHER(format_spec):
# Refuse the temptation to guess if both alignment _and_
# zero padding are specified.
if match["align"] is None or match["zeropad"] is None:
return self._format_float_style(match)
return self._format_float_style(match)

raise ValueError(
f"Invalid format specifier {format_spec!r} "
Expand Down
39 changes: 21 additions & 18 deletionsLib/test/test_fractions.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1326,6 +1326,8 @@ def test_format_e_presentation_type(self):
(F('1234567.123456'), '.5_e', '1.234_57e+06'),
# z flag is legal, but never makes a difference to the output
(F(-1, 7**100), 'z.6e', '-3.091690e-85'),
# fill and/or alignment with zero padding
(F(2, 3), '=010e', format(float(F(2, 3)), '=010e')),
]
for fraction, spec, expected in testcases:
with self.subTest(fraction=fraction, spec=spec):
Expand DownExpand Up@@ -1525,6 +1527,23 @@ def test_format_f_presentation_type(self):
(F(151, 1000), '.1f', '0.2'),
(F(22, 7), '.02f', '3.14'), # issue gh-130662
(F(22, 7), '005.02f', '03.14'),
# fill and/or alignment with zero padding
(F(2, 3), 'X>010f', format(float(F(2, 3)), 'X>010f')),
(F(2, 3), 'X<010f', format(float(F(2, 3)), 'X<010f')),
(F(2, 3), 'X^010f', format(float(F(2, 3)), 'X^010f')),
(F(2, 3), 'X=010f', format(float(F(2, 3)), 'X=010f')),
(F(2, 3), '0>010f', format(float(F(2, 3)), '0>010f')),
(F(2, 3), '0<010f', format(float(F(2, 3)), '0<010f')),
(F(2, 3), '0^010f', format(float(F(2, 3)), '0^010f')),
(F(2, 3), '0=010f', format(float(F(2, 3)), '0=010f')),
(F(2, 3), '>010f', format(float(F(2, 3)), '>010f')),
(F(2, 3), '<010f', format(float(F(2, 3)), '<010f')),
(F(2, 3), '^010f', format(float(F(2, 3)), '^010f')),
(F(2, 3), '=010e', format(float(F(2, 3)), '=010e')),
(F(2, 3), '=010f', format(float(F(2, 3)), '=010f')),
(F(2, 3), '>00.2f', format(float(F(2, 3)), '>00.2f')),
(F(2, 3), '>00f', format(float(F(2, 3)), '>00f')),
(F(2, 3), '=010%', format(float(F(2, 3)), '=010%')),
]
for fraction, spec, expected in testcases:
with self.subTest(fraction=fraction, spec=spec):
Expand DownExpand Up@@ -1593,6 +1612,8 @@ def test_format_g_presentation_type(self):
(F(2**64), '_.25g', '18_446_744_073_709_551_616'),
# As with 'e' format, z flag is legal, but has no effect
(F(-1, 7**100), 'zg', '-3.09169e-85'),
# fill and/or alignment with zero padding
(F(2, 3), '=010g', format(float(F(2, 3)), '=010g')),
]
for fraction, spec, expected in testcases:
with self.subTest(fraction=fraction, spec=spec):
Expand All@@ -1605,24 +1626,6 @@ def test_invalid_formats(self):

invalid_specs = [
'Q6f', # regression test
# illegal to use fill or alignment when zero padding
'X>010f',
'X<010f',
'X^010f',
'X=010f',
'0>010f',
'0<010f',
'0^010f',
'0=010f',
'>010f',
'<010f',
'^010f',
'=010e',
'=010f',
'=010g',
'=010%',
'>00.2f',
'>00f',
# Missing precision
'.e',
'.f',
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Align support of zero-padding option for:class:`~fractions.Fraction`
formatting with builtin numeric types. Patch by Sergey B Kirpichev.
Loading

[8]ページ先頭

©2009-2026 Movatter.jp