-
Notifications
You must be signed in to change notification settings - Fork 22
Open
Labels
help wantedExtra attention is neededExtra attention is needed
Description
it'd be great if the abstractmethod
s next_tactic
and therefore search
could have async flavors.
Could use this pattern to say "one of next_tactic_async
, next_tactic
needs to be implemented":
from abc import ABC, abstractmethod
class MyABC(ABC):
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
# Check that at least one of the required methods is implemented
method1_implemented = 'method_a' in cls.__dict__
method2_implemented = 'method_b' in cls.__dict__
if not (method1_implemented or method2_implemented):
raise TypeError(f"Class {cls.__name__} must implement at least one of: method_a, method_b")
def method_a(self):
raise NotImplementedError("Subclass must implement method_a or method_b")
def method_b(self):
raise NotImplementedError("Subclass must implement method_a or method_b")
# This works
class ValidSubclass(MyABC):
def method_a(self):
return "implemented method_a"
# This fails at class definition time
class InvalidSubclass(MyABC): # TypeError raised here
def some_other_method(self):
pass
Happy to send a PR this week if you don't disagree that this could be desirably upstreamed.
Metadata
Metadata
Assignees
Labels
help wantedExtra attention is neededExtra attention is needed