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

Change from ABC to protocol. #58

Open
BSchilperoort opened this issue Nov 8, 2024 · 0 comments
Open

Change from ABC to protocol. #58

BSchilperoort opened this issue Nov 8, 2024 · 0 comments

Comments

@BSchilperoort
Copy link
Contributor

Python nowadays supports "protocols". Protocols are mostly a typing thing, and allow you to specify that a class follows the protocol without explicitly being a subclass.

When using the BMI as an abstract base class, you currently need to use the following syntax:

def update_bmi(bmi: BmiABC) -> None:
    bmi.update()

class BmiImplementation(BmiABC):
    def update(self) -> None:
        ....

update_bmi(BmiImplementation())  # happy type checker

If you don't subclass BMI, type checkers will get angry at this, as BmiImplementation is not a subclass of BmiABC:

def update_bmi(bmi: BmiABC) -> None:
    bmi.update()

class BmiImplementation:
    def update(self) -> None:
        ....

update_bmi(BmiImplementation())  # sad type checker

However, if you specify that Bmi is a subclass of typing.protocol, the following syntax passes type checkers:

def update_bmi(bmi: BmiProtocol) -> None:
    bmi.update()

class BmiImplementation:
    def update(self) -> None:
        ...

update_bmi(BmiImplementation())  # happy type checker

This is because type checkers will check if BmiImplementation follows the BmiProtocol: does it have all methods that are in the protocol, and do all the methods have the correct typing.

To implement this, all that we would need to change is;

from typing import Protocol

class Bmi(Protocol):

Note that you can still subclass from Protocol like you could from an ABC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant