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
__init_subclass__ should be called in __init__ #86941
Comments
PEP-487 introduced __init_subclass__ and __set_name__, and both of those were wins for the common cases of metaclass usage. Unfortunately, the implementation of PEP-487 with regards to __init_subclass__ has made the writing of correct metaclasses significantly harder, if not impossible. The cause is that when a metaclass calls type.__new__ to actually create the class, type.__new__ calls the __init_subclass__ methods of the new class' parents, passing it the newly created, but incomplete, class. In code:
As you can deduce, when the parent init_subclass is called with the new class, For Enum, this means that __init_subclass__ doesn't have access to the new Enum's members (they haven't beet added yet). For ABC, this means that __init_subclass__ doesn't have access to __abstract_methods__ (it hasn't been created yet). Because Enum is pure Python code I was able to work around it:
I have not been able to work around the problem for ABC. The solution would seem to be to move the calls to __init_subclass__ and __set_names__ to type.__init__. |
My understanding of using keywords in class headers class MyClass(SomeBaseClass, setting='maybe'):
... is that the keywords would get passed into the super classes However, in the cases of
that wasn't happening -- until the initial patch of moving the calls from An |
Whoa, you start a discussion on python-dev and another on bpo? That sounds a bit hasty. |
Guido, I just wanted to get it all in place while it was fresh in my mind. Actual code tends to make a discussion easier. I'll make sure and transcribe any relevant discussion from python-dev to here. |
I see this as a backwards incompatible change and it's not worth doing for what seems to me a very minor benefit. |
Nick Coghlan made the observation that Tracking in bpo-42901. |
Thanks, that's great! And thanks, Nick! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: