33import asyncio
44import contextlib
55from collections .abc import AsyncIterator
6- from typing import Self , TypeVar , cast
6+ from typing import Self , TypeVar
77
88from reflex .constants import ROUTER_DATA
99from reflex .event import Event , get_hydrate_event
@@ -190,11 +190,14 @@ async def _link_to(self, token: str) -> Self:
190190 if not token :
191191 msg = "Cannot link shared state to empty token."
192192 raise ReflexRuntimeError (msg )
193- if self ._linked_to == token : # pyright: ignore[reportAttributeAccessIssue]
193+ if not isinstance (self , SharedState ):
194+ msg = "Can only link SharedState instances."
195+ raise RuntimeError (msg )
196+ if self ._linked_to == token :
194197 return self # already linked to this token
195- if self ._linked_to and self ._linked_to != token : # pyright: ignore[reportAttributeAccessIssue]
198+ if self ._linked_to and self ._linked_to != token :
196199 # Disassociate from previous linked token since unlink will not be called.
197- self ._linked_from .discard (self .router .session .client_token ) # pyright: ignore[reportAttributeAccessIssue]
200+ self ._linked_from .discard (self .router .session .client_token )
198201 # TODO: Change StateManager to accept token + class instead of combining them in a string.
199202 if "_" in token :
200203 msg = f"Invalid token { token } for linking state { self .get_full_name ()} , cannot use underscore (_) in the token name."
@@ -215,6 +218,10 @@ async def _unlink(self):
215218 """
216219 from reflex .istate .manager import get_state_manager
217220
221+ if not isinstance (self , SharedState ):
222+ msg = "Can only unlink SharedState instances."
223+ raise ReflexRuntimeError (msg )
224+
218225 state_name = self .get_full_name ()
219226 if (
220227 not self ._reflex_internal_links
@@ -225,7 +232,7 @@ async def _unlink(self):
225232
226233 # Break the linkage for future events.
227234 self ._reflex_internal_links .pop (state_name )
228- self ._linked_from .discard (self .router .session .client_token ) # pyright: ignore[reportAttributeAccessIssue]
235+ self ._linked_from .discard (self .router .session .client_token )
229236
230237 # Patch in the original state, apply updates, then rehydrate.
231238 private_root_state = await get_state_manager ().get_state (
@@ -271,7 +278,10 @@ async def _internal_patch_linked_state(
271278 linked_root_state = await get_state_manager ().get_state (
272279 _substate_key (token , type (self ))
273280 )
274- linked_state = cast (SharedState , await linked_root_state .get_state (type (self )))
281+ linked_state = await linked_root_state .get_state (type (self ))
282+ if not isinstance (linked_state , SharedState ):
283+ msg = f"Linked state for token { token } is not a SharedState."
284+ raise ReflexRuntimeError (msg )
275285 # Avoid unnecessary dirtiness of shared state when there are no changes.
276286 if type (self ) not in self ._held_locks [token ]:
277287 self ._held_locks [token ][type (self )] = linked_state
@@ -286,7 +296,7 @@ async def _internal_patch_linked_state(
286296 full_delta = full_delta ,
287297 )
288298 )
289- return linked_state # pyright: ignore[reportReturnType]
299+ return linked_state
290300
291301 def _held_locks_linked_states (self ) -> list ["SharedState" ]:
292302 """Get all linked states currently held by this state.
0 commit comments