|
1 | | -"""Module abstract base class and related data types.""" |
| 1 | +"""Module protocol and related data types.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | from dataclasses import dataclass, field |
6 | 6 |
|
7 | | -from typing import Any |
| 7 | +from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable |
8 | 8 |
|
9 | | -__all__ = ["ModuleAnnotations", "ModuleExample", "ValidationResult"] |
| 9 | +if TYPE_CHECKING: |
| 10 | + from pydantic import BaseModel |
| 11 | + |
| 12 | + from apcore.context import Context |
| 13 | + |
| 14 | +__all__ = ["Module", "ModuleAnnotations", "ModuleExample", "ValidationResult"] |
| 15 | + |
| 16 | + |
| 17 | +@runtime_checkable |
| 18 | +class Module(Protocol): |
| 19 | + """Protocol for apcore modules. |
| 20 | +
|
| 21 | + Any class with ``input_schema``, ``output_schema``, ``description``, |
| 22 | + and an ``execute(inputs, context)`` method satisfies this protocol. |
| 23 | + Inheriting from ``Module`` is optional but provides IDE autocompletion. |
| 24 | +
|
| 25 | + At runtime, ``@runtime_checkable`` only checks attribute existence. |
| 26 | + Static type checkers (pyright) will verify the full signature. |
| 27 | + """ |
| 28 | + |
| 29 | + input_schema: type[BaseModel] |
| 30 | + output_schema: type[BaseModel] |
| 31 | + description: str |
| 32 | + |
| 33 | + def execute(self, inputs: dict[str, Any], context: Context) -> dict[str, Any]: ... |
10 | 34 |
|
11 | 35 |
|
12 | 36 | @dataclass(frozen=True) |
|
0 commit comments