When we have a pending label, usually this means that we still don't know whether this is triaged. AFAICT, what the OP described as an issue was not an issue. IMO, there is nothing to do (unless @skirpichev had something precise in mind?)
Documentation for printf-style formatting lacks description of rules to format non-builtin types. @picnixz, if such rules are obvious for you - perhaps there is no issue.
Oh if for non-built-in types, we don't document %-format, then yes I'm fine with documenting them. But I don't really where we would put them. I think we can amend the printf-style sections for str and bytes to say something like:
For non built-in types, for instance, %d calls the __int__ method of the value to convert to get its int representation to format.
The above sentence is clearly not perfect and I would rather have something equivalent and better but that convenes this intent (that is, we have an implicit conversion).
changed the title [-]Add a description about how Decimal behaves differently with __format__ and % formatting[/-][+]Improve `printf`-style formatting for non-built-in types[/+]on Nov 30, 2025
changed the title [-]Improve `printf`-style formatting for non-built-in types[/-][+]Improve `printf`-style formatting docs for non-built-in types[/+]on Nov 30, 2025
For %o & co, it's actually documented in object.__index__ but it's honestly better if we were to indicate this in the printf-style section. So I'm in favor of having something that really explains the logic at the same place (I guess the entire thing is documented but probably in differnt places which makes it very hard to search)
I misunderstood that there is only one document about formatting,
and that the basic functionalities of the format-specification mini-language are also applicable to % formatting,
except for some additional functions.
Called to implement operator.index(), and whenever Python needs to losslessly convert the numeric object to an integer object (such as in slicing, or in the built-in bin(), hex() and oct() functions).
But you are right, in sense that we could reference to this logic in formatting docs.
I don't think this is issue is very newcomer friendly actually. It requires quite a deep knowledge in the subtle terms as well as knowledge in the implementation. I would suggest either @skirpichev or me to document this but I don't have much time.
It's not explicitly stated, but I would have assumed that mention to bin/hex/oct combined with "whenever Python needs to losslessly convert the numeric object to an integer object" actually covered this. Actually the oct docs say:
Convert an integer number to an octal string prefixed with “0o”.
[...]
If you want to convert an integer number to an octal string either with the prefix “0o” or not, you can use either of the following ways.
I believe there should at least be a link to #printf-style-string-formatting in #format-specification-mini-language.
Or maybe replace the literal %-formatting in #format-examples by a link
This section contains examples of the str.format() syntax and comparison with the old %-formatting.
I guess it doesn't hurt to also backreference %-style in format-style with a single sentence at the end of the first paragraph. "For old-style formatting, see [...]".
Side story. Should we promote old-style formatting in usual examples? I don't think so.
Personally, I use old-style formatting because it's easier to me to read. There is a real visual benefit IMO where I can more easily parse % tokens rather than {varname}. So while I understand that people don't always like old-style formatting I do use it myself if needed (it's especially useful for double-formatting; (fmt % args).format(after), which happens when you want to pre-compute the width of your field (it's possible to do it with f-strings but it's unreadable to me honestly).
Hi, I attempted a PR but realized after review that I may have misunderstood the scope of this issue.
The issue title mentions "printf-style formatting" (% operator) but the linked doc section is about the Format Specification Mini-Language (format / f-strings / .format()).
Activity
skirpichev commented on Nov 30, 2025
It's just not true. Only when precision is omitted in f/e/g formats. This is documented.
This is not specific to Decimal's at all:
Old-style formatting is documented here: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
Maybe rules are obvious, yet they aren't documented. N.B.: implementation is in the Objects/unicode_format.c
Yashp002 commented on Nov 30, 2025
I'd like to work on this. I'll add clarification about Decimal's formatting behavior to the format specification docs.
picnixz commented on Nov 30, 2025
When we have a pending label, usually this means that we still don't know whether this is triaged. AFAICT, what the OP described as an issue was not an issue. IMO, there is nothing to do (unless @skirpichev had something precise in mind?)
skirpichev commented on Nov 30, 2025
Sorry, perhaps I wasn't clear.
Documentation for printf-style formatting lacks description of rules to format non-builtin types. @picnixz, if such rules are obvious for you - perhaps there is no issue.
picnixz commented on Nov 30, 2025
Oh if for non-built-in types, we don't document %-format, then yes I'm fine with documenting them. But I don't really where we would put them. I think we can amend the printf-style sections for str and bytes to say something like:
The above sentence is clearly not perfect and I would rather have something equivalent and better but that convenes this intent (that is, we have an implicit conversion).
skirpichev commented on Nov 30, 2025
We have a dedicated section for %-formatting: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
Yes, something like that. Though, logic is much more complex, e.g. for integer formatting types:
cpython/Objects/unicode_format.c
Lines 297 to 317 in 229ed3d
(For floats it's more close to float(obj) for non-string obj's.)
picnixz commented on Nov 30, 2025
For %o & co, it's actually documented in object.__index__ but it's honestly better if we were to indicate this in the printf-style section. So I'm in favor of having something that really explains the logic at the same place (I guess the entire thing is documented but probably in differnt places which makes it very hard to search)
1nftf commented on Nov 30, 2025
I discovered the formatting difference while investigating #142019
I found this document first:
https://docs.python.org/3/library/string.html#format-specification-mini-language.
When I tried to find another document, I searched for '% format'
and '% formatting' but found nothing related to
https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting.
I misunderstood that there is only one document about formatting,
and that the basic functionalities of the format-specification mini-language are also applicable to % formatting,
except for some additional functions.
Perhaps a note could be added at the beginning of
https://docs.python.org/3/library/string.html#format-specification-mini-language
stating that this is not suitable for % formatting, along with a link back to
https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
1nftf commented on Nov 30, 2025
I'm focusing on #142019, so I can't pay much attention to this currently.
skirpichev commented on Nov 30, 2025
Not, unless I misread it:
But you are right, in sense that we could reference to this logic in formatting docs.
I don't think we need this.
1nftf commented on Nov 30, 2025
Why? Because this old formatting syntax is not encouraged to be used?
picnixz commented on Nov 30, 2025
Format specifications and printf-style are different. One is for fmt % value, the other is for format() or str.format.
picnixz commented on Nov 30, 2025
I don't think this is issue is very newcomer friendly actually. It requires quite a deep knowledge in the subtle terms as well as knowledge in the implementation. I would suggest either @skirpichev or me to document this but I don't have much time.
It's not explicitly stated, but I would have assumed that mention to bin/hex/oct combined with "whenever Python needs to losslessly convert the numeric object to an integer object" actually covered this. Actually the oct docs say:
So I guess we could say it's documented, though maybe a bit hard to find.
skirpichev commented on Nov 30, 2025
Because we can't clutter docs with notes each time someone misread them in some way.
I think it's a good opportunity to learn. There is an open pr, lets see if @Yashp002 will read discussion thread and address review comments.
If not, I'll try to finish this.
BTW,
Side story. Should we promote old-style formatting in usual examples? I don't think so.
1nftf commented on Nov 30, 2025
I believe there should at least be a link to #printf-style-string-formatting in #format-specification-mini-language.
Or maybe replace the literal %-formatting in #format-examples by a link
I didn't realize #printf-style-string-formatting exists, until @skirpichev mentioned it.
The built-in document search engine is basically useless when it comes to searching for symbols and multiple keywords.
picnixz commented on Nov 30, 2025
I guess it doesn't hurt to also backreference %-style in format-style with a single sentence at the end of the first paragraph. "For old-style formatting, see [...]".
Personally, I use old-style formatting because it's easier to me to read. There is a real visual benefit IMO where I can more easily parse % tokens rather than {varname}. So while I understand that people don't always like old-style formatting I do use it myself if needed (it's especially useful for double-formatting; (fmt % args).format(after), which happens when you want to pre-compute the width of your field (it's possible to do it with f-strings but it's unreadable to me honestly).
skirpichev commented on Nov 30, 2025
Seems fine.
It depends on how you read the docs. Starting from the Tutorial here you will quickly navigate to both pages.
Yashp002 commented on Dec 1, 2025
Hi, I attempted a PR but realized after review that I may have misunderstood the scope of this issue.
The issue title mentions "printf-style formatting" (% operator) but the linked doc section is about the Format Specification Mini-Language (format / f-strings / .format()).
Could you clarify:
pythongh-142108: document conversion rules for printf-style formatting