Skip to content

Improve TypeError hints for %-formats #142037

Closed

Description

@WFLing-seaer

Bug report

Bug description:

>>> "%.f"%(1+1j)
Traceback (most recent call last):
  File "<python-input-27>", line 1, in <module>
    "%.f"%(1+1j)
    ~~~~~^^~~~~~
TypeError: must be real number, not complex
>>> "%.d"%(1+1j)
Traceback (most recent call last):
  File "<python-input-28>", line 1, in <module>
    "%.d"%(1+1j)
    ~~~~~^^~~~~~
TypeError: %d format: a real number is required, not complex

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.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Activity

  1. WFLing-seaer commented on Nov 28, 2025

    @WFLing-seaer
    Author

    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

  2. serhiy-storchaka commented on Nov 28, 2025

    @serhiy-storchaka
    Member

    "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.

  3. added
    type-featureA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Nov 28, 2025
  4. changed the title [-]Inconsistent TypeError hints[/-] [+]Improve TypeError hints for `%`-formats[/+] on Nov 28, 2025
  5. added a commit that references this issue on Nov 29, 2025
  6. serhiy-storchaka commented on Nov 29, 2025

    @serhiy-storchaka
    Member

    #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
  7. added a commit that references this issue on Jan 24, 2026
  8. encukou commented on Jan 26, 2026

    @encukou
    Member

    The PR introduced a reference leak.

  9. serhiy-storchaka commented on Jan 26, 2026

    @serhiy-storchaka
    Member

    Thank you, @encukou. Should be fixed in #144256.

  10. added a commit that references this issue on Jan 26, 2026
  11. added 2 commits that reference this issue on Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions