Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
AsgerJon wants to merge3 commits intopython:mainfromAsgerJon:main

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 classis derived from itsmetaclass
  • A classis based on itsbase classes

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:

classMetaFoo(type):passclassMetaBar(type):passclassFoo(metaclass=MetaFoo):passclassBar(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-botbot commentedMay 28, 2025
edited
Loading

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.

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

@ZeroIntensityZeroIntensity 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);

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__");

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(

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);

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.

@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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ZeroIntensityZeroIntensityZeroIntensity requested changes

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@AsgerJon@ZeroIntensity

[8]ページ先頭

©2009-2025 Movatter.jp