The validation.py module#
Summary#
Parse code and return a typed result (memoized). |
|
Replace JavaScript-style Booleans/null with Python equivalents. |
Description#
Static code validation shared across backends.
AST parse plus a few cheap heuristics. Backends with a live solver session can layer real semantic checks on top by extending validate_code in their own implementation.
Module detail#
- validation.validate_python_source(code: str, *, forbidden_calls: Iterable[str] = _FORBIDDEN_CALLS, strict: bool = False, extra_allowed_names: Iterable[str] = ()) ansys.fluent.mcp.common.models.RunCodeResult#
Parse code and return a typed result (memoized).
This is a pure function, which means the same inputs always produce the same RunCodeResult. A bounded LRU keyed by
(sha256(code), strict, frozenset(forbidden_calls), frozenset(extra_allowed_names))avoids re-walking the AST when the agent re-validates the same snippet, which is common during multi-edit retries).- Parameters:
- Returns:
RunCodeResultResult produced by the function.
- validation.sanitize_python_code(code: str) tuple[str, list[str]]#
Replace JavaScript-style Booleans/null with Python equivalents.
Uses the
tokenizemodule so that replacements only affectNAMEtokens. Occurrences inside string literals or comments are left untouched.Returns
(sanitized_code, fixes)wherefixesis a list of human-readable descriptions of each replacement made. If no replacements are needed, the original code is returned unchanged.