Skip to content

type_bridge.generator.render.entities

entities

Render entity class definitions from parsed schema.

EntityContext dataclass

EntityContext(class_name, base_class, docstring, flags_args, prefix=None, plays=list(), fields=list(), cascade_attrs=list(), subkey_groups=dict(), plays_cardinalities=dict())

Context for rendering a single entity class.

render_entities

render_entities(schema, attr_class_names, entity_class_names, implicit_key_attributes=None)

Render the complete entities module source.

Parameters:

Name Type Description Default
schema ParsedSchema

Parsed schema containing entity definitions

required
attr_class_names dict[str, str]

Mapping from TypeDB attr names to Python class names

required
entity_class_names dict[str, str]

Mapping from TypeDB entity names to Python class names

required
implicit_key_attributes set[str] | None

Attributes to treat as keys even without @key

None

Returns:

Type Description
str

Complete Python source code for entities.py

Source code in type_bridge/generator/render/entities.py
def render_entities(
    schema: ParsedSchema,
    attr_class_names: dict[str, str],
    entity_class_names: dict[str, str],
    implicit_key_attributes: set[str] | None = None,
) -> str:
    """Render the complete entities module source.

    Args:
        schema: Parsed schema containing entity definitions
        attr_class_names: Mapping from TypeDB attr names to Python class names
        entity_class_names: Mapping from TypeDB entity names to Python class names
        implicit_key_attributes: Attributes to treat as keys even without @key

    Returns:
        Complete Python source code for entities.py
    """
    logger.debug(f"Rendering {len(schema.entities)} entity classes")
    implicit_keys = implicit_key_attributes or set()
    needs_card = _needs_card_import(schema)

    imports = ["Entity", "Flag", "Key", "TypeFlags", "Unique"]
    if needs_card:
        imports.insert(1, "Card")

    entities = []
    all_names = []
    for entity_name in _topological_sort_entities(schema):
        all_names.append(entity_class_names[entity_name])
        entities.append(
            _build_entity_context(
                entity_name,
                schema,
                attr_class_names,
                entity_class_names,
                implicit_keys,
            )
        )

    template = get_template("entities.py.jinja")
    result = template.render(
        imports=imports,
        entities=entities,
        all_names=sorted(all_names),
    )

    logger.info(f"Rendered {len(all_names)} entity classes")
    return result