Skip to content

Enable ruff typing-only import rules (TC) - #3902

Draft
anth-volk wants to merge 2 commits into
masterfrom
ruff-typing-imports
Draft

anth-volk wants to merge 2 commits into
masterfrom
ruff-typing-imports

Conversation

@anth-volk

Copy link
Copy Markdown
Collaborator

Fixes #3901

Summary

Phase 1 step 1.8, group 2: typing-only imports. Stacked on #3898.

  • What changed: select gains TC, and about 400 fixes move imports used only in annotations under TYPE_CHECKING. This removes import-time work and import cycles, without changing runtime behaviour.
  • What stays at runtime: Pydantic and SQLModel models, SQLAlchemy declarative models and FastAPI endpoints all evaluate annotations at runtime, so their imports must stay. [tool.ruff.lint.flake8-type-checking] lists:
    • Runtime-evaluated base classes: pydantic.BaseModel, sqlmodel.SQLModel, sqlalchemy.orm.DeclarativeBase, V1Base, and every project model class that other models inherit from (35). Ruff checks direct bases only, so each must be listed. A new model base that's missing from the list is caught by the test suite, which builds every model and route.
    • Runtime-evaluated decorators: the APIRouter route decorators.
  • Found during testing: the first attempt broke V1Base models, because SQLAlchemy resolves Mapped[...] at class creation. Hence DeclarativeBase and V1Base are on the list.
  • Shared tool: scripts/rules changes are mirrored in sim, so the tool stays byte-identical.

Testing

  • pytest tests/unit tests/contract: 2,358 passed. tests/to_refactor and the in-flight dedupe test: 53 passed, the same as the base branch.
  • mypy is clean. ruff check . passes with 0.15.12 and 0.16.8.
  • Rules tool unittest passes on Python 3.11, 3.12 and 3.14. All guards pass.

🤖 Generated with Claude Code

@anth-volk
anth-volk added this pull request to stack #3888 October 13, 2026 11:39
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 26.25369% with 250 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.64%. Comparing base (172bbed) to head (11c3b1f).

Files with missing lines Patch % Lines
...cyengine_api/services/v2/user_policies/services.py 14.28% 5 Missing and 1 partial ⚠️
...pi/services/v2/comparison_runs/database_session.py 0.00% 5 Missing ⚠️
...olicyengine_api/services/v2/households/services.py 16.66% 4 Missing and 1 partial ⚠️
policyengine_api/services/v2/policies/services.py 16.66% 4 Missing and 1 partial ⚠️
...engine_api/services/v2/policies/transformations.py 16.66% 4 Missing and 1 partial ⚠️
...engine_api/services/v2/user_households/services.py 16.66% 4 Missing and 1 partial ⚠️
policyengine_api/asgi_factory.py 20.00% 3 Missing and 1 partial ⚠️
policyengine_api/data/v2/catalog/records.py 20.00% 3 Missing and 1 partial ⚠️
policyengine_api/data/v2/report_runs.py 20.00% 3 Missing and 1 partial ⚠️
policyengine_api/fastapi_routes/dependencies.py 0.00% 4 Missing ⚠️
... and 79 more
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3902       +/-   ##
===========================================
+ Coverage   45.27%   85.64%   +40.36%     
===========================================
  Files         183      198       +15     
  Lines       11084    12355     +1271     
  Branches     1907     2247      +340     
===========================================
+ Hits         5018    10581     +5563     
+ Misses       5644     1093     -4551     
- Partials      422      681      +259     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anth-volk
anth-volk force-pushed the ruff-typing-imports branch from b71530a to 5d3461e Compare October 13, 2026 16:56
@anth-volk
anth-volk removed this pull request from stack #3888 October 13, 2026 19:26
@anth-volk
anth-volk added this pull request to stack #3917 October 13, 2026 19:26
@anth-volk
anth-volk force-pushed the ruff-typing-imports branch from 5d3461e to e950a77 Compare October 14, 2026 13:52
@anth-volk
anth-volk force-pushed the ruff-typing-imports branch 2 times, most recently from a59bb82 to c354d5f Compare October 14, 2026 16:59
@anth-volk
anth-volk force-pushed the ruff-typing-imports branch 3 times, most recently from e7aff0c to 6b8a90a Compare October 14, 2026 20:04
@anth-volk
anth-volk force-pushed the ruff-typing-imports branch 4 times, most recently from 5035c82 to cd21647 Compare October 14, 2026 22:29
Base automatically changed from ruff-safe-autofixes to master October 14, 2026 22:55
@anth-volk
anth-volk force-pushed the ruff-typing-imports branch from cd21647 to e0a9e49 Compare October 14, 2026 22:56
anth-volk and others added 2 commits October 15, 2026 04:05
Select TC and move imports used only in annotations under TYPE_CHECKING.
Pydantic, SQLModel and SQLAlchemy evaluate model annotations at runtime,
and FastAPI evaluates endpoint annotations, so those imports stay at
runtime: pyproject.toml lists the runtime-evaluated base classes (every
project model base, since ruff only checks direct bases) and the
APIRouter decorators. scripts/rules changes are mirrored in
policyengine-sim-api.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
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.

Enable ruff typing-only import rules (TC)

1 participant