Skip to content

Commit 546527c

Browse files
committed
refactor: remove WithState as it wasn't doing anything beyond functools.wraps
1 parent 28e5e5b commit 546527c

File tree

8 files changed

+43
-119
lines changed

8 files changed

+43
-119
lines changed

CHANGELOG.md

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

3+
## Upcoming
4+
5+
- refactor: remove `WithState` as it wasn't doing anything beyond `functools.wraps`
6+
37
## Version 0.22.2
48

59
- fix: add `__qualname__`, `__annotations__`, `__module__`, `__defaults__` and `__kwdefaults__` to `Autorun` and `WithStore` instances so that they play nice when passed as a function to something assuming they are normal function having these properties.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ authors = [{ name = "Sassan Haradji", email = "[email protected]" }]
77
maintainers = [{ name = "Sassan Haradji", email = "[email protected]" }]
88
readme = "README.md"
99
requires-python = ">=3.11"
10-
keywords = ['store', 'redux', 'reactive', 'autorun']
10+
keywords = ['python', 'store', 'redux', 'reactive', 'autorun', 'view']
1111
dependencies = ["python-immutable >= 1.1.1", "python-strtobool >= 1.0.0"]
1212

1313
[tool.uv]
1414
dev-dependencies = [
1515
"poethepoet >= 0.24.4",
16-
"pyright >= 1.1.397",
16+
"pyright >= 1.1.399",
1717
"ruff >= 0.11.0",
1818
"pytest >= 8.1.1",
1919
"pytest-cov >= 4.1.0",
@@ -85,7 +85,7 @@ exclude = ['typings', '.venv']
8585
filterwarnings = 'error'
8686

8787
[tool.pytest.ini_options]
88-
log_cli = 1
88+
log_cli = true
8989
log_cli_level = 'ERROR'
9090
timeout = 1
9191

redux/main.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import queue
88
import weakref
99
from collections import defaultdict
10+
from functools import wraps
1011
from threading import Lock, Thread
1112
from typing import (
1213
TYPE_CHECKING,
@@ -59,7 +60,6 @@
5960
)
6061
from redux.serialization_mixin import SerializationMixin
6162
from redux.side_effect_runner import SideEffectRunnerThread
62-
from redux.with_state import WithState
6363

6464
if TYPE_CHECKING:
6565
from collections.abc import Awaitable, Callable
@@ -442,7 +442,26 @@ def with_state_decorator(
442442
ReturnType,
443443
],
444444
) -> Callable[Args, ReturnType]:
445-
return WithState(store=self, selector=selector, func=cast('Callable', func))
445+
def wrapper(*args: Args.args, **kwargs: Args.kwargs) -> ReturnType:
446+
if self._state is None:
447+
msg = 'Store has not been initialized yet.'
448+
raise RuntimeError(msg)
449+
return func(selector(self._state), *args, **kwargs)
450+
451+
signature = inspect.signature(func)
452+
parameters = list(signature.parameters.values())
453+
if parameters and parameters[0].kind in [
454+
inspect.Parameter.POSITIONAL_ONLY,
455+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
456+
]:
457+
parameters = parameters[1:]
458+
459+
wrapped = wraps(func)(wrapper)
460+
wrapped.__signature__ = signature.replace( # pyright: ignore [reportAttributeAccessIssue]
461+
parameters=parameters,
462+
)
463+
464+
return wrapped
446465

447466
return with_state_decorator
448467

redux/with_state.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

redux_pytest/fixtures/snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
if TYPE_CHECKING:
1818
from collections.abc import Callable
1919

20-
from _pytest.fixtures import SubRequest
20+
from _pytest.fixtures import SubRequest # pyright: ignore[reportPrivateImportUsage]
2121

2222
from redux.main import Store
2323

tests/test_wait_for_fixture.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# ruff: noqa: D100, D101, D102, D103, D104, D107
22
import pytest
3-
from _pytest.outcomes import Failed
43
from tenacity import RetryError, stop_after_delay
54
from tenacity.wait import wait_fixed
65

@@ -15,9 +14,9 @@ def test_asyncheonous(
1514
async def runner() -> None:
1615
@wait_for(run_async=True, timeout=1)
1716
def check() -> None:
18-
pytest.fail('Never')
17+
raise RuntimeError
1918

20-
with pytest.raises(Failed, match=r'^Never$'):
19+
with pytest.raises(RetryError):
2120
await check()
2221

2322
event_loop.stop()
@@ -50,16 +49,16 @@ def check(min_value: int) -> None:
5049
def test_with_stop(wait_for: WaitFor) -> None:
5150
@wait_for(stop=stop_after_delay(0.1))
5251
def check() -> None:
53-
pytest.fail('Never')
52+
raise RuntimeError
5453

55-
with pytest.raises(Failed, match=r'^Never$'):
54+
with pytest.raises(RetryError):
5655
check()
5756

5857

5958
def test_with_timeout(wait_for: WaitFor) -> None:
6059
@wait_for(timeout=0.1)
6160
def check() -> None:
62-
pytest.fail('Never')
61+
raise RuntimeError
6362

64-
with pytest.raises(Failed, match=r'^Never$'):
63+
with pytest.raises(RetryError):
6564
check()

tests/test_with_state.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,14 @@ def test_name_attr(store: StoreType) -> None:
6363
def decorated(value: int) -> int:
6464
return value
6565

66-
assert decorated.__name__ == 'WithState:decorated'
67-
assert decorated.__qualname__ == 'WithState:test_name_attr.<locals>.decorated'
66+
assert decorated.__name__ == 'decorated'
67+
assert decorated.__qualname__ == 'test_name_attr.<locals>.decorated'
6868

6969
inline_decorated = store.with_state(lambda state: state.value)(
7070
lambda value: value,
7171
)
7272

73-
assert inline_decorated.__name__ == 'WithState:<lambda>'
74-
75-
class Decorated:
76-
def __call__(self, value: int) -> int:
77-
return value
78-
79-
def __repr__(self) -> str:
80-
return 'Decorated'
81-
82-
decorated_instance = store.with_state(lambda state: state.value)(Decorated())
83-
84-
assert decorated_instance.__name__ == 'WithState:Decorated'
73+
assert inline_decorated.__name__ == '<lambda>'
8574

8675
store.dispatch(InitAction())
8776
store.dispatch(FinishAction())
@@ -95,7 +84,7 @@ def func(value: int) -> int:
9584
return value
9685

9786
assert re.match(
98-
r'.*\(func: <function test_repr\.<locals>\.func at .*>\)$',
87+
r'.*<function test_repr\.<locals>\.func at .*>$',
9988
repr(func),
10089
)
10190

uv.lock

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

0 commit comments

Comments
 (0)