Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Improved error message for metaclass incompatibility#134864
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
python-cla-botbot commentedMay 28, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This comment was marked as duplicate.
This comment was marked as duplicate.
1 similar comment
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We don't need this file, you can go ahead and delete it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for contributing :)
High level, I'm not sure I like this change. We typically try to keep error messages on the shorter side, and putting newlines in them hurts some use-cases (e.g., when people are writing error messages to log files). Would you mind creating an issue explaining some of the rationale here?
Also, please don't force push. We squash merge at the end, and it ends up just making the bot louder.
@@ -169,7 +169,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, | |||
/* meta is really a class, so check for a more derived | |||
metaclass, or possible metaclass conflicts: */ | |||
winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta, | |||
bases); | |||
bases,name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
bases,name); | |
bases,name); |
PyObject *base_meta_name = NULL; | ||
PyObject *format_result = NULL; | ||
declared_meta_name = PyObject_GetAttrString((PyObject *)winner, "__name__"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
PyObject_GetAttrString
sets an exception when it fails, so we might try and invoke the eval loop in the subsequent calls, which isn't safe. It needs to be in a pattern like this:
PyObject*first_attr=PyObject_GetAttrString(whatever,"first_attr");if (first_attr==NULL) {returnNULL;}PyObject*second_attr=PyObject_GetAttrString(whatever,"second_attr");if (second_attr==NULL) {Py_DECREF(first_attr);returnNULL;}/* ... */Py_DECREF(first_attr);Py_DECREF(second_attr);
base_meta_name = PyObject_GetAttrString((PyObject *)tmptype, "__name__"); | ||
if (declared_meta_name && base_class_name && base_meta_name) { | ||
format_result = PyUnicode_FromFormat( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You can usePyErr_Format
instead of manually creating the string.
/* Get resolve the name from 'spec' */ | ||
PyObject *name = NULL; | ||
if (spec && spec->name) { | ||
name = PyUnicode_FromString(spec->name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I don't think we have to manually convert this to a string.PyUnicode_FromFormat
(orPyErr_Format
) accept C strings with the%s
format specifier.
It occurs to me that not including the intended name of the class allows for a much simpler implementation of the improved error message. I will close this pull request, open an issue and then submit a new pull request not including the name of the class in the error message. |
Summary
This PR adds context to the metaclass conflict error message along with clear terminology.
Motivation
Class inheritance in OOP has well-established terminology conventions across languages, none of which should be used to describe the custom metaclass introduced by Python. This PR proposes to distinguish between them by the following convention:
This convention is then reflected in the proposed error message raised by metaclass conflicts.
Example
In the example below,
MetaFoo
andMetaBar
are both subclasses oftype
and neither is a (non-strict) subclass of the other.Foo
is a class derived fromMetaFoo
, andBar
attempts to be derived fromMetaBar
while also based onFoo
, revealing the incompatibility betweenMetaFoo
andMetaBar
:Behaviour Before
Previously, the resulting TypeError would show:
Behaviour After
This pull request attempts to improve with:
Conclusion
This PR hopes to contribute to the ongoing effort towards better error messages, whilst reinforcing clear and consistent terminology.