Coercing authored values into the pure node IR.
coerce_input normalizes any value flowing into a node input to a
Procedure: a raw scalar lifts to a Constant, and a Path position used
where a value is wanted resolves to the CacheProcedure that wrote it. Both
the node metaclass and the fluent operators (|=, &=, []) funnel
through here, so there is one definition of "what a value input accepts".
Normalize a node-input value to a Procedure.
- a path-typed
Procedure → the CacheProcedure that wrote that slot
(an unwritten slot raises: loading is explicit, via .load() /
.load_or()), but only when resolve_path is set; a field that itself
wants a Path passes resolve_path=False to keep the position intact
- any other
Procedure → unchanged
- a
JSONScalar instance or raw bool/int/float/str → a
Constant
- anything else → unchanged (the node's own validation raises a field-aware error)
Source code in capturegraph-lib/capturegraph/procedures/procedure/authoring/bridge.py
| def coerce_input(
value: object,
*,
resolve_path: bool = True,
) -> Procedure[CGType]:
"""Normalize a node-input value to a ``Procedure``.
- a path-typed ``Procedure`` → the ``CacheProcedure`` that wrote that slot
(an unwritten slot raises: loading is explicit, via ``.load()`` /
``.load_or()``), but only when ``resolve_path`` is set; a field that itself
wants a ``Path`` passes ``resolve_path=False`` to keep the position intact
- any other ``Procedure`` → unchanged
- a ``JSONScalar`` instance or raw ``bool``/``int``/``float``/``str`` → a
``Constant``
- anything else → unchanged (the node's own validation raises a field-aware error)
"""
from capturegraph.procedures.procedure import (
Procedure,
)
from capturegraph.types.core.scalar.base import (
JSONScalar,
)
from capturegraph.types.special.path import (
Path,
)
if isinstance(value, Procedure):
return_type = value.return_type
if resolve_path and isinstance(return_type, type) and issubclass(return_type, Path):
return _resolve_path_value(cast(Procedure[Path], value))
return value
if isinstance(value, (JSONScalar, bool, int, float, str)):
from capturegraph.procedures.nodes.process.constant import (
const,
)
return const(value)
# Not coercible: hand it back so the node's __post_init__ validation rejects it
# with a message that names the offending field.
return value # type: ignore[return-value]
|