-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
The Python implementation of Decimal does not support the "N" format #89902
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
Comments
The C implementation supports both formats "n" and "N". The Python implementation only supports format "n". >>> from decimal import Decimal
>>> format(Decimal('1e100'), 'n')
'1e+100'
>>> format(Decimal('1e100'), 'N')
'1E+100'
>>> from _pydecimal import Decimal
>>> format(Decimal('1e100'), 'n')
'1e+100'
>>> format(Decimal('1e100'), 'N')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/_pydecimal.py", line 3766, in __format__
spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/serhiy/py/cpython/Lib/_pydecimal.py", line 6194, in _parse_format_specifier
raise ValueError("Invalid format specifier: " + format_spec)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Invalid format specifier: N |
Interesting. I think the behaviour of the Python implementation behaviour is actually more correct here: neither >>> format(123, 'n')
'123'
>>> format(123, 'N')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'N' for object of type 'int' |
I don’t think “N” should be supported. It was never the intention to have it work. |
Eric, Serhiy: do you have opinions on the right way forward? Here are 6 options, on a spectrum of increasing level of acceptance of "N". -2. Remove "N" support for cdecimal right now (i.e., for Python 3.11), on the basis that there's no need for deprecation warnings, because it was never officially a feature.
|
I could be persuaded for any of options -1, 1 and 2. I don't much like option 0. |
I support deprecating "N". |
I'd support -1. |
I have no strong opinion. I found the discrepancy when reviewed one of Mark's PRs. I was surprised because I did not know that "N" is supported. On other hand, there are reasons for supporting upper-case variant of "n" if we support upper-case variants of "e", "g" and "x" (formats which produce output containing letters). There is an alternative solution of the problem solved by upper-case variations of formats: add a special converter for converting a string to upper case, so for example f'{x:12.8E}' could be written as f'{x:12.8e!u}'. I prefer -1 or 2. If we choose deprecation I prefer adding an upper-case converter. |
While I'd prefer -1 overall, I would also prefer 3 over 2. If we were adding the feature from scratch, we wouldn't have decimal differ from int, float, and complex. And I'm not in favor of an uppercase converter, no matter what we do here. The other converters work like: f'{obj!s}' -> format(str(obj)) but the proposed !u would be f'{obj!u}' -> format(obj).upper() That is, it operates on the result of __format__, not its input. |
You are right about an uppercase converter. I forget this. I withdraw this proposition. It seems that even if we add support of "N" to all numeric formatting it will not cover all use cases. "n" is a locale specific variant of "g", but other formats could have locale specific variants too. In C all numeric formatting is locale specific. Maybe add a flag which makes any numeric formatting locale specific? Then "n" will became obsolete. |
It was not documented and only supported in the C implementation.
…-110508) It was not documented and only supported in the C implementation.
We missed terms for 3.11 and 3.12, but finally it has been deprecated in 3.13. It can be removed in 3.15 or like. |
…al (pythonGH-110508) It was not documented and only supported in the C implementation.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: