-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PEP 737: PyUnicode_FromFormat(): Add %T format to format the type name of an object #111696
Comments
It was proposed adding
Since this revert change, the |
* Add "%T" and "%#T" formats to PyUnicode_FromFormat() * Add "T" and "#T" formats to type.__format__() * Add type.__fullyqualname__ attribute
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add "T" and "#T" formats to type.__format__(). * Add type.__fullyqualname__ read-only attribute.
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add "T" and "#T" formats to type.__format__(). * Add type.__fullyqualname__ read-only attribute.
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add "T" and "#T" formats to type.__format__(). * Add type.__fullyqualname__ read-only attribute.
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add "T" and "#T" formats to type.__format__(). * Add type.__fullyqualname__ read-only attribute.
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add "T" and "#T" formats to type.__format__(). * Add type.__fullyqualname__ read-only attribute.
Old related changes list from @serhiy-storchaka: |
Is there already an open issue with similar proposition? This idea was discussed years ago. If add a specialized format, we should provide options for all reasonable variants:
It would be nice to have also option that include Note that we need both options for the type itself and for the type of the argument, because we want to avoid calling
We can use the We can use the "size" modifiers This is a half of the issue. Other half, that stopped the previous attempt, is designing a similar features for printf-like formatting in Python (it is easy, we can just copy this format) and for new-style formatting and f-strings. Several changes can help with the latter (they are optional and can be combined):
|
Done by my PR.
My PR adds What is
Eric Smith was against it: https://mail.python.org/archives/list/python-dev@python.org/message/BMIW3FEB77OS7OB3YYUUDUBITPWLRG3U/
Isn't it already the case, module + qualname?
I disagree. Having too many options would lead to too many differences between functions. I would prefer to have limited choices. It's always possible to access to other formats by calling functions or reading type attributes, for more specialized cases.
I would prefer to have two choices:
Currently,
In Python, the short type name is usually used (
My PR uses # modifier to select between short type name and fully qualified type name. I don't think that a modifier is the right API to chose between
My PR proposes a different API in Python and in C:
Is it common to handle a type, instead of an instance? |
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add type.__fullyqualname__ read-only attribute.
* Add "%T" and "%#T" formats to PyUnicode_FromFormat(). * Add type.__fullyqualname__ read-only attribute.
Coarse statistics:
It seems like most Python functions raising error messages including a type name prefers the short name over the qualified name. |
I created a discussion for this issue: https://discuss.python.org/t/enhance-type-name-formatting-when-raising-an-exception-add-t-format-in-c-and-add-type-fullyqualname/38129 |
Such API will ease the conversion of C extensions using Argument Clinic to the limited C API (such as the def bad_argument(self, displayname: str, expected: str, *, limited_capi: bool, expected_literal: bool = True) -> str:
assert '"' not in expected
if limited_capi:
if expected_literal:
return (f'PyErr_Format(PyExc_TypeError, '
f'"{{{{name}}}}() {displayname} must be {expected}, not %.50s", '
f'{{argname}} == Py_None ? "None" : Py_TYPE({{argname}})->tp_name);')
else:
return (f'PyErr_Format(PyExc_TypeError, '
f'"{{{{name}}}}() {displayname} must be %.50s, not %.50s", '
f'"{expected}", '
f'{{argname}} == Py_None ? "None" : Py_TYPE({{argname}})->tp_name);')
else:
if expected_literal:
expected = f'"{expected}"'
if clinic is not None:
clinic.add_include('pycore_modsupport.h', '_PyArg_BadArgument()')
return f'_PyArg_BadArgument("{{{{name}}}}", "{displayname}", {expected}, {{argname}});' |
* Update modules: * enum * functools * optparse * pdb * xmlrcp.server * Update tests: * test_dataclasses * test_descrtut * test_cmd_line_script
As discussed previously, one option is to change |
Add PyType_GetFullyQualName() function.
Add PyType_GetFullyQualName() function.
Add PyType_GetFullyQualName() function.
Add PyType_GetFullyQualName() function with documentation and tests.
Add PyType_GetFullyQualName() function with documentation and tests.
Add PyType_GetFullyQualName() function with documentation and tests.
Rewrite tests on type names in Python, they were written in C.
Rewrite tests on type names in Python, they were written in C.
Rewrite tests on type names in Python, they were written in C.
Rewrite tests on type names in Python, they were written in C.
Author: Eric Snow <ericsnowcurrently@gmail.com>
Author: Eric Snow <ericsnowcurrently@gmail.com>
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
…python#116815) Rewrite tests on type names in Python, they were written in C.
…#116824) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
…python#116815) Rewrite tests on type names in Python, they were written in C.
…#116824) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* Fix implementation of %#T and %#N (they were implemented as %T# and %N#). * Restore tests removed in pythongh-111696.
…python#116815) Rewrite tests on type names in Python, they were written in C.
…#116824) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
It's common to format a type name by accessing
PyTypeObject.tp_name
member. Example:Problems:
PyTypeObject.tp_name
(type.__name__
) is less helpful thanPyHeapTypeObject.ht_qualname
(type.__qualname__
). I would prefer to display the qualified type name.PyTypeObject.tp_name
is a UTF-8 encoded string, it requires to decode the UTF-8 string at each call.PyTypeObject
members (tp_name
) from the public C API: issue C API: Investigate how the PyTypeObject members can be removed from the public C API #105970.PyString_FromFormat()
used a buffer with a fixed size, so the output string should be truncated to avoid overflow. But nowadays,PyUnicode_FromFormat()
allocates a buffer on the heap and is no longer limited to 200 characters. Truncated a type name can miss important information in the error message. I would prefer to not truncate the type name.I propose adding a
%T
format to PyUnicode_FromUnicode() to format the qualified name of an object type. For example, the example would become:In 2018, I already added
%T
format to PyUnicode_FromFormat(): issue GH-78776. See related python-dev discussion. The change was reverted.Linked PRs
The text was updated successfully, but these errors were encountered: