GitHub

API reference for runtime.

What generated schema operations call (Effect's SchemaCompiler/runtime). It holds no code generation, so a module the ahead-of-time compiler wrote runs on it alone. Generated modules must come from the same pyfect version.

Re-exports

Classes

Functions

keys_of

keys_of(input) -> Mapping[object, object] | None

input read as keys, as a struct reads it (None: not an object).

ParameterTypeDescription
inputobject

Returns Mapping[object, object] | None

def keys_of(input: object) -> Mapping[object, object] | None:
    """``input`` read as keys, as a struct reads it (``None``: not an object)."""
    if isinstance(input, Mapping):
        return cast("Mapping[object, object]", input)
    if reads_properties(input):
        return ObjectKeys(input)
    return None

check_issues

check_issues(ast, value, encoded, options) -> bool

ast's checks (its encoding checks when encoded) fail on value.

ParameterTypeDescription
astAST
valueobject
encodedbool
optionsParseOptions

Returns bool

def check_issues(ast: AST, value: object, encoded: bool, options: ParseOptions) -> bool:
    """``ast``'s checks (its encoding checks when ``encoded``) fail on ``value``."""
    checks = ast.encoding_checks if encoded else ast.checks
    if checks is None or options.get("disable_checks"):
        return False
    return collect_issues(checks, value, ast, options) is not None

local_value

local_value(ast, value, options) -> object

value parsed by leaf ast and its checks, or INVALID.

ParameterTypeDescription
astAST
valueobject
optionsParseOptions

Returns object

def local_value(ast: AST, value: object, options: ParseOptions) -> object:
    """``value`` parsed by leaf ``ast`` and its checks, or ``INVALID``."""
    result = ast.parse_local(value, options)
    return result.value if type(result) is Success else INVALID

index_entries

index_entries(ast, keys, options) -> list[list[object]]

The keys each index signature takes, read before any property, as the interpreter reads them.

ParameterTypeDescription
astObjects
keysMapping[object, object]
optionsParseOptions

Returns list[list[object]]

def index_entries(
    ast: Objects, keys: Mapping[object, object], options: ParseOptions
) -> list[list[object]]:
    """The keys each index signature takes, read before any property, as
    the interpreter reads them."""
    return [[key for key in keys if index.selects(key, options)] for index in ast.index_signatures]

has_excess

has_excess(ast, keys, options) -> bool

keys holds a key no property declares and no index signature takes.

ParameterTypeDescription
astObjects
keysMapping[object, object]
optionsParseOptions

Returns bool

def has_excess(ast: Objects, keys: Mapping[object, object], options: ParseOptions) -> bool:
    """``keys`` holds a key no property declares and no index signature takes."""
    declared = {p.name for p in ast.properties}
    return any(
        key not in declared and not any(i.selects(key, options) for i in ast.index_signatures)
        for key in keys
    )

declared

declared(ast) -> frozenset[Hashable]
ParameterTypeDescription
astObjects

Returns frozenset[Hashable]

def declared(ast: Objects) -> frozenset[Hashable]:
    return frozenset(p.name for p in ast.properties)

array_maker

array_maker(ast, element) -> Make

A homogeneous array's synchronous constructor over element's.

ParameterTypeDescription
astArrays
elementMake

Returns Make

def array_maker(ast: Arrays, element: Make) -> Make:
    """A homogeneous array's synchronous constructor over ``element``'s."""

    def make(input: object, options: ParseOptions) -> object:
        if input is MISSING:
            return MISSING
        if not isinstance(input, (list, tuple)):
            return INVALID
        out: list[object] = []
        for item in cast("list[object]", input):
            value = element(item, options)
            if value is INVALID or value is MISSING:
                return INVALID
            out.append(value)
        if check_issues(ast, out, False, options):
            return INVALID
        return out if ast.mutable else tuple(out)

    return make

decode

decode(ast, resolve_child, detailed=False) -> Parser

The complete decoder of a compiled ast: a single pure transformation between leaves inline, else the interpreter over the registry (detailed: the node's own fast path ran first, so its children parse in detail).

ParameterTypeDescription
astAST
resolve_childResolve
detailedbool = False

Returns Parser

def decode(ast: AST, resolve_child: Resolve, detailed: bool = False) -> Parser:
    """The complete decoder of a compiled ``ast``: a single pure
    transformation between leaves inline, else the interpreter over the
    registry (``detailed``: the node's own fast path ran first, so its
    children parse in detail)."""
    inline = _encoding_decoder(ast)
    if inline is not None:
        return inline
    if isinstance(ast, Objects) and ast.properties and not ast.index_signatures:
        return interpreter(ast, _inlining(ast, resolve_child), detailed)
    return interpreter(ast, resolve_child, detailed)

make

make(ast, resolve_child) -> Parser

The complete constructor of ast over the registry.

ParameterTypeDescription
astAST
resolve_childResolve

Returns Parser

def make(ast: AST, resolve_child: Resolve) -> Parser:
    """The complete constructor of ``ast`` over the registry."""
    return interpreter(ast, resolve_child)

issues_of

issues_of(ast, value, options) -> Any

The issues of ast's checks on value (None: none, or checks disabled), for an issue built from them without running them again.

ParameterTypeDescription
astAST
valueobject
optionsParseOptions

Returns Any

def issues_of(ast: AST, value: object, options: ParseOptions) -> Any:
    """The issues of ``ast``'s checks on ``value`` (``None``: none, or
    checks disabled), for an issue built from them without running them
    again."""
    if ast.checks is None or options.get("disable_checks"):
        return None
    return collect_issues(ast.checks, value, ast, options)

invalid_source

invalid_source(ast, input, issues, options) -> ParseResult

input is not a value of ast's single encoding source (its type when issues is None, else its checks' issues).

ParameterTypeDescription
astAST
inputobject
issuesAny
optionsParseOptions

Returns ParseResult

def invalid_source(ast: AST, input: object, issues: Any, options: ParseOptions) -> ParseResult:
    """``input`` is not a value of ``ast``'s single encoding source (its
    type when ``issues`` is ``None``, else its checks' ``issues``)."""
    source = cast("Encoding", ast.encoding)[0].to
    shown = reported(input, options)
    inner = InvalidType(source, shown) if issues is None else Composite(source, issues, shown)
    return fail_exit(EncodingIssue(ast, inner, shown))

invalid_target

invalid_target(ast, value, issues, options) -> ParseResult

A transformed value is not of ast's type (issues is None) or fails its checks.

ParameterTypeDescription
astAST
valueobject
issuesAny
optionsParseOptions

Returns ParseResult

def invalid_target(ast: AST, value: object, issues: Any, options: ParseOptions) -> ParseResult:
    """A transformed ``value`` is not of ``ast``'s type (``issues`` is
    ``None``) or fails its checks."""
    shown = reported(value, options)
    return fail_exit(InvalidType(ast, shown) if issues is None else Composite(ast, issues, shown))

resolve

resolve(ast) -> Entry

The entry of ast, made and cached on first use: compiled by the installed compiler, if any, else interpreted.

ParameterTypeDescription
astAST

Returns Entry

def resolve(ast: AST) -> Entry:
    """The entry of ``ast``, made and cached on first use: compiled by the
    installed compiler, if any, else interpreted."""
    cached = _cached(ast)
    if cached is not None:
        return cached
    if not _State.active:
        return _store(ast, Entry(ast, None, False, _interpreted_under(ast)))
    compiler = _State.compiler
    compiled = None if compiler is None else compiler(ast, resolve)
    return _store(ast, Entry(ast, compiled, False, resolve))

set_compiler

set_compiler(ast, compile, resolve_child=resolve) -> Entry

Install compile for ast: each operation made on first use.

ParameterTypeDescription
astAST
compileCompile
resolve_childResolve = resolve

Returns Entry

def set_compiler(ast: AST, compile: Compile, resolve_child: Resolve = resolve) -> Entry:
    """Install ``compile`` for ``ast``: each operation made on first use."""
    _activate()
    return _store(ast, Entry(ast, compile, True, resolve_child))