Skip to content

make reflex state import dynamic#6258

Merged
adhami3310 merged 2 commits intomainfrom
make-reflex-state-import-dynamic
Mar 31, 2026
Merged

make reflex state import dynamic#6258
adhami3310 merged 2 commits intomainfrom
make-reflex-state-import-dynamic

Conversation

@adhami3310
Copy link
Copy Markdown
Member

No description provided.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 31, 2026

Greptile Summary

This PR fixes a circular import between reflex/state.py and reflex/istate/manager/__init__.py. reflex/state.py imports StateManager and related symbols from reflex.istate.manager at the very bottom of the module (lines 2868–2872), while reflex/istate/manager/__init__.py was previously importing BaseState from reflex.state at the top level — creating a cycle. The fix:

  • Adds from __future__ import annotations so all annotations in the file become lazy strings (PEP 563), meaning BaseState is never evaluated at runtime.
  • Guards the from reflex.state import BaseState import with TYPE_CHECKING, so it is only visible to static analysis tools and not executed at runtime.

The approach is correct and safe: every usage of BaseState in this file is in a type annotation position (state: type[BaseState], return types, parameter types), which with from __future__ import annotations are stored as strings and not evaluated. The dataclasses.dataclass decorator handles string annotations without resolving them, and the abstract method signatures are unaffected at runtime. The concrete sub-managers (disk.py, memory.py, redis.py) still import BaseState directly, but since they are only imported lazily inside StateManager.create(), they do not participate in the circular import.

Confidence Score: 5/5

  • Safe to merge — minimal, focused change that correctly resolves a circular import with no runtime impact.
  • The change is a well-understood Python pattern (TYPE_CHECKING + from __future__ import annotations) applied correctly. All uses of BaseState in the file are annotation-only; none are evaluated at runtime. The dataclasses.dataclass decorator works correctly with string annotations under PEP 563. No logic changes, no data mutations, no API surface changes.
  • No files require special attention.

Important Files Changed

Filename Overview
reflex/istate/manager/init.py Moves BaseState import under TYPE_CHECKING guard and adds from __future__ import annotations to break the circular import between reflex.state (which imports from reflex.istate.manager at module bottom) and this file (which previously imported BaseState from reflex.state at the top).

Sequence Diagram

sequenceDiagram
    participant state as reflex/state.py
    participant manager as istate/manager/__init__.py
    participant submanager as istate/manager/{disk,memory,redis}.py

    note over state,manager: Before PR — circular import at module level
    state->>manager: import StateManager (lines 2868-2872)
    manager->>state: from reflex.state import BaseState ❌ circular

    note over state,manager: After PR — TYPE_CHECKING guard breaks the cycle
    state->>manager: import StateManager (lines 2868-2872)
    manager-->>state: BaseState only imported during type-checking ✅

    note over manager,submanager: Sub-managers still import BaseState at top level,<br/>but they are only imported lazily inside StateManager.create()
    manager->>submanager: (lazy import inside create())
Loading

Reviews (1): Last reviewed commit: "make reflex state import dynamic" | Re-trigger Greptile

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 31, 2026

Merging this PR will not alter performance

✅ 8 untouched benchmarks


Comparing make-reflex-state-import-dynamic (1896dbc) with main (4e5e227)

Open in CodSpeed

masenf
masenf previously approved these changes Mar 31, 2026
@adhami3310 adhami3310 merged commit 2178184 into main Mar 31, 2026
38 of 40 checks passed
@adhami3310 adhami3310 deleted the make-reflex-state-import-dynamic branch March 31, 2026 00:51
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.

2 participants