Skip to content

Disallow instantiating a Protocol class #12261

Description

@adriangb

The following gives an error in Pyright and I think it should give an error in MyPy as well:

from typing import Protocol

class Foo(Protocol):
    ...

Foo()

image

https://mypy-play.net/?mypy=latest&python=3.10&gist=f4ca0ee490d9e8b3a117ee785b7bdaf6

I think that even if the protocol class implements an __init__ method, this should still be disallowed:

from typing import Protocol

class Foo(Protocol):
    def __init__(self) -> None:  pass

Foo()  # disallowed


class ConcreteFoo(Foo):
    ...

ConcreteFoo()  # allowed

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions