Skip to content

Conversation

@adhami3310
Copy link
Member

No description provided.

@codspeed-hq
Copy link

codspeed-hq bot commented Dec 22, 2025

CodSpeed Performance Report

Merging #6064 will not alter performance

Comparing improve-self-aenter-typing (7348eb9) with main (f4561ec)

Summary

✅ 8 untouched

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 22, 2025

Greptile Summary

This PR improves type checking for async context managers by changing the return type of __aenter__ methods from concrete types (StateProxy, BaseState) to Self from typing_extensions.

Key changes:

  • StateProxy.__aenter__ now returns Self instead of StateProxy, ensuring type checkers understand that subclasses like ReadOnlyStateProxy return their own type when used as context managers
  • BaseState.__aenter__ now returns Self instead of BaseState, ensuring type checkers preserve the specific state subclass type through async with blocks

This is a pure typing improvement with no runtime behavior changes. The Self type is already imported in both files, making this a safe and straightforward enhancement to type safety.

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • This is a pure typing annotation change with no runtime behavior modifications. Both Self imports already exist in the files, the change correctly improves type inference for context managers, and there are no custom rule violations or logical issues.
  • No files require special attention

Important Files Changed

Filename Overview
reflex/istate/proxy.py Changed StateProxy.__aenter__ return type from StateProxy to Self for better type inference
reflex/state.py Changed BaseState.__aenter__ return type from BaseState to Self for better type inference

Sequence Diagram

sequenceDiagram
    participant BT as Background Task
    participant SP as StateProxy/BaseState Instance
    participant TC as Type Checker
    
    Note over BT,TC: Before (returns concrete type)
    BT->>SP: async with self:
    SP->>SP: __aenter__() returns StateProxy
    SP->>TC: Type: StateProxy (loses subclass info)
    BT->>SP: self.some_method()
    TC-->>BT: Type error if method only on subclass
    
    Note over BT,TC: After (returns Self)
    BT->>SP: async with self:
    SP->>SP: __aenter__() returns Self
    SP->>TC: Type: Preserves actual type (ReadOnlyStateProxy, CustomState, etc)
    BT->>SP: self.some_method()
    TC-->>BT: Correctly typed based on actual class
Loading

@adhami3310 adhami3310 merged commit 3c82a7e into main Dec 22, 2025
48 checks passed
@adhami3310 adhami3310 deleted the improve-self-aenter-typing branch December 22, 2025 21:44
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