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

Calling super().__init__ in subclasses of typing.Protocol raises RecursionError #89284

Closed
hauntsaninja opened this issue Sep 6, 2021 · 11 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes release-blocker stdlib Python modules in the Lib dir

Comments

@hauntsaninja
Copy link
Contributor

BPO 45121
Nosy @ericvsmith, @ambv, @JelleZijlstra, @pablogsal, @miss-islington, @hauntsaninja, @uriyyo, @Fidget-Spinner
PRs
  • bpo-45121: Fix issue when Protocol.__init__ raise RecursionError #28206
  • [3.10] bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) #28232
  • [3.9] bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) #28233
  • 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 = None
    closed_at = <Date 2021-09-19.19:19:02.151>
    created_at = <Date 2021-09-06.22:05:20.335>
    labels = ['release-blocker', 'library', '3.9', '3.10', '3.11']
    title = 'Calling super().__init__ in subclasses of typing.Protocol raises RecursionError'
    updated_at = <Date 2021-10-04.19:18:42.785>
    user = 'https://github.com/hauntsaninja'

    bugs.python.org fields:

    activity = <Date 2021-10-04.19:18:42.785>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-09-19.19:19:02.151>
    closer = 'pablogsal'
    components = ['Library (Lib)']
    creation = <Date 2021-09-06.22:05:20.335>
    creator = 'hauntsaninja'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45121
    keywords = ['patch']
    message_count = 11.0
    messages = ['401184', '401197', '401199', '401200', '401284', '401369', '401384', '401385', '401386', '402164', '403161']
    nosy_count = 8.0
    nosy_names = ['eric.smith', 'lukasz.langa', 'JelleZijlstra', 'pablogsal', 'miss-islington', 'hauntsaninja', 'uriyyo', 'kj']
    pr_nums = ['28206', '28232', '28233']
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue45121'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @hauntsaninja
    Copy link
    Contributor Author

    Consider:

    from typing import Protocol
    
    class P(Protocol):
        ...
    
    class C(P):
        def __init__(self):
            super().__init__()
    
    C()
    

    This code passes without error on 3.9.6.

    With 3.9.7, we get:

    Traceback (most recent call last):
      File "/Users/shantanu/dev/test.py", line 10, in <module>
        C()
      File "/Users/shantanu/dev/test.py", line 8, in __init__
        super().__init__()
      File "/Users/shantanu/.pyenv/versions/3.9.7/lib/python3.9/typing.py", line 1083, in _no_init
        raise TypeError('Protocols cannot be instantiated')
    TypeError: Protocols cannot be instantiated
    

    I bisected this to:

    bpo-44806: Fix __init__ in subclasses of protocols (GH-27545) (GH-27559)

    Note there is also an interaction with the later commit:

    bpo-45081: Fix __init__ method generation when inheriting from Protocol (GH-28121) (GH-28132)

    This later commit actually causes a RecursionError:

      File "/Users/shantanu/dev/cpython/Lib/typing.py", line 1103, in _no_init_or_replace_init
        cls.__init__(self, *args, **kwargs)
      File "/Users/shantanu/dev/test.py", line 8, in __init__
        super().__init__()
      File "/Users/shantanu/dev/cpython/Lib/typing.py", line 1103, in _no_init_or_replace_init
        cls.__init__(self, *args, **kwargs)
      File "/Users/shantanu/dev/test.py", line 8, in __init__
        super().__init__()
    

    Originally reported by @tyralla on Gitter.

    @hauntsaninja hauntsaninja added 3.9 only security fixes stdlib Python modules in the Lib dir labels Sep 6, 2021
    @ericvsmith
    Copy link
    Member

    Possibly related to bpo-45081, which ended up not having anything to do with dataclasses (despite the title).

    Can you check if that fix solves your problem?

    @hauntsaninja
    Copy link
    Contributor Author

    As I mentioned in the post, bpo-45081 actually makes this issue worse, since we get a RecursionError.

    I think TypeError: Protocols cannot be instantiated is probably okay behaviour (as opposed to RecursionError), more just unfortunate that it seems to be an unanticipated breaking change.

    @hauntsaninja
    Copy link
    Contributor Author

    Sorry if my message was confusing. Hopefully the following makes things clear:

    3.9.6: snippet runs without error

    3.9.7, with bpo-44806: (a probably reasonable) TypeError, but a breaking change

    main, with bpo-45081: RecursionError

    @ericvsmith
    Copy link
    Member

    Sorry I didn't notice your reference to bpo-45081.

    @Fidget-Spinner Fidget-Spinner added 3.10 only security fixes 3.11 only security fixes labels Sep 7, 2021
    @Fidget-Spinner Fidget-Spinner changed the title Regression in 3.9.7 with typing.Protocol Calling super().__init__ in subclasses of typing.Protocol raises RecursionError Sep 7, 2021
    @Fidget-Spinner Fidget-Spinner added 3.10 only security fixes 3.11 only security fixes labels Sep 7, 2021
    @Fidget-Spinner Fidget-Spinner changed the title Regression in 3.9.7 with typing.Protocol Calling super().__init__ in subclasses of typing.Protocol raises RecursionError Sep 7, 2021
    @Fidget-Spinner
    Copy link
    Member

    New changeset c11956a by Yurii Karabas in branch 'main':
    bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206)
    c11956a

    @ambv
    Copy link
    Contributor

    ambv commented Sep 8, 2021

    New changeset 99506dc by Ken Jin in branch '3.9':
    [3.9] bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28233)
    99506dc

    @ambv
    Copy link
    Contributor

    ambv commented Sep 8, 2021

    New changeset c081649 by Miss Islington (bot) in branch '3.10':
    bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28232)
    c081649

    @ambv
    Copy link
    Contributor

    ambv commented Sep 8, 2021

    Pablo, marking as release blocker. PR #72419 is merged to 3.10. It should be cherry-picked for 3.10.0 inclusion.

    @pablogsal
    Copy link
    Member

    Pablo, marking as release blocker. PR #72419 is merged to 3.10. It should be cherry-picked for 3.10.0 inclusion.

    Done!

    @pablogsal
    Copy link
    Member

    New changeset db762a9 by Pablo Galindo (Miss Islington (bot)) in branch '3.10':
    bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28232)
    db762a9

    @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.9 only security fixes 3.10 only security fixes 3.11 only security fixes release-blocker stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants