Repository navigation
Description
Problem
Graphify reports Python import cycles that appear to be driven by imports guarded under if TYPE_CHECKING:. These are type-only imports: they are intentionally not executed at runtime, but Graphify currently records them as ordinary extracted imports / imports_from edges and then includes them in GRAPH_REPORT.md import cycles.
Observed with graphify 0.9.50 / graphifyy 0.9.50 on a FastAPI Python package (packages/bsquant_server):
- Graph size: 3,634 nodes / 10,820 graph edges / 143 communities.
GRAPH_REPORT.mdlists multiple import cycles involvingsrc/bsquant_server/main.py.- Several cycle-closing edges are generated from
TYPE_CHECKINGblocks that importAppStateonly for annotations.
Examples from the generated report:
- 3-file cycle: `src/bsquant_server/main.py -> src/bsquant_server/services/backtest_service.py -> src/bsquant_server/services/backtest_data_adapter.py -> src/bsquant_server/main.py`
- 3-file cycle: `src/bsquant_server/main.py -> src/bsquant_server/runtime.py -> src/bsquant_server/services/component_registration.py -> src/bsquant_server/main.py`
- 3-file cycle: `src/bsquant_server/api/routers/components.py -> src/bsquant_server/main.py -> src/bsquant_server/api/routes.py -> src/bsquant_server/api/routers/components.py`
But the source imports that close those cycles are type-only:
from typing import TYPE_CHECKING, Any, Callable, Iterator
if TYPE_CHECKING:
from bsquant_server.main import AppStateSimilar examples:
# services/component_registration.py
from typing import TYPE_CHECKING, Any, Callable, Protocol
if TYPE_CHECKING:
from bsquant_server.core.auth import UserContext
from bsquant_server.main import AppState# core/state.py
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bsquant_server.main import AppStateIn graphify-out/graph.json, these are emitted as ordinary extracted import edges, for example:
EDGE imports / imports_from
source: src/bsquant_server/services/backtest_data_adapter.py
edge_loc: src/bsquant_server/services/backtest_data_adapter.py L18
target: AppState / src/bsquant_server/main.py
confidence: EXTRACTED
context: import
Why this matters
Treating type-only imports as runtime import edges makes architecture reports harder to trust:
GRAPH_REPORT.mdflags cycles that may not exist in Python runtime import order.- Composition-root modules like
main.pylook more cyclic than they actually are. - Refactoring work may get directed toward harmless annotation dependencies instead of real runtime coupling.
- The graph loses the distinction between "this file imports at runtime" and "this file mentions a type for static analysis only".
Expected behavior
Graphify should preserve or downweight this distinction. Possible approaches:
- Do not include imports under
if TYPE_CHECKING:in runtime import-cycle analysis. - Emit them with a distinct relation such as
type_imports/typing_dependencyinstead ofimports/imports_from. - Keep them in the graph, but mark metadata such as
type_only=trueand exclude them from runtime cycle warnings. - If a report includes type-only cycles, label them separately from runtime import cycles.
Minimal reproduction sketch
# app.py
from service import Service
class AppState:
service: Service# service.py
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from app import AppState
class Service:
def use(self, state: "AppState") -> None:
passRun:
graphify .service.py -> app.py should not be reported as a runtime import-cycle edge.
Acceptance criteria
- Imports guarded by
if TYPE_CHECKING:are represented distinctly from runtime imports. - Runtime import-cycle reporting excludes type-only import edges by default.
- Graph/query/path output still has a way to surface annotation/type dependencies when useful.
- Add a regression test using a two-file Python example where
TYPE_CHECKINGimports would otherwise create a false cycle.
Activity
oohara-nan commentedon Aug 29, 2026
Hi , I'd like to work on this issue.
I've reproduced the issue locally and traced the cycle detection code.
The two-file repro doesn't trigger the cycle detection as written , so I created a 3 file repro that produces the expected false file level case.
My proposed fix is to tag TYPE_CHECKING imports with type_only: True and exclude them from find_import_cycles, following the existing deferred import pattern .
Happy to open the pr once the regression test is passing
fix(python): keep TYPE_CHECKING-only imports out of import cycles (#3159
Merge upstream/v8 (v0.9.80) into chore/sync-upstream-0980
Merge upstream/v8 (v0.9.80) into consolidated/php-support-1682
omsenjalia commentedon Oct 18, 2026
This looks fixed on the
v8branch by 12926393 (fix(python): keep TYPE_CHECKING-only imports out of import cycles (#3159)). Could a maintainer confirm and close?