Skip to content

Conversation

@marcorudolphflex
Copy link
Contributor

@marcorudolphflex marcorudolphflex commented Dec 16, 2025

  • adds script for auto-detecting moving from type-checking imports with fix and check mode
  • use fix mode for pre-commit hook
  • use check mode for CI

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_CHECKING guards, improving runtime import performance and reducing circular import issues.

Changes:

  • Created scripts/move_type_imports.py - AST/libcst-based script that detects type-only imports and moves them behind if TYPE_CHECKING: blocks
  • Pre-commit hook runs in --mode fix --only-changed to automatically fix type imports on changed files before commit
  • CI check runs in --mode check_on_change on all files to catch any unguarded type-only imports
  • Example fix in tidy3d/web/tests/conftest.py showing Generator moved to TYPE_CHECKING block

The script intelligently handles class-level annotations (which need runtime imports for pydantic), re-exported symbols, and provides a # noqa: TC escape hatch.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are well-structured infrastructure improvements with clear separation of concerns. The script has appropriate safeguards (class-level annotation handling, reexport detection), the pre-commit hook uses fix mode on changed files only, and CI validates all files. The implementation is defensive and provides escape hatches.
  • No files require special attention

Important Files Changed

Filename Overview
scripts/move_type_imports.py New utility script to automatically move type-only imports behind TYPE_CHECKING guards. Well-structured code using AST and libcst parsing.
.pre-commit-config.yaml Added pre-commit hook to automatically fix type-only imports in changed files before commit.
.github/workflows/tidy3d-python-client-tests.yml Added CI job to check type-only imports are properly guarded, runs on all files in check mode.
tidy3d/web/tests/conftest.py Example fix: moved Generator import behind TYPE_CHECKING guard as it's only used in type annotations.

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
Loading

Base automatically changed from FXC-4545-move-type-hints-imports-behind-type-checking to yaugenst-flex/pydantic-v2 December 16, 2025 08:14
@marcorudolphflex marcorudolphflex force-pushed the FXC-4559-add-type-checking-import-moving-and-checking-to-pre-commit-and-ci branch 3 times, most recently from 96baf60 to 0157641 Compare December 16, 2025 08:59
@marcorudolphflex
Copy link
Contributor Author

@greptile

Copy link

@greptile-apps greptile-apps bot left a 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

Edit Code Review Agent Settings | Greptile

@marcorudolphflex marcorudolphflex force-pushed the FXC-4559-add-type-checking-import-moving-and-checking-to-pre-commit-and-ci branch from 0157641 to d80cef2 Compare December 16, 2025 09:48
@marcorudolphflex marcorudolphflex marked this pull request as ready for review December 16, 2025 09:48
Copy link

@greptile-apps greptile-apps bot left a 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

Edit Code Review Agent Settings | Greptile

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
Copy link
Collaborator

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?

Copy link
Collaborator

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?

Copy link
Contributor Author

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 ]

Copy link
Collaborator

@daquinteroflex daquinteroflex Dec 16, 2025

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?

Copy link
Contributor Author

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...

Comment on lines +6 to +9
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.
Copy link
Collaborator

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

@marcorudolphflex marcorudolphflex force-pushed the FXC-4559-add-type-checking-import-moving-and-checking-to-pre-commit-and-ci branch from d80cef2 to 61fe5ad Compare January 5, 2026 17:40
@greptile-apps
Copy link

greptile-apps bot commented Jan 5, 2026

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants