Skip to content

Instantly share code, notes, and snippets.

@rafaellehmkuhl
Last active July 2, 2021 18:20
Show Gist options
  • Save rafaellehmkuhl/0185fdd86a16854781e4cc958aa810e9 to your computer and use it in GitHub Desktop.
Save rafaellehmkuhl/0185fdd86a16854781e4cc958aa810e9 to your computer and use it in GitHub Desktop.
MyPy abstract method implementation check
import abc
from typing import List, Type
class AbstractClass(metaclass=abc.ABCMeta):
def __init__(self) -> None:
pass
@staticmethod
@abc.abstractmethod
def is_ok() -> bool:
pass
@staticmethod
def available_subclasses() -> List[Type["AbstractClass"]]:
available_interfaces = []
for subclass in AbstractClass.__subclasses__():
is_ok_method = getattr(subclass, "is_ok", None)
if callable(is_ok_method):
available_interfaces.append(subclass)
return available_interfaces
# return [subclass for subclass in AbstractClass.__subclasses__() if subclass.is_ok()]
class ConcreteClass(AbstractClass):
def __init__(self) -> None:
super().__init__()
# @staticmethod
# def is_ok() -> bool:
# return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment