Skip to content

type_bridge.generator.render.attributes

attributes

Render attribute class definitions from parsed schema.

AttributeContext dataclass

AttributeContext(class_name, base_class, docstring, flags_args, regex=None, allowed_values=None, range_min=None, range_max=None, default=None, transform=None, independent=False)

Context for rendering a single attribute class.

render_attributes

render_attributes(schema, class_names)

Render the complete attributes module source.

Parameters:

Name Type Description Default
schema ParsedSchema

Parsed schema containing attribute definitions

required
class_names dict[str, str]

Mapping from TypeDB names to Python class names

required

Returns:

Type Description
str

Complete Python source code for attributes.py

Source code in type_bridge/generator/render/attributes.py
def render_attributes(schema: ParsedSchema, class_names: dict[str, str]) -> str:
    """Render the complete attributes module source.

    Args:
        schema: Parsed schema containing attribute definitions
        class_names: Mapping from TypeDB names to Python class names

    Returns:
        Complete Python source code for attributes.py
    """
    logger.debug(f"Rendering {len(schema.attributes)} attribute classes")

    imports = sorted(_get_required_imports(schema, class_names))
    uses_classvar = any(
        attr.allowed_values or attr.regex or attr.range_min or attr.range_max
        for attr in schema.attributes.values()
    )

    attributes = []
    all_names = []
    for attr_name in _topological_sort_attributes(schema):
        all_names.append(class_names[attr_name])
        attributes.append(_build_attribute_context(attr_name, schema, class_names))

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

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