Visitar URL original
gh-108946: correctly calculate most specific metaclass by Atry · Pull Request #108949 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260624064803/https://github.com/python/cpython/pull/108949
Skip to content
/ cpython Public

gh-108946: correctly calculate most specific metaclass#108949

Closed
Atry wants to merge 8 commits into
python:mainfrom
Atry:patch-2
Closed

gh-108946: correctly calculate most specific metaclass#108949
Atry wants to merge 8 commits into
python:mainfrom
Atry:patch-2

Conversation

@Atry

@Atry Atry commented Sep 5, 2023 •

Copy link
Copy Markdown

This PR fixes #108946 by finding the most specific single metaclass among all bases, instead of raising an exception in the middle of the _calculate_meta

@bedevere-bot

Copy link
Copy Markdown

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@Atry Atry marked this pull request as draft September 5, 2023 17:02
@Atry Atry marked this pull request as ready for review September 5, 2023 17:12
@ghost

ghost commented Sep 5, 2023 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

@Atry

Atry commented Oct 4, 2023

Copy link
Copy Markdown
Author

Could you review this PR @AlexWaygood?

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you review this PR @AlexWaygood?

Not sure why I'm getting pinged here specifically, but sure! :)

This PR doesn't appear to do what it says it's doing. I rebuilt Python with your PR branch, and exactly the same exception is raised that you reported in #108946:

Running Debug|x64 interpreter...
Python 3.13.0a0 (heads/patch-2:414a44dd80, Oct  4 2023, 16:11:24) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Meta1(type): pass
...
>>> class Meta2(type): pass
...
>>> class Meta3(Meta1, Meta2): pass
...
>>> class Class1(metaclass=Meta1): pass
...
>>> class Class2(metaclass=Meta2): pass
...
>>> class Class3(metaclass=Meta3): pass
...
>>> class Conflict(Class1, Class2, Class3): pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

And I'd expect that to be the case. The exception you reported in #108946 isn't raised from a function in types.py -- you can tell that from the fact that types.py doesn't appear anywhere in the traceback. It's raised here:

cpython/Objects/typeobject.c

Lines 3119 to 3125 in bf4bc36

/* else: */
PyErr_SetString(PyExc_TypeError,
"metaclass conflict: "
"the metaclass of a derived class "
"must be a (non-strict) subclass "
"of the metaclasses of all its bases");
return NULL;

The function you're tweaking in types.py is a helper function for the types.prepare_class function, which is a pure-Python emulation of the logic that takes place in Objects/typeobject.c. It's not actually involved in creating classes through the class statement; it's just a standard-library function to aid people who want to dynamically create classes for whatever reason.

Even if this PR worked correctly, we'd be unable to merge it unless you wrote some tests for it :) Tests would also have been helpful for you, as they would have alerted you to the fact that your PR wasn't working in the way you'd intended.

N.B. If you don't know how to build Python from source or run tests, there's a lot of great information on that in the devguide! https://devguide.python.org

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 13, 2026
@AlexWaygood

Copy link
Copy Markdown
Member

Closing since it's been 3 years and my review comments were not addressed. Feel free to open a new PR if you're still interested in this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected metaclass conflict

3 participants