Skip to content

Protocol can't constrain __init__ #12970

@glyph

Description

@glyph

Bug Report

The following code produces an error when var() is called, but not when it is declared. If you can declare __init__ on Protocols, shouldn't implementations have to match, at least when assigned as Types?

from typing import Protocol, Type

class Proto(Protocol):
    def __init__(self, x: int) -> None:
        ...

class Impl:
    def __init__(self) -> None:
        ...

var: Type[Proto] = Impl
var()

Your Environment

  • Mypy version used: mypy 0.961 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.10.4 (python.org)
  • Operating system and version:
ProductName:	macOS
ProductVersion:	12.4
BuildVersion:	21F79

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions