The validation.py module#

Summary#

validate_python_source

Parse code and return a typed result (memoized).

sanitize_python_code

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:
codestr

Python code snippet to validate or execute.

forbidden_callsIterable[str]

Forbidden calls to supply to the function.

strictbool

Strict to supply to the function.

extra_allowed_namesIterable[str]

Extra allowed names to supply to the function.

Returns:
RunCodeResult

Result produced by the function.

validation.sanitize_python_code(code: str) tuple[str, list[str]]#

Replace JavaScript-style Booleans/null with Python equivalents.

Uses the tokenize module so that replacements only affect NAME tokens. Occurrences inside string literals or comments are left untouched.

Returns (sanitized_code, fixes) where fixes is a list of human-readable descriptions of each replacement made. If no replacements are needed, the original code is returned unchanged.

Parameters:
codestr

Python code snippet to validate or execute.

Returns:
tuple[str, list[str]]

Collection containing the operation results.