Skip to content

Should concrete implementations be required to implement protocol? #8926

Description

@pauleveritt
  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Protocol


class Person(Protocol):
    first_name: str

    def greeting(self, message: str) -> str:
        ...


class FrenchPerson(Person):
    prenom: str

    def __init__(self, name: str):
        self.prenom = name

    def salutation(self, message: str) -> str:
        return f"{message} {self.prenom}"


def print_person(person: Person) -> str:
    return person.greeting(message="Je m'appelle")


def main():
    fp = FrenchPerson(name='Henri')
    response = print_person(fp)
    print(response)
  • What is the actual behavior/output?

PEP 544 says you can state that an implementation supports a protocol by
subclassing the protocol.

It also says: "If one omits Protocol in the base class list, this would
be a regular (non-protocol) class that must implement Sized."

  • What is the behavior/output you expect?

I would hope mypy would, either at declaration time or in the print_person function, spot that something claimed to be a Person but had neither the protocol variable name nor the method greeting.

It's also a shame that FrenchPerson got the greeting method added to it from the subclassing.

  • What are the versions of mypy and Python you are using?

mypy 0.770 and Python 3.8.1

  • What are the mypy flags you are using? (For example --strict-optional)

None.

Activity

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions