Skip to content

super().__init__ doesn't work in explicit protocol subclasses #572

Closed

Description

@ilevkivskyi

A possible solution is to append a call to existing __init__ while overwriting (after the check for _is_protocol=True, where we unconditionally raise because protocols can't be instantiated). This way explicit non-protocol subclasses can use a protocol __init__ as a "default constructor". Default implementations are already supported for other protocol members, so it is natural to expect that __init__ is not different. See also python/mypy#3823

cc: @JukkaL

Activity

  1. changed the title [-]super().__init__ is not called in explicit protocol subclasses[/-] [+]super().__init__ doesn't work in explicit protocol subclasses[/+] on Jul 13, 2018
  2. grahamc commented on May 18, 2020

    @grahamc

    I have run in to this problem with @adisbladis, and he came up with this workaround:

    if not TYPE_CHECKING:
        from typing import Generic
        Protocol = Generic
        def runtime_checkable(f):
            return f
    else:
        from typing_extensions import Protocol, runtime_checkable

    I accidentally commented this on #561 at first, my apologies.

  3. Yobmod commented on Aug 9, 2021

    @Yobmod

    Hi, I've just come across this in our testing when updating from 3.10.0.b4 to 3.10.0.rc1.
    There's been some change made to protocols for 3.10.0 that change runtime behaviour?

    In my case, the protocol classes don't have an __init__(), so I didn't expect them to cause an error, and they didn't with 3.8 or 3.9.

    I can't see what the change is in the 3.10 release notes, maybe the fix for runtime_checkable protocols with only attributes? (But we have methods, just no init().)

  4. grahamc commented on Aug 9, 2021

    @grahamc

    @Yobmod this bug has been open for 3 years, I wonder if you did some research that ties such a very recent change to this bug in particular?

  5. hauntsaninja commented on Aug 9, 2021

    @hauntsaninja
    Collaborator
  6. Yobmod commented on Aug 9, 2021

    @Yobmod

    @hauntsaninja Thanks for the heads-up! (I put in a workaround for now, but I'll check if fixed next rc).

  7. srittau commented on Nov 4, 2021

    @srittau
    Collaborator

    Closing here as this has been filed to bpo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions