Skip to content

Improve TypeError hints for %-formats #142037

@WFLing-seaer

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

Metadata

Metadata

Labels

interpreter-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