Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

AsgerJon
Copy link

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:

  • A class is derived from its metaclass
  • A class is based on its base classes

This convention is then reflected in the proposed error message raised by metaclass conflicts.

Example

In the example below, MetaFoo and MetaBar are both subclasses of type and neither is a (non-strict) subclass of the other. Foo is a class derived from MetaFoo, and Bar attempts to be derived from MetaBar while also based on Foo, revealing the incompatibility between MetaFoo and MetaBar:

class MetaFoo(type):  pass

class MetaBar(type): pass

class Foo(metaclass=MetaFoo):  pass

class Bar(Foo, metaclass=MetaBar):  pass  # raises

Behaviour Before

Previously, the resulting TypeError would show:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Behaviour After

This pull request attempts to improve with:

Metaclass conflict while defining class: 'Foo'
- Declared metaclass: 'MetaFoo'
- Incompatible base class: 'Bar'
- That base is derived from metaclass: 'MetaBar'
All base classes must be based on classes derived from the same metaclass or a subclass thereof.

Conclusion

This PR hopes to contribute to the ongoing effort towards better error messages, whilst reinforcing clear and consistent terminology.

@python-cla-bot
Copy link

python-cla-bot bot commented May 28, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

This comment was marked as duplicate.

1 similar comment
@bedevere-app

This comment was marked as duplicate.

@bedevere-app

This comment was marked as duplicate.

@bedevere-app

This comment was marked as duplicate.

Copy link
Member

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.

Copy link
Member

@ZeroIntensity ZeroIntensity left a 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bases,name);
bases, name);

PyObject *base_meta_name = NULL;
PyObject *format_result = NULL;

declared_meta_name = PyObject_GetAttrString((PyObject *)winner, "__name__");
Copy link
Member

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(
Copy link
Member

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);
Copy link
Member

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.

@AsgerJon
Copy link
Author

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.

Sign up for free to 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