The validation.py module#

Summary#

validate_python_source

Validate Python source code before it is executed in a CFX session.

sanitize_python_code

Replace common non-Python literal tokens in generated Python code.

Description#

Static code validation shared across backends.

Phase 2: AST parse + 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.cfx.mcp.common.models.RunCodeResult#

Validate Python source code before it is executed in a CFX session.

Parameters:
codestr

Python source code submitted for validation, grounding, or execution.

forbidden_callsIterable[str], optional

Dotted call names that must be rejected by the validator. Default is _FORBIDDEN_CALLS.

strictbool, optional

Whether to enforce import and name allow-lists in addition to syntax checks. Default is False.

extra_allowed_namesIterable[str], optional

Additional top-level names allowed when strict validation is enabled. Default is ().

Returns:
RunCodeResult

Validation result describing whether the submitted Python source is acceptable.

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

Replace common non-Python literal tokens in generated Python code.

Parameters:
codestr

Python source code submitted for validation, grounding, or execution.

Returns:
tuple[str, list[str]]

Sanitized code and a list of textual fixes that were applied.