◐ Shell
clean mode source ↗

PEP 737: gh-111696: Add %T format to PyUnicode_FromFormat() by vstinner · Pull Request #111703 · python/cpython

@vstinner

I updated my PR:

  • Remove the change: Add T and "#T" formats to type.__format__().
  • Exchange %T and %#T format: %T is now the short type name, and %#T is the long type name.

@vstinner

I reduced this PR to the bare minimum, non controversial and most important: only add %T format just to add a type name (type.__name__).

Later, we can extend the API:

  • Add a %#T format to PyUnicode_FromFormat() if needed.
  • Add a type.__fullyqualname__ attribute.
  • Add T and #T formats to type.__format__().

Extended the API later is not incompatible with this change: they would be additions.

@vstinner

@serhiy-storchaka: Are you ok with this minor addition to PyUnicode_FromFormat()?

My work on the limited C API is blocked by this missing feature. As I wrote previously, the format can be extended later if someone wants to cover most cases, add an alternative format, etc.

@serhiy-storchaka

I would prefer to make the size modifier mandatory and use it as the format specifier in type.__format__. So you can write
PyUnicode_FromFormat("%zT", Py_TYPE(obj)) in C and f"{type(obj):z}" in Python.

It is also less ambiguous.

@vstinner

My previous attempt in 2018: #9122

@vstinner

Previous attempt, merged commit 886483e, which was reverted a few days later.

@vstinner

@vstinner vstinner changed the title gh-111696: Add %T format to PyUnicode_FromFormat() PEP 737: gh-111696: Add %T format to PyUnicode_FromFormat()

Dec 1, 2023

@vstinner

PEP 737 changed the API since this PR was created. I close this PR for now and will create a new one (or maybe reopen this PR) since PEP 737 will be approved.