Skip to content
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

Resolution / determining of metaclass docs don't align with what really happens #99505

Open
robsdedude opened this issue Nov 15, 2022 · 2 comments · May be fixed by #123349
Open

Resolution / determining of metaclass docs don't align with what really happens #99505

robsdedude opened this issue Nov 15, 2022 · 2 comments · May be fixed by #123349
Labels
docs Documentation in the Doc dir

Comments

@robsdedude
Copy link

robsdedude commented Nov 15, 2022

Documentation

I'm not 100% sure if this is an issue with the docs or the interpreter. But it's evident that the docs don't match what the interpreter does.

The docs say here:

The appropriate metaclass for a class definition is determined as follows:

  • if no bases and no explicit metaclass are given, then :func:type is used;
  • if an explicit metaclass is given and it is not an instance of
    :func:type, then it is used directly as the metaclass;
  • if an instance of :func:type is given as the explicit metaclass, or
    bases are defined, then the most derived metaclass is used.

The most derived metaclass is selected from the explicitly specified
metaclass (if any) and the metaclasses (i.e. type(cls)) of all specified
base classes. The most derived metaclass is one which is a subtype of all
of these candidate metaclasses. If none of the candidate metaclasses meets
that criterion, then the class definition will fail with TypeError.

So according to that, the metaclass of D in this code

class M1(type): pass
class M2(type): pass
class Mx(M1, M2): pass

class A1(metaclass=M1): pass
class A2(A1): pass

class B1(metaclass=M2): pass

class C1(metaclass=Mx): pass

class D(A2, B1, C1): pass

should be Mx as it's the most derived of all candidates (M1, M2, Mx). But instead the cpython exitis with

    class D(A2, B1, C1): pass
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Note that class D(A2, C1, B1): pass works.

Some additional context:

As said above, I'm not even sure what the actually algorithm is that determines the implicit metaclass. From my experimentation, I assume it's:

  • If there's an explicit metaclass, it is checked to be a sub-class of the metaclass of each parent
  • If there's no explicit metaclass, the parents are checked from left to right:
    • When a new metaclass is encountered, it is checked to be a sub-class of the previous implicit metaclass and, if it is, set as the new implicit metaclass.

Linked PRs

@robsdedude robsdedude added the docs Documentation in the Doc dir label Nov 15, 2022
@robsdedude
Copy link
Author

If not a fix of the docs, I'd be really happy about somebody enlightening me about how the interpreter handles the resolution (or rather how it's supposed to handle it), as this would unblock my work over at mypy.

@robsdedude
Copy link
Author

Looking at the code here

cpython/Lib/types.py

Lines 130 to 145 in 2f20f5a

def _calculate_meta(meta, bases):
"""Calculate the most derived metaclass."""
winner = meta
for base in bases:
base_meta = type(base)
if issubclass(winner, base_meta):
continue
if issubclass(base_meta, winner):
winner = base_meta
continue
# else:
raise TypeError("metaclass conflict: "
"the metaclass of a derived class "
"must be a (non-strict) subclass "
"of the metaclasses of all its bases")
return winner

It seems that my assumption was right:

  • If there's no explicit metaclass, the parents are checked from left to right:
    • When a new metaclass is encountered, it is checked to be a sub-class of the previous implicit metaclass and, if it is, set as the new implicit metaclass.

robsdedude added a commit to robsdedude/cpython that referenced this issue Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant