From df36970532bb719bedc6c6a3780d205ca77cc56c Mon Sep 17 00:00:00 2001 From: Louis Mandel Date: Fri, 8 Aug 2025 14:42:20 -0400 Subject: [PATCH 1/2] feat: check Python syntax in the linter Signed-off-by: Louis Mandel --- src/pdl/pdl_linter.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/pdl/pdl_linter.py b/src/pdl/pdl_linter.py index 06f1be22c..02204ffc5 100644 --- a/src/pdl/pdl_linter.py +++ b/src/pdl/pdl_linter.py @@ -64,6 +64,7 @@ """ import argparse +import ast import logging import sys import tomllib @@ -72,6 +73,9 @@ from pydantic import BaseModel, ConfigDict, Field +from pdl.pdl_ast import BlockType, CodeBlock +from pdl.pdl_ast_utils import iter_block_children +from pdl.pdl_interpreter import EXPR_START_STRING from pdl.pdl_parser import PDLParseError from pdl.pdl_parser import parse_file as parse_pdl_file @@ -364,7 +368,8 @@ def _lint_pdl_file(file_path: Path, config: LinterConfig) -> bool: return True try: - _, _ = parse_pdl_file(file_path) + prog, _ = parse_pdl_file(file_path) + _lint_python_code_blocks(prog.root) logger.info(" - ✅ %s", file_path) return True except PDLParseError as e: @@ -376,6 +381,16 @@ def _lint_pdl_file(file_path: Path, config: LinterConfig) -> bool: return False +def _lint_python_code_blocks(block: BlockType): + match block: + case CodeBlock(lang="python", code=code): + if isinstance(code, str) and EXPR_START_STRING not in code: + # Try to parse the Python code if the code block is + # a string that does not contains a jinja expression + ast.parse(code) + iter_block_children(_lint_python_code_blocks, block) + + def _lint_pdl_files_in_directory( directory: Path, recursive: bool, config: LinterConfig ) -> List[Path]: From fcfc8b3cbaf2557bb406e677426ae3c3d117df69 Mon Sep 17 00:00:00 2001 From: Louis Mandel Date: Fri, 8 Aug 2025 14:46:34 -0400 Subject: [PATCH 2/2] Formatting Signed-off-by: Louis Mandel --- src/pdl/pdl_linter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pdl/pdl_linter.py b/src/pdl/pdl_linter.py index 02204ffc5..51385f07e 100644 --- a/src/pdl/pdl_linter.py +++ b/src/pdl/pdl_linter.py @@ -385,7 +385,7 @@ def _lint_python_code_blocks(block: BlockType): match block: case CodeBlock(lang="python", code=code): if isinstance(code, str) and EXPR_START_STRING not in code: - # Try to parse the Python code if the code block is + # Try to parse the Python code if the code block is # a string that does not contains a jinja expression ast.parse(code) iter_block_children(_lint_python_code_blocks, block)