Skip to content

Commit 6baf2ee

Browse files
committed
Revert rest of f4cef9c
Python docs doesn't specify 0-padding behavior, when alignment is specified. Lets be more strict here as Fraction/Decimal's: just reject 0 flag in this case. float's behavior is just odd: >>> format(1.123, '<020f') '1.123000000000000000'
1 parent 26d3b78 commit 6baf2ee

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

mpmath/libmp/libmpf.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,17 +1474,22 @@ def read_format_spec(format_spec):
14741474
format_dict['rounding'] = _GMPY_ROUND_CHAR_DICT[rounding_char]
14751475

14761476
if match['zeropad']:
1477-
if not match['align']:
1478-
format_dict['align'] = '='
1479-
if not match['fill_char']:
1480-
format_dict['fill_char'] = '0'
1481-
1482-
if format_dict['precision'] < 0 and format_dict['type'].lower() not in ['', 'a', 'b']:
1477+
if match['fill_char']:
1478+
raise ValueError("Fill character conflicts with '0'"
1479+
f" in format specifier: '{format_spec}'")
1480+
if match['align']:
1481+
raise ValueError("Alignment conflicts with '0'"
1482+
f" in format specifier: '{format_spec}'")
1483+
format_dict['align'] = '='
1484+
format_dict['fill_char'] = '0'
1485+
1486+
if format_dict['precision'] < 0 and (format_dict['type'].lower()
1487+
not in ['', 'a', 'b']):
14831488
format_dict['precision'] = 6
1484-
else:
1485-
raise ValueError("Invalid format specifier '{}'".format(format_spec))
14861489

1487-
return format_dict
1490+
return format_dict
1491+
1492+
raise ValueError(f"Invalid format specifier '{format_spec}'")
14881493

14891494

14901495
def format_fixed(s, precision=6, rounding=round_nearest):

mpmath/tests/test_format.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,8 @@ def test_mpf_fmt():
618618

619619
# Tests for = alignment
620620
assert f"{mp.mpf('0.24'):=+20.2f}" == '+ 0.24'
621-
assert f"{mp.mpf('0.24'):=+020.2e}" == '+000000000002.40e-01'
622-
assert f"{mp.mpf('0.24'):=+020.2g}" == '+0000000000000000.24'
621+
assert f"{mp.mpf('0.24'):0=+20.2e}" == '+000000000002.40e-01'
622+
assert f"{mp.mpf('0.24'):0=+20.2g}" == '+0000000000000000.24'
623623

624624
# Tests for different kinds of rounding
625625
num = mp.mpf('-1.23456789999901234567')
@@ -816,6 +816,13 @@ def test_errors():
816816
with pytest.raises(ValueError, match="Invalid format specifier '12.3 E '"):
817817
f"{mp.mpf('4'):12.3 E }"
818818

819+
with pytest.raises(ValueError, match="Fill character conflicts"):
820+
f"{mp.mpf('4'):q<03f}"
821+
with pytest.raises(ValueError, match="Alignment conflicts"):
822+
f"{mp.mpf('4'):=03f}"
823+
with pytest.raises(ValueError, match="Fill character conflicts"):
824+
f"{mp.mpf('4'): =03f}"
825+
819826

820827
@settings(max_examples=10000)
821828
@given(st.floats(allow_nan=True, allow_infinity=True,

0 commit comments

Comments
 (0)