-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(autorun): remove
auto_call
option as it was addressing suc…
…h a rare use case that it was not worth the complexity
- Loading branch information
Showing
8 changed files
with
110 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "python-redux" | ||
version = "0.16.1" | ||
version = "0.17.0" | ||
description = "Redux implementation for Python" | ||
authors = ["Sassan Haradji <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# ruff: noqa: D100, D101, D102, D103, D104, D107 | ||
from __future__ import annotations | ||
|
||
from dataclasses import replace | ||
from typing import Literal | ||
|
||
import pytest | ||
from immutable import Immutable | ||
|
||
from redux.basic_types import ( | ||
BaseAction, | ||
CompleteReducerResult, | ||
CreateStoreOptions, | ||
FinishAction, | ||
FinishEvent, | ||
InitAction, | ||
InitializationActionError, | ||
) | ||
from redux.main import Store | ||
|
||
|
||
class StateType(Immutable): | ||
value1: int | ||
value2: int | ||
|
||
|
||
class IncrementAction(BaseAction): | ||
which: Literal[1, 2] | ||
|
||
|
||
Action = IncrementAction | InitAction | FinishAction | ||
|
||
|
||
def reducer( | ||
state: StateType | None, | ||
action: Action, | ||
) -> StateType | CompleteReducerResult[StateType, Action, FinishEvent]: | ||
if state is None: | ||
if isinstance(action, InitAction): | ||
return StateType(value1=0, value2=0) | ||
raise InitializationActionError(action) | ||
|
||
if isinstance(action, IncrementAction): | ||
field_name = f'value{action.which}' | ||
return replace( | ||
state, | ||
**{field_name: getattr(state, field_name) + 1}, | ||
) | ||
|
||
return state | ||
|
||
|
||
StoreType = Store[StateType, Action, FinishEvent] | ||
|
||
|
||
@pytest.fixture | ||
def store() -> StoreType: | ||
return Store(reducer, options=CreateStoreOptions(auto_init=True)) | ||
|
||
|
||
def test_autorun_of_view(store: StoreType) -> None: | ||
@store.autorun( | ||
lambda state: state.value2, | ||
lambda state: (state.value1, state.value2), | ||
) | ||
@store.view(lambda state: state.value1) | ||
def view(value1: int, value2: int) -> tuple[int, int]: | ||
return (value1, value2) | ||
|
||
assert view() == (0, 0) | ||
|
||
store.dispatch(IncrementAction(which=1)) | ||
|
||
assert view() == (1, 0) | ||
|
||
store.dispatch(IncrementAction(which=2)) | ||
|
||
assert view() == (1, 1) | ||
|
||
store.dispatch(FinishAction()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters