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

__init_subclass__ should be called in __init__ #86941

Closed
ethanfurman opened this issue Dec 29, 2020 · 7 comments
Closed

__init_subclass__ should be called in __init__ #86941

ethanfurman opened this issue Dec 29, 2020 · 7 comments
Assignees
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@ethanfurman
Copy link
Member

BPO 42775
Nosy @gvanrossum, @rhettinger, @ncoghlan, @ethanfurman, @serhiy-storchaka, @carltongibson
PRs
  • bpo-42775: call init_subclass and set_name from type.__init__ #23986
  • Superseder
  • bpo-42901: [Enum] move member creation to set_name in order to support init_subclass
  • 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:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2021-01-12.01:29:46.590>
    created_at = <Date 2020-12-29.03:06:49.589>
    labels = ['interpreter-core', 'type-bug', '3.10']
    title = '__init_subclass__ should be called in __init__'
    updated_at = <Date 2021-01-26.15:07:32.117>
    user = 'https://github.com/ethanfurman'

    bugs.python.org fields:

    activity = <Date 2021-01-26.15:07:32.117>
    actor = 'carltongibson'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2021-01-12.01:29:46.590>
    closer = 'ethan.furman'
    components = ['Interpreter Core']
    creation = <Date 2020-12-29.03:06:49.589>
    creator = 'ethan.furman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42775
    keywords = []
    message_count = 7.0
    messages = ['383946', '383948', '383960', '383962', '384020', '384876', '384877']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'rhettinger', 'ncoghlan', 'stutzbach', 'ethan.furman', 'serhiy.storchaka', 'carltongibson']
    pr_nums = ['23986']
    priority = 'high'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = '42901'
    type = 'behavior'
    url = 'https://bugs.python.org/issue42775'
    versions = ['Python 3.10']

    @ethanfurman
    Copy link
    Member Author

    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:

    class Meta(type):
        #
        def __new__(mcls, name, bases, namespace, **kwds):
            # create new class, which will call __init_subclass__ and __set_name__
            new_class = type.__new__(mcls, name, bases, namespace, **kwds)
            # finish setting up class
            new_class.some_attr = 9
    

    As you can deduce, when the parent init_subclass is called with the new class, some_attr has not been added yet -- the new class is incomplete.

    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:

    • remove new __init_subclass__ (if it exists)
    • insert dummy class with a no-op __init_subclass__
    • call type.__new__
    • save any actual __init_subclass__
    • add back any new __init_subclass__
    • rewrite the new class' __bases__, removing the no-op dummy class
    • finish creating the class
    • call the parent __init_subclass__ with the now complete Enum class

    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__.

    @ethanfurman ethanfurman added the 3.10 only security fixes label Dec 29, 2020
    @ethanfurman ethanfurman self-assigned this Dec 29, 2020
    @ethanfurman ethanfurman added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error 3.10 only security fixes labels Dec 29, 2020
    @ethanfurman ethanfurman self-assigned this Dec 29, 2020
    @ethanfurman ethanfurman added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 29, 2020
    @ethanfurman
    Copy link
    Member Author

    My understanding of using keywords in class headers

      class MyClass(SomeBaseClass, setting='maybe'):
        ...

    is that the keywords would get passed into the super classes __init_subclass__ (SomeBaseClass and setting).

    However, in the cases of

    • test_descr.py
    • test_py_compile.py
    • typing.py

    that wasn't happening -- until the initial patch of moving the calls from type.__new__ to type.__init__.

    An __init__ has been added in those three locations to discard the keyword arguments being passed in.

    @gvanrossum
    Copy link
    Member

    Whoa, you start a discussion on python-dev and another on bpo? That sounds a bit hasty.

    @ethanfurman
    Copy link
    Member Author

    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.

    @gvanrossum
    Copy link
    Member

    I see this as a backwards incompatible change and it's not worth doing for what seems to me a very minor benefit.

    @ethanfurman
    Copy link
    Member Author

    Nick Coghlan made the observation that __set_name__ should be doing what is currently the after-new work.

    Tracking in bpo-42901.

    @gvanrossum
    Copy link
    Member

    Thanks, that's great! And thanks, Nick!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants