xnano.utils.introspection
xnano.utils.introspection
¶
xnano.utils.introspection
Hook signature inspection and safe state-expression evaluation.
State/field expressions (@on_state("count > 0")) are evaluated by
walking a parsed AST with a small, fixed set of operators rather than
calling eval: names resolve only against the supplied namespace,
calls are limited to a whitelist of pure builtins, and attribute access
to dunder names is refused. There is no code execution path, so a
crafted expression can read declared state but cannot reach the
interpreter.
Functions:
-
get_compiled_state_expression–Return the cached parsed AST for
expression. -
evaluate_state_expression–Evaluate
expressionagainststate's attributes. -
is_reference_expression–Return whether
expressionis a bare reference (see above). -
evaluate_reference_value–Return the current value of a bare reference, or
_MISSING. -
get_first_function_parameter_type–Returns the type annotation of the first parameter of a
-
get_function_extra_parameter_count–Return parameters after
self/clson a callable.
get_compiled_state_expression
¶
get_compiled_state_expression(
expression: str,
) -> Expression | None
Return the cached parsed AST for expression.
Parses and caches on first use; a source that fails to parse is cached
as None so it is not retried on every call.
Parameters:
-
expression(str) –The expression source to parse.
Returns:
-
Expression | None–The parsed
eval-mode AST, orNonewhenexpressiondoes not parse.
Source code in xnano/utils/introspection.py
evaluate_state_expression
¶
Evaluate expression against state's attributes.
Safe by construction — the expression is walked node by node over a
fixed operator/builtin set, never executed. Returns False on any
error, including an unknown name, an unsupported construct, or an
AttributeError. The parse is cached by source, since hooks
re-evaluate the same expression on every tick/frame.
Parameters:
-
expression(str) –The expression to evaluate.
-
state(StateT) –The object whose attributes the expression reads.
Returns:
-
bool–The truthiness of the evaluated expression.
Source code in xnano/utils/introspection.py
is_reference_expression
¶
Return whether expression is a bare reference (see above).
Bare references drive mutation-triggered hooks: the watcher fires when the referenced value changes rather than each frame it is truthy.
Source code in xnano/utils/introspection.py
evaluate_reference_value
¶
Return the current value of a bare reference, or _MISSING.
Used by mutation-triggered @on_state/@on_field hooks to read
the watched value each frame. Any evaluation error (unknown name,
missing attribute) resolves to _MISSING so the watcher simply
holds its previous value instead of firing.
Source code in xnano/utils/introspection.py
get_first_function_parameter_type
¶
Returns the type annotation of the first parameter of a function.
Parameters:
-
function(Callable) –The function to get the first parameter type from.
Returns:
-
type | None–The type annotation of the first parameter of the function (if one exists).
Source code in xnano/utils/introspection.py
get_function_extra_parameter_count
¶
Return parameters after self / cls on a callable.
Parameters:
-
function(Callable) –The callable to inspect.
Returns:
-
int–The number of parameters following
selforcls.