Skip to content

Distinguish TYPE_CHECKING imports from runtime import cycles #3159

Open

Description

@SameTrouble

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.md lists multiple import cycles involving src/bsquant_server/main.py.
  • Several cycle-closing edges are generated from TYPE_CHECKING blocks that import AppState only 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 AppState

Similar 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 AppState

In 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.md flags cycles that may not exist in Python runtime import order.
  • Composition-root modules like main.py look 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:

  1. Do not include imports under if TYPE_CHECKING: in runtime import-cycle analysis.
  2. Emit them with a distinct relation such as type_imports / typing_dependency instead of imports / imports_from.
  3. Keep them in the graph, but mark metadata such as type_only=true and exclude them from runtime cycle warnings.
  4. 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:
        pass

Run:

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_CHECKING imports would otherwise create a false cycle.

Activity

  1. oohara-nan commented on Aug 29, 2026

    @oohara-nan

    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

  2. omsenjalia commented on Oct 18, 2026

    @omsenjalia
    Contributor

    This looks fixed on the v8 branch by 12926393 (fix(python): keep TYPE_CHECKING-only imports out of import cycles (#3159)). Could a maintainer confirm and close?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions