[docs] [issue28437] Documentation for handling of non-type metaclass hints is unclear

Neil Girdhar report at bugs.python.org
Sun Oct 16 03:22:48 EDT 2016


Neil Girdhar added the comment:

Thanks for taking the time to explain, but it's still not working for me:

from types import new_class


class MyMetaclass(type):
    pass

class OtherMetaclass(type):
    pass

def metaclass_callable(name, bases, namespace):
    return new_class(name, bases, dict(metaclass=OtherMetaclass))

class MyClass(metaclass=MyMetaclass):
    pass

try:
    class MyDerived(MyClass, metaclass=metaclass_callable):
        pass
except:
    print("Gotcha!")


try:
    MyDerived = new_class("MyDerived", (MyClass,), dict(metaclass=metaclass_callable))
except:
    print("Gotcha again!")

So my questions are:

1. Why shouldn't Lib/types:new_class behave in exactly the same way as declaring a class using "class…" notation?

2. What's the point of checking if the metaclass is an instance of type?  It seems to me that in Python 2, non-type metaclasses did not have to be the "most derived class" (that's what the documentation seems to suggest with the second rule).  However, we no longer accept that in CPython 3 — neither in the Lib/types, nor in a regular declaration.  In fact, the exception is:

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

So why not just check that unconditionally in Lib/types.py?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28437>
_______________________________________


More information about the docs mailing list