-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PEP 786: Precision and Modulo-Precision Flag format specifiers for integer fields #4416
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't read the content of the PEP yet, but a few notes:
|
||
* Format all lines to ~80 characters. I've left this formatting until we're happy with the contents. | ||
* RFC 2119 Style Specification? After all is said and done here. | ||
* Add Sergey and Raymond to the authors field, or is Thanks enough? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps easiest to ask them which they'd prefer?
It's better to give the formatspec one canonical ordering than permit an overly liberal rearrangeability. If commutativity were added, then as well as the messy description required for the docs for the particular case of `int` data, two people could write two different format spec that result in the same output and not realise it or agree, because they've written different things, leading to confusion etc.
peps/pep-0786.rst @ncoghlan | ||
# ... | ||
peps/pep-0787.rst @ncoghlan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this gap:
peps/pep-0786.rst @ncoghlan | |
# ... | |
peps/pep-0787.rst @ncoghlan | |
peps/pep-0786.rst @ncoghlan | |
peps/pep-0787.rst @ncoghlan |
@@ -0,0 +1,355 @@ | |||
PEP: 786 | |||
Title: Precision and Modulo-Precision Flag format specifiers for integer fields |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistent case: https://devguide.python.org/documentation/style-guide/#capitalization
Title: Precision and Modulo-Precision Flag format specifiers for integer fields | |
Title: Precision and modulo-precision flag format specifiers for integer fields |
Rationale | ||
========= | ||
|
||
When string formatting integers in binary octal and hexadecimal, one often desires the resulting string to contain a guaranteed minimum number of digits. For unsigned integers of known machine-width bounds (eg 8 bit bytes) this often also ends up the exact resulting number of digits. This has previously been implemented in the old-style ``%`` formatting using the ``.`` "precision" format specifier, closely related to that of the C programming language. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid Latin abbreviations: https://devguide.python.org/documentation/style-guide/#use-simple-language
When string formatting integers in binary octal and hexadecimal, one often desires the resulting string to contain a guaranteed minimum number of digits. For unsigned integers of known machine-width bounds (eg 8 bit bytes) this often also ends up the exact resulting number of digits. This has previously been implemented in the old-style ``%`` formatting using the ``.`` "precision" format specifier, closely related to that of the C programming language. | |
When string formatting integers in binary octal and hexadecimal, one often desires the resulting string to contain a guaranteed minimum number of digits. For unsigned integers of known machine-width bounds (for example, 8-bit bytes) this often also ends up the exact resulting number of digits. This has previously been implemented in the old-style ``%`` formatting using the ``.`` "precision" format specifier, closely related to that of the C programming language. |
>>> "0o%.3o" % 18 | ||
'0o022' # three octal digits, ideal for displaying a umask or file permissions | ||
|
||
When :pep:`3101` new-style formatting was first introduced, used in ``str.format`` and ``f``\ strings, the `format specification <formatspec_>`_ was simple enough that the behavior of "precision" could be trivially emulated with the ``width`` format specifier. Precision therefore was left unimplemented and forbidden for ``int`` fields. However, as time has progressed and new format specifiers have been added, whose interactions with ``width`` noticeably diverge its behavior away from emulating precision, the readmission of precision as its own format specifier, ``.``, is sufficiently warranted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When :pep:`3101` new-style formatting was first introduced, used in ``str.format`` and ``f``\ strings, the `format specification <formatspec_>`_ was simple enough that the behavior of "precision" could be trivially emulated with the ``width`` format specifier. Precision therefore was left unimplemented and forbidden for ``int`` fields. However, as time has progressed and new format specifiers have been added, whose interactions with ``width`` noticeably diverge its behavior away from emulating precision, the readmission of precision as its own format specifier, ``.``, is sufficiently warranted. | |
When :pep:`3101` new-style formatting was first introduced, used in ``str.format`` and f-strings, the `format specification <formatspec_>`_ was simple enough that the behavior of "precision" could be trivially emulated with the ``width`` format specifier. Precision therefore was left unimplemented and forbidden for ``int`` fields. However, as time has progressed and new format specifiers have been added, whose interactions with ``width`` noticeably diverge its behavior away from emulating precision, the readmission of precision as its own format specifier, ``.``, is sufficiently warranted. |
>>> f"{x:#08b}" | ||
'0b001100' # we wanted 8 bits, not 6 :( | ||
|
||
One could attempt to argue that since the length of a prefix is known to always be 2, it can be accounted for manually by adding 2 to the desired number of digits. Consider however the following demonstrations of why this is a bad idea: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One could attempt to argue that since the length of a prefix is known to always be 2, it can be accounted for manually by adding 2 to the desired number of digits. Consider however the following demonstrations of why this is a bad idea: | |
One could attempt to argue that since the length of a prefix is known to always be two, it can be accounted for manually by adding two to the desired number of digits. Consider however the following demonstrations of why this is a bad idea: |
>>> f"{y:z#.8b}" | ||
'0b[...1]11111111' | ||
|
||
This may have been useful to educate beginner users on how bitwise binary operations work, for example showing how ``-1 & x`` is always trivially equal to ``x``, or how the binary representation of the negation of a number can be obtained by adding one to its bitwise complement: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may have been useful to educate beginner users on how bitwise binary operations work, for example showing how ``-1 & x`` is always trivially equal to ``x``, or how the binary representation of the negation of a number can be obtained by adding one to its bitwise complement: | |
This may have been useful to educate beginners on how bitwise binary operations work, for example showing how ``-1 & x`` is always trivially equal to ``x``, or how the binary representation of the negation of a number can be obtained by adding one to its bitwise complement: |
|
||
Verdict: | ||
|
||
- Whilst graphically attractive, ``!`` would add too much more clutter to the format specification for a purpose that can be achieved by overloading the preexisting ``z`` flag. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"too much" or "much more" instead of "too much more"?
- Whilst graphically attractive, ``!`` would add too much more clutter to the format specification for a purpose that can be achieved by overloading the preexisting ``z`` flag. | |
- Whilst graphically attractive, ``!`` would add too much clutter to the format specification for a purpose that can be achieved by overloading the preexisting ``z`` flag. |
- Whilst graphically attractive, ``!`` would add too much more clutter to the format specification for a purpose that can be achieved by overloading the preexisting ``z`` flag. | |
- Whilst graphically attractive, ``!`` would add much more clutter to the format specification for a purpose that can be achieved by overloading the preexisting ``z`` flag. |
Backwards Compatibility | ||
======================= | ||
|
||
To quote :pep:`682` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To quote :pep:`682` | |
To quote :pep:`682`: |
|
||
To quote :pep:`682` | ||
|
||
The new formatting behavior is opt-in, so numerical formatting of existing programs will not be affected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new formatting behavior is opt-in, so numerical formatting of existing programs will not be affected | |
The new formatting behavior is opt-in, so numerical formatting of existing programs will not be affected. |
|
||
The new formatting behavior is opt-in, so numerical formatting of existing programs will not be affected | ||
|
||
unless someone out there is specifically relying upon ``.`` raising a ``ValueError`` for integers as it currently does, but to quote :pep:`475` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless someone out there is specifically relying upon ``.`` raising a ``ValueError`` for integers as it currently does, but to quote :pep:`475` | |
unless someone out there is specifically relying upon ``.`` raising a ``ValueError`` for integers as it currently does, but to quote :pep:`475`: |
New rebased PR with the correct branch name to avoid confusion (786 not 791).
Thank you Alyssa for sponsoring this!
There is a
TODO
section in the PEP that shall perish as the PEP is tweaked before acceptingRelevant discussions, issues, PRs linked
Basic requirements (all PEP Types)
pep-NNNN.rst
), PR title (PEP 123: <Title of PEP>
) andPEP
headerAuthor
orSponsor
, and formally confirmed their approval: @ncoghlanAuthor
,Status
(Draft
),Type
andCreated
headers filled out correctlyPEP-Delegate
,Topic
,Requires
andReplaces
headers completed if appropriate: Is a PEP-Delegate required?.github/CODEOWNERS
for the PEPStandards Track requirements
Python-Version
set to valid (pre-beta) future Python version, if relevant: pendingDiscussions-To
andPost-History
📚 Documentation preview 📚: https://pep-previews--4416.org.readthedocs.build/pep-0786/