You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BTW, I like more strict formatting rules for Fraction's. Maybe we should keep them, then this should be documented as now docs says: "If the format_spec format specification string ends with one of the presentation types 'e', 'E', 'f', 'F', 'g', 'G' or '%' then formatting follows the rules outlined for the float type in the Format Specification Mini-Language section." Adding more strict processing of width and precision for floats - probably will be too severe compatibility break.
I'm not crazy about the proposed fix. It's harmless, but it perpetuates the questionable support for meaningless extra leading zeros.
I agree we can't tighten float's formatting language, it's not worth breaking working code over.
As far as the docs, maybe we can change the float docs to disallow the redundant leading zeros and explain in a note that CPython does support those for backwards compatibility reasons, but only for float, not for Fraction? Or otherwise mention the difference in the Fraction docs.
I agree we can't tighten float's formatting language, it's not worth breaking working code over.
Maybe we can. I doubt someone rely on this "feature". Thus, deprecating this behavior will not affect too much code.
On another hand, it seems that more strict processing - slightly complicates parsing in C (get_integer() helper) and the grammar rules. So, maybe those zeros aren't a big problem: it's easy to support them in the fractions module, see pr. Support this - also not a big problem for external modules, see the mpmath issue.
maybe we can change the float docs to disallow the redundant leading zeros and explain in a note that CPython does support those for backwards compatibility reasons, but only for float, not for Fraction? Or otherwise mention the difference in the Fraction docs.
Formatting docs already (IMO) big and complex. I would prefer to avoid adding more special cases (there is already float vs Decimal differences, etc).
Maybe we could just document more strict requirements for width/precision and soft-deprecate old behavior in WhatsNew?
I think it is better to allow leading zeroes in "width" and "precision" in the Fraction format. The current specification allows leading zeroes, forbidding them would make it even more complex. AFAIK, leading zeroes are allowed in all other programming languages. So forbidding them would complicate the documentation, the implementation, will create unnecessary difference from other programming languages.
Yeah, so I am now against “fixing” this. It is easy for users to avoid the issue: don’t use redundant leading zeros.
If there is concern about the docs, let’s change the float docs to no longer promise support for redundant leading zeros. Then users who discover that they work can file issues about getting float fixed, to which we can respond that it is an accidental historic bug that they are supported, and we recommend not using that, it we are reluctant to fix it for fear of unnecessary breaking working code.
I suspect that the pattern for width and precision in _FLOAT_FORMAT_SPECIFICATION_MATCHER was just copied from the pattern for width in _GENERAL_FORMAT_SPECIFICATION_MATCHER, as it was the nearest place. There was reason for not supporting leading zeroes in _GENERAL_FORMAT_SPECIFICATION_MATCHER (the zeropad flag is not supported), but that does not apply for _FLOAT_FORMAT_SPECIFICATION_MATCHER.
Okay, this is much more of a quagmire than I had assumed. In the end I don't care enough about this issue. I will withdraw from the discussion and let you all decide (you might simply vote on it).
Activity
pythongh-130662: make Fraction's formatting more compatible wrt float's
skirpichev commentedon Feb 28, 2025
CC @mdickinson
BTW, I like more strict formatting rules for Fraction's. Maybe we should keep them, then this should be documented as now docs says: "If the format_spec format specification string ends with one of the presentation types 'e', 'E', 'f', 'F', 'g', 'G' or '%' then formatting follows the rules outlined for the float type in the Format Specification Mini-Language section." Adding more strict processing of width and precision for floats - probably will be too severe compatibility break.
PR is ready for review: #130663
gvanrossum commentedon Feb 28, 2025
I'm not crazy about the proposed fix. It's harmless, but it perpetuates the questionable support for meaningless extra leading zeros.
I agree we can't tighten float's formatting language, it's not worth breaking working code over.
As far as the docs, maybe we can change the float docs to disallow the redundant leading zeros and explain in a note that CPython does support those for backwards compatibility reasons, but only for float, not for Fraction? Or otherwise mention the difference in the Fraction docs.
skirpichev commentedon Feb 28, 2025
Maybe we can. I doubt someone rely on this "feature". Thus, deprecating this behavior will not affect too much code.
On another hand, it seems that more strict processing - slightly complicates parsing in C (get_integer() helper) and the grammar rules. So, maybe those zeros aren't a big problem: it's easy to support them in the fractions module, see pr. Support this - also not a big problem for external modules, see the mpmath issue.
Formatting docs already (IMO) big and complex. I would prefer to avoid adding more special cases (there is already float vs Decimal differences, etc).
Maybe we could just document more strict requirements for width/precision and soft-deprecate old behavior in WhatsNew?
serhiy-storchaka commentedon Feb 28, 2025
I think it is better to allow leading zeroes in "width" and "precision" in the Fraction format. The current specification allows leading zeroes, forbidding them would make it even more complex. AFAIK, leading zeroes are allowed in all other programming languages. So forbidding them would complicate the documentation, the implementation, will create unnecessary difference from other programming languages.
gvanrossum commentedon Feb 28, 2025
I note that Python numbers cannot have leading zeros (to avoid confusion with octal notation in C, C++ and in ancient Python).
But as the simplest (and fully backwards compatible) solution is to allow redundant leading zeros for Fraction, let's just do that.
For consistency I would also make the same change in
_GENERAL_FORMAT_SPECIFICATION_MATCHER.skirpichev commentedon Mar 1, 2025
Note, that in the given context we have only decimal notation.
BTW, Decimal's seems to be partially affected by this issue (width processing):
gvanrossum commentedon Mar 1, 2025
Yeah, so I am now against “fixing” this. It is easy for users to avoid the issue: don’t use redundant leading zeros.
If there is concern about the docs, let’s change the float docs to no longer promise support for redundant leading zeros. Then users who discover that they work can file issues about getting float fixed, to which we can respond that it is an accidental historic bug that they are supported, and we recommend not using that, it we are reluctant to fix it for fear of unnecessary breaking working code.
pythongh-130662: refine width/precision specification in format-spec
serhiy-storchaka commentedon Mar 1, 2025
I suspect that the pattern for width and precision in _FLOAT_FORMAT_SPECIFICATION_MATCHER was just copied from the pattern for width in _GENERAL_FORMAT_SPECIFICATION_MATCHER, as it was the nearest place. There was reason for not supporting leading zeroes in _GENERAL_FORMAT_SPECIFICATION_MATCHER (the zeropad flag is not supported), but that does not apply for _FLOAT_FORMAT_SPECIFICATION_MATCHER.
skirpichev commentedon Mar 1, 2025
Alternative pr: #130717 (docs only)
gvanrossum commentedon Mar 1, 2025
Okay, this is much more of a quagmire than I had assumed. In the end I don't care enough about this issue. I will withdraw from the discussion and let you all decide (you might simply vote on it).
22 remaining items
serhiy-storchaka commentedon Jun 2, 2025
Something related issues: accepting ASCII-only or non-ASCII digits and interpreting the single 0 as the zero-fill flag or the width.
skirpichev commentedon Jun 2, 2025
#135025 is related
bintime: Fix TimeDelta.__repr__ formatting errors with PyPy
nitypes: Enable testing with PyPy (#134)
pythongh-130662: Accept leading zeros in precision/width for Fraction…
pythongh-130662: Accept leading zeros in precision/width for Decimal'…
[3.14] gh-130662: Accept leading zeros in precision/width for Fractio…
[3.14] gh-130662: Accept leading zeros in precision/width for Decimal…
pythongh-130662: Accept leading zeros in precision/width for Fraction…
pythongh-130662: Accept leading zeros in precision/width for Decimal'…
pythongh-130662: Accept leading zeros in precision/width for Fraction…
pythongh-130662: Accept leading zeros in precision/width for Decimal'…
pythongh-130662: Accept leading zeros in precision/width for Fraction…
pythongh-130662: Accept leading zeros in precision/width for Decimal'…