Skip to content

Commit c7ba418

Browse files
authored
Merge pull request #3 from AzisK/Switch-Python-parsing-to-AST
Switch python parsing to ast ### Changed - **Switched to AST-based Python parsing**: Replaced regex-based string extraction with Python's `ast` module. - **Improved Robustness**: Eliminated edge cases where comments or non-assignment strings were incorrectly formatted. - **Enhanced F-string support**: More reliable parsing of SQL within f-strings. - **False Positive Prevention**: Correctly ignores SQL-like text in docstrings and comments, which were previously matched by the regex. - **Strict Targeting**: Only formats strings that are actual variable assignments. - **Modernized File Handling**: Adopted `pathlib` for all file system operations. - **Type Safety**: Added comprehensive type hints to the codebase.
2 parents bc71f1f + 4695f65 commit c7ba418

9 files changed

Lines changed: 527 additions & 79 deletions

File tree

.github/workflows/ci.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
4848
lint:
4949
name: Lint
50-
runs-on: ubuntu-latest
50+
runs-on: ubuntu-slim
5151

5252
steps:
5353
- name: Checkout repository
@@ -65,3 +65,22 @@ jobs:
6565
run: |
6666
uv pip install pre-commit
6767
uv run pre-commit run --all-files --show-diff-on-failure
68+
69+
type-check:
70+
name: Type Check
71+
runs-on: ubuntu-slim
72+
73+
steps:
74+
- name: Checkout repository
75+
uses: actions/checkout@v5
76+
77+
- name: Install uv
78+
uses: astral-sh/setup-uv@v7
79+
with:
80+
enable-cache: true
81+
82+
- name: Install dependencies
83+
run: uv sync --all-extras
84+
85+
- name: Run mypy
86+
run: uv run mypy .

AGENTS.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ uv run readsql file1.sql file2.py folder/ # Process multiple files
2222
# Run tests
2323
uv run pytest -v
2424

25-
# Pre-commit (uses black with -S flag, zimports)
25+
# Type Check (MyPy)
26+
uv run mypy .
27+
28+
# Pre-commit (uses black with -S flag, ruff)
2629
pre-commit run --all-files
2730
```
2831

@@ -36,7 +39,7 @@ The CLI accepts one or multiple files/folders as input. When given a folder, it
3639
1. `parse_args()` - Handles CLI arguments (`-s` for string mode, `-n` for dry-run, `-py` for custom Python variable names)
3740
2. For strings: `read_replace()` applies regex substitutions directly
3841
3. For files: `read_file()` dispatches to `read_sql_file()` or `read_python_file()` based on extension
39-
4. `read_python_file()` extracts SQL from variable assignments (default: `query`) using regex, then applies `read_replace()`
42+
4. `read_python_file()` uses `ast` module to safely extract SQL from variable assignments (default: `query`) while preserving formatting, then applies `read_replace()`
4043

4144
### Regex Rules (`readsql/regexes.txt`)
4245
Format: `SUBSTITUTE__REGEX__GROUP` (double underscore delimited)

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.1.0] - 2026-01-22
6+
7+
### Changed
8+
9+
- **Switched to AST-based Python parsing**: Replaced regex-based string extraction with Python's `ast` module.
10+
- **Improved Robustness**: Eliminated edge cases where comments or non-assignment strings were incorrectly formatted.
11+
- **Enhanced F-string support**: More reliable parsing of SQL within f-strings.
12+
- **False Positive Prevention**: Correctly ignores SQL-like text in docstrings and comments, which were previously matched by the regex.
13+
- **Strict Targeting**: Only formats strings that are actual variable assignments.
14+
- **Modernized File Handling**: Adopted `pathlib` for all file system operations.
15+
- **Type Safety**: Added comprehensive type hints to the codebase.
16+
517
## [1.0.0] - 2026-01-21
618

719
### Changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ We can look for different strings in Python files with `-py` arguments
4747
readsql <FILE_OR_FOLDER_PATH> -py <PY_VAR1> <PY_VAR2>
4848
```
4949

50+
> **Note**: `readsql` uses Python's AST parser, so it supports:
51+
> - **Type Hints**: `query: str = "..."`
52+
> - **F-strings**: `f"SELECT * FROM {table}"`
53+
> - **Nested Scopes**: Robustly handles variables inside functions and classes.
54+
> - **Comments/Docstrings**: Correctly ignores SQL-like text in comments or docstrings.
55+
5056
# Usage examples
5157

5258
1. `readsql 'select sushi from tokyo' -s` command returns

pyproject.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "readsql"
3-
version = "1.0.0"
3+
version = "1.1.0"
44
description = "Convert SQL to most human readable format"
55
authors = [{ name = "Azis", email = "azuolas.krusna@yahoo.com" }]
66
maintainers = [{ name = "Azis", email = "azuolas.krusna@yahoo.com" }]
@@ -50,4 +50,14 @@ exclude = ["tests/*_example*.py"]
5050
[dependency-groups]
5151
dev = [
5252
"ruff>=0.14.13",
53+
"mypy>=1.0.0",
5354
]
55+
56+
[tool.mypy]
57+
python_version = "3.9"
58+
strict = true
59+
ignore_missing_imports = true
60+
61+
[[tool.mypy.overrides]]
62+
module = "tests.*"
63+
ignore_errors = true

0 commit comments

Comments
 (0)