Skip to content

containers

containers #

Descending a container type one step — the shared answer for both access families.

root.session.photo and CapturePanorama().frames[1] ask the same three questions of a type: what is this struct's field, this array's element, this map's entry? The path family (...nodes.destination.access) and its value twin (...nodes.process.access) each resolve their return type through here, so the rules and the error wording are written once and the node bodies stay a line or two.

Every function takes the context naming the caller (the node kind), so a failure reads "PathIndex: Session is not an Array".

array_element(container, context) #

The element type of the Array container.

Raises:

Type Description
TypeError

If container is not an Array, or is schemaless.

Source code in capturegraph-lib/capturegraph/procedures/procedure/type_checking/containers.py
def array_element(
    container: type[CGType],
    context: str,
) -> type[CGType]:
    """The element type of the ``Array`` ``container``.

    Raises:
        TypeError: If ``container`` is not an ``Array``, or is schemaless.
    """
    if not issubclass(container, Array):
        raise TypeError(f"{context}: {container.__name__} is not an Array")
    if container._element is None:
        raise TypeError(f"{context}: {container.__name__} is a schemaless Array")
    return container._element

map_entry(container, key_type, context) #

The value type of the Map container, for an entry keyed by key_type.

Raises:

Type Description
TypeError

If container is not a Map, is schemaless, or key_type is not a keyable scalar matching the map's key.

Source code in capturegraph-lib/capturegraph/procedures/procedure/type_checking/containers.py
def map_entry(
    container: type[CGType],
    key_type: object,
    context: str,
) -> type[CGType]:
    """The value type of the ``Map`` ``container``, for an entry keyed by ``key_type``.

    Raises:
        TypeError: If ``container`` is not a ``Map``, is schemaless, or ``key_type``
            is not a keyable scalar matching the map's key.
    """
    key, value = map_types(container, context)
    if not is_keyable(key_type):
        raise TypeError(f"{context}: {type_name(key_type)} is not a keyable scalar")
    check_type(key_type, key, f"{context} key")
    return value

map_types(container, context) #

The (key, value) types of the Map container.

Raises:

Type Description
TypeError

If container is not a Map, or is schemaless.

Source code in capturegraph-lib/capturegraph/procedures/procedure/type_checking/containers.py
def map_types(
    container: type[CGType],
    context: str,
) -> tuple[type[CGType], type[CGType]]:
    """The ``(key, value)`` types of the ``Map`` ``container``.

    Raises:
        TypeError: If ``container`` is not a ``Map``, or is schemaless.
    """
    if not issubclass(container, Map):
        raise TypeError(f"{context}: {container.__name__} is not a Map")
    if container._key is None or container._element is None:
        raise TypeError(f"{context}: {container.__name__} is a schemaless Map")
    return container._key, container._element

path_target(source) #

The concrete type a Procedure[Path[T]] points at, i.e. T.

Raises:

Type Description
TypeError

If the source's return type is not a Path with a concrete target (e.g. used before @cg.procedure rooted it at a schema).

Source code in capturegraph-lib/capturegraph/procedures/procedure/type_checking/containers.py
def path_target(source: object) -> type[CGType]:
    """The concrete type a ``Procedure[Path[T]]`` points at, i.e. ``T``.

    Raises:
        TypeError: If the source's return type is not a ``Path`` with a concrete
            target (e.g. used before ``@cg.procedure`` rooted it at a schema).
    """
    return_type = getattr(source, "return_type", None)
    target = getattr(return_type, "_target", None)
    if not (isinstance(target, type) and issubclass(target, CGType)):
        raise TypeError(
            f"expected a Path with a concrete target, got {type_name(return_type)} from {source}"
        )
    return target

struct_field(container, field, context) #

The type of container's field.

Raises:

Type Description
TypeError

If container is not a Struct, or declares no such field.

Source code in capturegraph-lib/capturegraph/procedures/procedure/type_checking/containers.py
def struct_field(
    container: type[CGType],
    field: str,
    context: str,
) -> type[CGType]:
    """The type of ``container``'s ``field``.

    Raises:
        TypeError: If ``container`` is not a ``Struct``, or declares no such field.
    """
    if not issubclass(container, Struct):
        raise TypeError(f"{context}: {container.__name__} is not a Struct")
    if field not in container._fields:
        raise TypeError(f"{context}: {container.__name__} has no field {field!r}")
    return container._fields[field]