Skip to content

feat: check Python syntax in the linter #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/pdl/pdl_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"""

import argparse
import ast
import logging
import sys
import tomllib
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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]:
Expand Down
Loading