Skip to content

Commit 2d92c5c

Browse files
authored
feat(pre-commit): add internal-import alias check with auto-fix (#49)
- Add an AST-based hook enforcing code style guide §3.3: public modules must alias symbols imported from a private module with a `_` prefix; private modules must not - On commit it fails and reports each violation with a suggested fix, but does not edit files — run the script with `--fix` to apply fixes manually (kept opt-in so its rewrites don't get entangled with the formatter/linter hooks) - Detect absolute, relative, and aliased plain imports, plus custom aliases; preserve comments and handle multibyte source via byte offsets; skip cases that are unsafe to rename (shadowed or rebound names) - Add a (before, after) test suite under `tests/devtools/` covering every case - Add the required `_` alias to private-module imports in public modules, drop the redundant `_` alias in private modules, and rename references to match, per code style guide §3.3
1 parent 012f399 commit 2d92c5c

34 files changed

Lines changed: 1382 additions & 485 deletions

.pre-commit-config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,20 @@ repos:
305305
types: [python]
306306
exclude: ^tests/
307307

308+
- repo: local
309+
hooks:
310+
- id: check-internal-import-aliases
311+
name: Check `_`-prefix aliasing of internal imports
312+
description: |
313+
Enforce code_style_guide.md §3.3: public modules must alias symbols
314+
imported from private modules with a `_` prefix; private modules must
315+
not. Fails the commit and reports each violation with a suggested fix;
316+
it does not edit files — run the script with `--fix` to apply fixes.
317+
entry: python scripts/pre_commit/check_internal_import_aliases.py
318+
language: system
319+
types: [python]
320+
exclude: ^tests/
321+
308322
- repo: local
309323
hooks:
310324
- id: towncrier-check

docs/contributing/code_style_guide.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This guide documents coding conventions and best practices for the Core AI Optim
2121
- [Why this matters](#why-this-matters)
2222
- [Best practice](#best-practice)
2323
- [When to use this pattern](#when-to-use-this-pattern)
24+
- [Enforcement](#enforcement)
2425
- [3.4 Import Examples](#34-import-examples)
2526
- [4. Type Annotations](#4-type-annotations)
2627
- [4.1 When to Annotate](#41-when-to-annotate)
@@ -330,6 +331,17 @@ from proj_pkg.api import private_helper # ✗ AttributeError - not accessible
330331

331332
- When importing project-internal symbols into public modules to prevent unintended re-export
332333

334+
#### Enforcement
335+
336+
The pre-commit hook `check-internal-import-aliases` (defined in `.pre-commit-config.yaml`, implemented at `scripts/pre_commit/check_internal_import_aliases.py`) enforces this rule. On commit it **fails** when it finds a violation and prints each one with a suggested fix, but it does not edit files — so the commit is blocked until the imports are corrected. Fix them by hand, or run the script with `--fix` to apply the fixable ones automatically:
337+
338+
- Add the missing `_` alias in public modules.
339+
- Remove the unnecessary `_` alias in private modules.
340+
- Rename every reference to the bound name in the same file so the rewrite leaves the module compiling.
341+
- Report (without fixing) when the bound name is shadowed inside a function scope (rare) — resolve those by hand.
342+
343+
Fixing is a separate, opt-in step rather than an automatic commit-time rewrite: auto-editing on commit would interleave this hook's changes with those of the formatting/linting hooks, making them hard to review or revert. Run `python scripts/pre_commit/check_internal_import_aliases.py --fix` (optionally with specific paths) to fix everything in one go, then review and commit the changes.
344+
333345
### 3.4 Import Examples
334346

335347
**Importing from Public API:**

0 commit comments

Comments
 (0)