-
Notifications
You must be signed in to change notification settings - Fork 68
chore(tidy3d): FXC-4559-add-type-checking-import-moving-and-checking-to-pre-commit-and-ci #3093
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
base: yaugenst-flex/pydantic-v2
Are you sure you want to change the base?
Conversation
96baf60 to
0157641
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 files reviewed, 2 comments
0157641 to
d80cef2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 files reviewed, no comments
| hooks: | ||
| - id: move-type-imports | ||
| name: move type-only imports under TYPE_CHECKING | ||
| entry: poetry run python scripts/move_type_imports.py --mode fix --only-changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we can do this without using poetry? Just because not everyone has it installed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it, should we just avoid it in the precommit but keep it in CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was not expecting that...
Either we might skip it or do this one?
- repo: local
hooks:
- id: move-type-imports
name: move type-only imports under TYPE_CHECKING
entry: python scripts/move_type_imports.py --mode fix --only-changed
language: python
additional_dependencies:
- "libcst==1.4.0"
pass_filenames: false
stages: [ pre-commit ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this will work for people who don't even have python installed in the system? Just checking how standalone it is? Is it fully self contained?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only requires python with the libcst package.
So it's not perfectly standalone, but I would not assume that these users use the pre-commit hook? I mean there is no force to do it...
| The script scans module-level imports and moves those referenced only in | ||
| annotations or existing ``if TYPE_CHECKING`` blocks into a consolidated | ||
| ``if TYPE_CHECKING:`` section. Imports used in class-level annotations are | ||
| left in place to avoid breaking pydantic field evaluation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this is part of a wider conversation about how we do dependency managements and typing. For now sounds good to use it but in the long run we can evaluate how we do this between the planned packages
5293ae1 to
6f85dd4
Compare
6cc521f to
95adf41
Compare
2cef17c to
61fe5ad
Compare
d80cef2 to
61fe5ad
Compare
…to-pre-commit-and-ci
Greptile's behavior is changing!From now on, if a review finishes with no comments, we will not post an additional "statistics" comment to confirm that our review found nothing to comment on. However, you can confirm that we reviewed your changes in the status check section. This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR". |
looks good (see move-type-imports):
https://github.com/flexcompute/tidy3d/actions/runs/20262172636/job/58176641591
Greptile Summary
This PR adds automated tooling to enforce that imports used only for type annotations are placed behind
TYPE_CHECKINGguards, improving runtime import performance and reducing circular import issues.Changes:
scripts/move_type_imports.py- AST/libcst-based script that detects type-only imports and moves them behindif TYPE_CHECKING:blocks--mode fix --only-changedto automatically fix type imports on changed files before commit--mode check_on_changeon all files to catch any unguarded type-only importstidy3d/web/tests/conftest.pyshowingGeneratormoved to TYPE_CHECKING blockThe script intelligently handles class-level annotations (which need runtime imports for pydantic), re-exported symbols, and provides a
# noqa: TCescape hatch.Confidence Score: 5/5
Important Files Changed
Sequence Diagram
sequenceDiagram participant Dev as Developer participant PreCommit as Pre-commit Hook participant Script as move_type_imports.py participant CI as CI Workflow Dev->>PreCommit: git commit PreCommit->>Script: Run in fix mode (--only-changed) Script->>Script: Analyze changed files Script->>Script: Move type-only imports to TYPE_CHECKING Script->>PreCommit: Auto-fix applied PreCommit->>Dev: Commit proceeds Dev->>CI: Push to PR CI->>Script: Run in check_on_change mode (all files) Script->>Script: Scan all tidy3d files Script->>Script: Check for unguarded type-only imports alt Found unguarded imports Script->>CI: Exit 1 (fail) CI->>Dev: ❌ CI fails with error message else All imports guarded Script->>CI: Exit 0 (success) CI->>Dev: ✅ CI passes end