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#
- From
pyfect.exit:die_exit,Success - From
pyfect.schema.ast:MISSING,MISSING_EXIT - From
pyfect.schema.compiler:DEFAULT_PARSE_OPTIONS,INVALID
Classes#
Functions#
keys_of#
keys_of(input) -> Mapping[object, object] | Noneinput read as keys, as a struct reads it (None: not an object).
| Parameter | Type | Description |
|---|---|---|
input | object |
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 Nonecheck_issues#
check_issues(ast, value, encoded, options) -> boolast's checks (its encoding checks when encoded) fail on value.
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
value | object | |
encoded | bool | |
options | ParseOptions |
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 Nonelocal_value#
local_value(ast, value, options) -> objectvalue parsed by leaf ast and its checks, or INVALID.
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
value | object | |
options | ParseOptions |
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 INVALIDindex_entries#
index_entries(ast, keys, options) -> list[list[object]]The keys each index signature takes, read before any property, as the interpreter reads them.
| Parameter | Type | Description |
|---|---|---|
ast | Objects | |
keys | Mapping[object, object] | |
options | ParseOptions |
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) -> boolkeys holds a key no property declares and no index signature takes.
| Parameter | Type | Description |
|---|---|---|
ast | Objects | |
keys | Mapping[object, object] | |
options | ParseOptions |
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]| Parameter | Type | Description |
|---|---|---|
ast | Objects |
Returns frozenset[Hashable]
def declared(ast: Objects) -> frozenset[Hashable]:
return frozenset(p.name for p in ast.properties)array_maker#
array_maker(ast, element) -> MakeA homogeneous array's synchronous constructor over element's.
| Parameter | Type | Description |
|---|---|---|
ast | Arrays | |
element | Make |
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 makedecode#
decode(ast, resolve_child, detailed=False) -> ParserThe 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).
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
resolve_child | Resolve | |
detailed | bool = 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) -> ParserThe complete constructor of ast over the registry.
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
resolve_child | Resolve |
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) -> AnyThe issues of ast's checks on value (None: none, or
checks disabled), for an issue built from them without running them
again.
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
value | object | |
options | ParseOptions |
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) -> ParseResultinput is not a value of ast's single encoding source (its
type when issues is None, else its checks' issues).
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
input | object | |
issues | Any | |
options | ParseOptions |
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) -> ParseResultA transformed value is not of ast's type (issues is
None) or fails its checks.
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
value | object | |
issues | Any | |
options | ParseOptions |
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) -> EntryThe entry of ast, made and cached on first use: compiled by the
installed compiler, if any, else interpreted.
| Parameter | Type | Description |
|---|---|---|
ast | AST |
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) -> EntryInstall compile for ast: each operation made on first use.
| Parameter | Type | Description |
|---|---|---|
ast | AST | |
compile | Compile | |
resolve_child | Resolve = 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))