1- # ruff: noqa: D100, D101, D102, D103, D104, D105, D107
1+ """Redux store for managing state and side effects."""
22from __future__ import annotations
33
44import dataclasses
@@ -74,13 +74,14 @@ def run(self: _SideEffectRunnerThread[Event]) -> None:
7474
7575
7676class Store (Generic [State , Action , Event ]):
77- custom_serializer = None
77+ """Redux store for managing state and side effects."""
7878
7979 def __init__ (
8080 self : Store [State , Action , Event ],
8181 reducer : ReducerType [State , Action , Event ],
8282 options : CreateStoreOptions | None = None ,
8383 ) -> None :
84+ """Create a new store."""
8485 self .store_options = options or CreateStoreOptions ()
8586 self .reducer = reducer
8687 self ._create_task : Callable [[Coroutine ], Any ] = (
@@ -175,6 +176,7 @@ def _run_event_handlers(self: Store[State, Action, Event]) -> None:
175176 cast (Callable [[], Any ], event_handler )()
176177
177178 def run (self : Store [State , Action , Event ]) -> None :
179+ """Run the store."""
178180 with self ._is_running :
179181 while len (self ._actions ) > 0 or len (self ._events ) > 0 :
180182 if len (self ._actions ) > 0 :
@@ -189,6 +191,7 @@ def dispatch(
189191 with_state : Callable [[State | None ], DispatchParameters [Action , Event ]]
190192 | None = None ,
191193 ) -> None :
194+ """Dispatch actions and/or events."""
192195 if with_state is not None :
193196 self .dispatch (with_state (self ._state ))
194197
@@ -217,6 +220,7 @@ def subscribe(
217220 * ,
218221 keep_ref : bool = True ,
219222 ) -> Callable [[], None ]:
223+ """Subscribe to state changes."""
220224 if keep_ref :
221225 listener_ref = listener
222226 elif inspect .ismethod (listener ):
@@ -234,6 +238,7 @@ def subscribe_event(
234238 * ,
235239 options : EventSubscriptionOptions | None = None ,
236240 ) -> Callable [[], None ]:
241+ """Subscribe to events."""
237242 subscription_options = (
238243 EventSubscriptionOptions () if options is None else options
239244 )
@@ -271,6 +276,8 @@ def autorun(
271276 SelectorOutput ,
272277 AutorunOriginalReturnType ,
273278 ]:
279+ """Create a new autorun, reflecting on state changes."""
280+
274281 def decorator (
275282 func : Callable [[SelectorOutput ], AutorunOriginalReturnType ]
276283 | Callable [[SelectorOutput , SelectorOutput ], AutorunOriginalReturnType ],
@@ -285,26 +292,19 @@ def decorator(
285292
286293 return decorator
287294
288- def set_custom_serializer (
289- self : Store ,
290- serializer : Callable [[object | type ], SnapshotAtom ],
291- ) -> None :
292- """Set a custom serializer for the store snapshot."""
293- self .custom_serializer = serializer
294-
295295 @property
296296 def snapshot (self : Store [State , Action , Event ]) -> SnapshotAtom :
297- return self ._serialize_value (self ._state )
297+ """Return a snapshot of the current state of the store."""
298+ return self .serialize_value (self ._state )
298299
299- def _serialize_value (self : Store , obj : object | type ) -> SnapshotAtom :
300- if self .custom_serializer :
301- return self .custom_serializer (obj )
300+ def serialize_value (self : Store , obj : object | type ) -> SnapshotAtom :
301+ """Serialize a value to a snapshot atom."""
302302 if is_immutable (obj ):
303303 return self ._serialize_dataclass_to_dict (obj )
304304 if isinstance (obj , (list , tuple )):
305- return [self ._serialize_value (i ) for i in obj ]
305+ return [self .serialize_value (i ) for i in obj ]
306306 if callable (obj ):
307- return self ._serialize_value (obj ())
307+ return self .serialize_value (obj ())
308308 if isinstance (obj , StrEnum ):
309309 return str (obj )
310310 if isinstance (obj , IntEnum ):
@@ -320,6 +320,6 @@ def _serialize_dataclass_to_dict(
320320 ) -> dict [str , Any ]:
321321 result = {}
322322 for field in dataclasses .fields (obj ):
323- value = self ._serialize_value (getattr (obj , field .name ))
323+ value = self .serialize_value (getattr (obj , field .name ))
324324 result [field .name ] = value
325325 return result
0 commit comments