Skip to content

PEP 737: gh-111696: Add %T format to PyUnicode_FromFormat() - #111703

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:fromformat_typename
Closed

PEP 737: gh-111696: Add %T format to PyUnicode_FromFormat()#111703
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:fromformat_typename

Conversation

@vstinner

@vstinnervstinner commented Nov 3, 2023

Copy link
Copy Markdown
Member

@vstinner

Copy link
Copy Markdown
MemberAuthor

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

Copy link
Copy Markdown
MemberAuthor

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

Copy link
Copy Markdown
MemberAuthor

@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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
MemberAuthor

My previous attempt in 2018: #9122

@vstinner

Copy link
Copy Markdown
MemberAuthor

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

@vstinner

Copy link
Copy Markdown
MemberAuthor

I wrote PEP 737 – Unify type name formatting for these changes: see the PEP discussion.

@vstinnervstinner 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

Copy link
Copy Markdown
MemberAuthor

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.

@vstinner
vstinner deleted the fromformat_typename branch December 20, 2023 10:56
@vstinner
vstinner restored the fromformat_typename branch March 14, 2024 14:03
@vstinner
vstinner deleted the fromformat_typename branch January 14, 2025 08:20
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@vstinner@serhiy-storchaka