-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
| 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) {
return NULL;
}
PyObject *second_attr = PyObject_GetAttrString(whatever, "second_attr");
if (second_attr == NULL) {
Py_DECREF(first_attr);
return NULL;
}
/* ... */
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 use PyErr_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 (or PyErr_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,
MetaFooandMetaBarare both subclasses oftypeand neither is a (non-strict) subclass of the other.Foois a class derived fromMetaFoo, andBarattempts to be derived fromMetaBarwhile also based onFoo, revealing the incompatibility betweenMetaFooandMetaBar: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.