mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
35 lines
1,010 B
Python
35 lines
1,010 B
Python
"""Python code syntax validation.
|
|
|
|
This module provides a validator that uses libcst for Python syntax validation.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import libcst as cst
|
|
|
|
from aiservice.common.cst_utils import parse_module_to_cst
|
|
|
|
|
|
class PythonValidator:
|
|
"""Validator for Python code syntax using libcst."""
|
|
|
|
def validate_syntax(self, code: str) -> tuple[bool, str | None]:
|
|
"""Validate Python syntax using libcst.
|
|
|
|
Args:
|
|
code: The Python code to validate.
|
|
|
|
Returns:
|
|
A tuple of (is_valid, error_message).
|
|
- is_valid: True if the code is syntactically valid Python.
|
|
- error_message: None if valid, otherwise the parse error message.
|
|
|
|
"""
|
|
try:
|
|
parse_module_to_cst(code)
|
|
return True, None
|
|
except cst.ParserSyntaxError as e:
|
|
return False, str(e)
|
|
except Exception as e:
|
|
# Catch any other parsing errors
|
|
return False, f"Parse error: {e}"
|