Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/blocks/__init__.py (Python)

Code before:

from backend.data.block import Block

# Dynamically load all modules under backend.blocks
AVAILABLE_MODULES = []
current_dir = Path(__file__).parent
modules = [
    str(f.relative_to(current_dir))[:-3].replace(os.path.sep, ".")
    for f in current_dir.rglob("*.py")
    if f.is_file() and f.name != "__init__.py"
]
for module in modules:
    if not re.match("^[a-z_.]+$", module):
        raise ValueError(
            f"Block module {module} error: module name must be lowercase, "
            "separated by underscores, and contain only alphabet characters"
        )

    importlib.import_module(f".{module}", package=__name__)
    AVAILABLE_MODULES.append(module)

# Load all Block instances from the available modules
AVAILABLE_BLOCKS: dict[str, Type[Block]] = {}


def all_subclasses(clz):
    subclasses = clz.__subclasses__()
    for subclass in subclasses:
        subclasses += all_subclasses(subclass)
    return subclasses


for cls in all_subclasses(Block):
    name = cls.__name__

    if cls.__name__.endswith("Base"):
        continue

    if not cls.__name__.endswith("Block"):
        raise ValueError(
            f"Block class {cls.__name__} does not end with 'Block', If you are creating an abstract class, please name the class with 'Base' at the end"
        )

    block = cls()

    if not isinstance(block.id, str) or len(block.id) != 36:
        raise ValueError(f"Block ID {block.name} error: {block.id} is not a valid UUID")

    if block.id in AVAILABLE_BLOCKS:
        raise ValueError(f"Block ID {block.name} error: {block.id} is already in use")


Review comment (suggestion):
I know this isn't changed but python doesn't like `cls` here or something? It looks fine for me in vs code when changed.

Code after:

from backend.data.block import Block

# Dynamically load all modules under backend.blocks
AVAILABLE_MODULES = []
current_dir = Path(__file__).parent
modules = [
    str(f.relative_to(current_dir))[:-3].replace(os.path.sep, ".")
    for f in current_dir.rglob("*.py")
    if f.is_file() and f.name != "__init__.py"
]
for module in modules:
    if not re.match("^[a-z_.]+$", module):
        raise ValueError(
            f"Block module {module} error: module name must be lowercase, "
            "separated by underscores, and contain only alphabet characters"
        )

    importlib.import_module(f".{module}", package=__name__)
    AVAILABLE_MODULES.append(module)

# Load all Block instances from the available modules
AVAILABLE_BLOCKS: dict[str, Type[Block]] = {}


T = TypeVar("T")


def all_subclasses(cls: Type[T]) -> list[Type[T]]:
    subclasses = cls.__subclasses__()
    for subclass in subclasses:
        subclasses += all_subclasses(subclass)
    return subclasses


for block_cls in all_subclasses(Block):
    name = block_cls.__name__

    if block_cls.__name__.endswith("Base"):
        continue

    if not block_cls.__name__.endswith("Block"):
        raise ValueError(
            f"Block class {block_cls.__name__} does not end with 'Block', If you are creating an abstract class, please name the class with 'Base' at the end"
        )

    block = block_cls.create()

    if not isinstance(block.id, str) or len(block.id) != 36:
        raise ValueError(f"Block ID {block.name} error: {block.id} is not a valid UUID")