Skip to content

Commit 35672e0

Browse files
committed
feat(combine_reducers): initialization of sub-reducers is done with CombineReducerInitAction containing _id instead of normal InitAction
1 parent d6e1783 commit 35672e0

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Version 0.9.23
4+
5+
- feat(combine_reducers): initialization of sub-reducers is done with `CombineReducerInitAction`
6+
containing `_id` instead of normal `InitAction`
7+
38
## Version 0.9.22
49

510
- fix: `CombineReducerRegisterAction` should take care of `CompleteReducerResult`

poetry.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-redux"
3-
version = "0.9.22"
3+
version = "0.9.23"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <sassanh@gmail.com>"]
66
license = "Apache-2.0"
@@ -11,7 +11,6 @@ packages = [{ include = "redux" }]
1111
python = "^3.9"
1212
python-immutable = "^1.0.0"
1313
typing-extensions = "^4.9.0"
14-
pyright = "^1.1.348"
1514

1615

1716
[tool.poetry.scripts]
@@ -23,7 +22,7 @@ optional = true
2322

2423
[tool.poetry.group.dev.dependencies]
2524
poethepoet = "^0.24.3"
26-
pyright = "^1.1.348"
25+
pyright = "^1.1.349"
2726
ruff = "^0.1.9"
2827

2928
[build-system]

redux/combine_reducers.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import copy
55
import uuid
66
from dataclasses import asdict, make_dataclass
7-
from typing import TYPE_CHECKING, Any, TypeGuard, TypeVar, cast
7+
from typing import TYPE_CHECKING, Any, TypeVar, cast
88

99
from .basic_types import (
1010
Action,
@@ -25,37 +25,37 @@ class BaseCombineReducerState(Immutable):
2525
_id: str
2626

2727

28-
class BaseCombineReducerAction(BaseAction):
28+
class CombineReducerAction(BaseAction):
2929
_id: str
3030

3131

32-
class CombineReducerRegisterAction(BaseCombineReducerAction):
32+
class CombineReducerInitAction(CombineReducerAction, InitAction):
33+
key: str
34+
35+
36+
class CombineReducerRegisterAction(CombineReducerAction):
3337
key: str
3438
reducer: ReducerType
3539

3640

37-
class CombineReducerUnregisterAction(BaseCombineReducerAction):
41+
class CombineReducerUnregisterAction(CombineReducerAction):
3842
key: str
3943

4044

41-
CombineReducerAction = CombineReducerRegisterAction | CombineReducerUnregisterAction
4245
CombineReducerState = TypeVar(
4346
'CombineReducerState',
4447
bound=BaseCombineReducerState,
4548
)
4649
AnyAction = TypeVar('AnyAction', bound=BaseAction)
4750

4851

49-
def is_combine_reducer_action(action: BaseAction) -> TypeGuard[CombineReducerAction]:
50-
return isinstance(action, BaseCombineReducerAction)
51-
52-
5352
def combine_reducers(
5453
state_type: type[CombineReducerState],
55-
action_type: type[Action] = BaseAction, # noqa: ARG001
56-
event_type: type[Event] = BaseEvent, # noqa: ARG001
54+
action_type: type[Action] = BaseAction,
55+
event_type: type[Event] = BaseEvent,
5756
**reducers: ReducerType,
5857
) -> tuple[ReducerType[CombineReducerState, Action, Event], str]:
58+
_ = action_type, event_type
5959
reducers = reducers.copy()
6060
_id = uuid.uuid4().hex
6161

@@ -76,7 +76,7 @@ def combined_reducer(
7676
result_actions = []
7777
result_events = []
7878
nonlocal state_class
79-
if state is not None and is_combine_reducer_action(action):
79+
if state is not None and isinstance(action, CombineReducerAction):
8080
if isinstance(action, CombineReducerRegisterAction) and action._id == _id: # noqa: SLF001
8181
key = action.key
8282
reducer = action.reducer
@@ -86,7 +86,10 @@ def combined_reducer(
8686
('_id', *reducers.keys()),
8787
frozen=True,
8888
)
89-
reducer_result = reducer(None, InitAction())
89+
reducer_result = reducer(
90+
None,
91+
CombineReducerInitAction(_id=_id, key=key),
92+
)
9093
state = state_class(
9194
_id=state._id, # noqa: SLF001
9295
**(
@@ -136,7 +139,9 @@ def combined_reducer(
136139
reducers_results = {
137140
key: reducer(
138141
None if state is None else getattr(state, key),
139-
action,
142+
CombineReducerInitAction(key=key, _id=_id)
143+
if isinstance(action, InitAction)
144+
else action,
140145
)
141146
for key, reducer in reducers.items()
142147
}

0 commit comments

Comments
 (0)