Skip to content

hexa

hexa

Hexa: Type-declared dependency injection for Python.

Classes

MethodSpec dataclass

1
2
3
4
5
MethodSpec(
    name: str,
    args: list[str] = list(),
    kwargs: dict[str, str] = dict(),
)

Specification of a single abstract method.

PortNode dataclass

1
2
3
4
5
6
7
8
PortNode(
    name: str,
    port_cls: str,
    kind: str = "leaf",
    attributes: dict[str, ConfigValue] = dict(),
    children: list[PortNode] = list(),
    methods: dict[str, MethodSpec] = dict(),
)

A node in the hexa port tree.

Parameters

name : str Slot name, e.g. "parser", "amount_balance". kind : str One of "leaf", "port", or "parent". - "leaf": no children (e.g. NumberPort) - "port": has children (e.g. TransactionParserPort) - "parent": the root of the tree (e.g. ExtractionPipeline) port_cls : str ABC class name that gives this port its shape, e.g. "TransactionParserPort". Every port has a concrete typed shape. attributes : dict[str, ConfigValue] Annotated attributes: name -> type_expr for scalars, or a nested dict of sub-attributes (a struct) for grouped/structured values. children : list[PortNode] Nested port slots. methods : dict[str, MethodSpec] Abstract methods keyed by method name.

Functions
diff
diff(other: PortNode, path: str = '') -> list[str]

Return a list of human-readable difference strings.

An empty list means the trees are equal.

find
find(name: str) -> PortNode | None

Find a direct child by slot name.

walk
walk() -> list[PortNode]

Return all nodes in depth-first order (self first).

Functions

build

1
2
3
4
5
6
build(
    cls: type[T],
    *,
    instances: dict[str, Any] | None = None,
    config: dict[str, Any] | None = None
) -> T

Recursively instantiate the port tree from annotations.

Parameters

cls : type The root class to build (e.g. AxisExtractionPipeline). instances : dict, optional {slot_name: object} injected verbatim into every node whose annotations contain that name (runtime values — repos, handlers, clients — that must not be re-constructed). Applied to the whole tree by name. config : dict, optional {field_name: value} propagated by name to every node's annotated config fields (non-port attributes), replacing the default None.

Returns

instance A fully wired instance of cls and all its nested ports.