Skip to content

async agent baseclass #117

@quinn-dougherty

Description

@quinn-dougherty

it'd be great if the abstractmethods 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

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions