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
As you can see, trying to format a complex with .d and .f produces two outputs with the same meaning but different expressions.
Expected: same expressions for same meaning.
"must be real number" is more general, it is produced in PyFloat_AsDouble(), i.e. it is used in many other cases when a real number is expected and will be implicitly converted to floating-point.
"%d format: a real number is required" is more specific, it is produced in str.__mod__. It contains more information and is similar to error messages for %o and %x which require an integer. So this message is preferable.
We can also use opportunity to improve other error messages.
#142081 does much more than bringing the error message for floating point formatting in consistency with others. It adds the number or the name of the argument in the error messages related to arguments and the position of the start of the format unit in the error messages related to the format string, and raise more specific error messages containing more information.
For example (this is only a small part of improved errors):
>>> '%.*f'% (1, '2')
TypeError: format argument 2: a real number is required for format %f, not str
>>> '%.*f'% ('1', 2)
TypeError: format argument 1: * requires int, not str
>>> '%(x)f'% {'x': '1'}
TypeError: format argument 'x': a real number is required for format %f, not str
>>> '%.*f'% (1, 2, 3)
TypeError: not all arguments converted during string formatting (required 2, got 3)
>>> '%.*f'% (999999999999999999999999999999, 2)
OverflowError: format argument 1: too big for precision
>>> '%.999999999999999999999999999999f'%2
ValueError: precision too big at position 0
Activity
WFLing-seaer commentedon Nov 28, 2025
I'm quite confident that this issue hasn't been submitted yet; at least I haven't found any related issue in the Issues page
serhiy-storchaka commentedon Nov 28, 2025
"must be real number" is more general, it is produced in
PyFloat_AsDouble(), i.e. it is used in many other cases when a real number is expected and will be implicitly converted to floating-point."%d format: a real number is required" is more specific, it is produced in
str.__mod__. It contains more information and is similar to error messages for %o and %x which require an integer. So this message is preferable.We can also use opportunity to improve other error messages.
[-]Inconsistent TypeError hints[/-][+]Improve TypeError hints for `%`-formats[/+]pythongh-142037: Improve error messages for printf-style formatting
serhiy-storchaka commentedon Nov 29, 2025
#142081 does much more than bringing the error message for floating point formatting in consistency with others. It adds the number or the name of the argument in the error messages related to arguments and the position of the start of the format unit in the error messages related to the format string, and raise more specific error messages containing more information.
For example (this is only a small part of improved errors):
gh-142037: Improve error messages for printf-style formatting (GH-142081
encukou commentedon Jan 26, 2026
The PR introduced a reference leak.
Fix a refleak introduced in pythongh-142037
serhiy-storchaka commentedon Jan 26, 2026
Thank you, @encukou. Should be fixed in #144256.
gh-142037: Fix a refleak introduced in GH-142081 (GH-144256)
pythongh-142037: Improve error messages for printf-style formatting (p…
pythongh-142037: Fix a refleak introduced in pythonGH-142081 (pythonG…