Skip to content

Commit 9062a50

Browse files
committed
fix: add __name__ to WithStore instances so that they play nice when passed as a function to something assuming they are functions and have a __name__ attribute
1 parent e2ffd03 commit 9062a50

File tree

7 files changed

+51
-43
lines changed

7 files changed

+51
-43
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+
- fix: add `__name__` to `WithStore` instances so that they play nice when passed as a function to something assuming they are functions and have a `__name__` attribute
6+
37
## Version 0.22.0
48

59
- test: make sure pytest exits completely after running async tests

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ dependencies = ["python-immutable >= 1.1.1", "python-strtobool >= 1.0.0"]
1313
[tool.uv]
1414
dev-dependencies = [
1515
"poethepoet >= 0.24.4",
16-
"pyright >= 1.1.396",
17-
"ruff >= 0.10.0",
16+
"pyright >= 1.1.397",
17+
"ruff >= 0.11.0",
1818
"pytest >= 8.1.1",
1919
"pytest-cov >= 4.1.0",
2020
"pytest-timeout >= 2.3.1",

redux/autorun.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class Autorun(
6565
Event,
6666
SelectorOutput,
6767
ComparatorOutput,
68-
ReturnType,
6968
Args,
69+
ReturnType,
7070
],
7171
):
7272
"""Run a wrapped function in response to specific state changes in the store."""
@@ -84,7 +84,10 @@ def __init__(
8484
options: AutorunOptions[ReturnType],
8585
) -> None:
8686
"""Initialize the Autorun instance."""
87-
self.__name__ = func.__name__
87+
if hasattr(func, '__name__'):
88+
self.__name__ = f'Autorun:{func.__name__}'
89+
else:
90+
self.__name__ = f'Autorun:{func}'
8891
self._store = store
8992
self._selector = selector
9093
self._comparator = comparator
@@ -147,8 +150,8 @@ def unsubscribe(
147150
Event,
148151
SelectorOutput,
149152
ComparatorOutput,
150-
ReturnType,
151153
Args,
154+
ReturnType,
152155
],
153156
_: weakref.ref | None = None,
154157
) -> None:
@@ -164,8 +167,8 @@ def inform_subscribers(
164167
Event,
165168
SelectorOutput,
166169
ComparatorOutput,
167-
ReturnType,
168170
Args,
171+
ReturnType,
169172
],
170173
) -> None:
171174
"""Inform all subscribers about the latest value."""
@@ -186,8 +189,8 @@ def _task_callback(
186189
Event,
187190
SelectorOutput,
188191
ComparatorOutput,
189-
ReturnType,
190192
Args,
193+
ReturnType,
191194
],
192195
task: Task,
193196
*,
@@ -207,8 +210,8 @@ def _check(
207210
Event,
208211
SelectorOutput,
209212
ComparatorOutput,
210-
ReturnType,
211213
Args,
214+
ReturnType,
212215
],
213216
state: State | None,
214217
) -> bool:
@@ -239,8 +242,8 @@ def _call(
239242
Event,
240243
SelectorOutput,
241244
ComparatorOutput,
242-
ReturnType,
243245
Args,
246+
ReturnType,
244247
],
245248
*args: Args.args,
246249
**kwargs: Args.kwargs,
@@ -287,8 +290,8 @@ def __call__(
287290
Event,
288291
SelectorOutput,
289292
ComparatorOutput,
290-
ReturnType,
291293
Args,
294+
ReturnType,
292295
],
293296
*args: Args.args,
294297
**kwargs: Args.kwargs,
@@ -307,8 +310,8 @@ def __repr__(
307310
Event,
308311
SelectorOutput,
309312
ComparatorOutput,
310-
ReturnType,
311313
Args,
314+
ReturnType,
312315
],
313316
) -> str:
314317
"""Return a string representation of the Autorun instance."""
@@ -325,8 +328,8 @@ def value(
325328
Event,
326329
SelectorOutput,
327330
ComparatorOutput,
328-
ReturnType,
329331
Args,
332+
ReturnType,
330333
],
331334
) -> ReturnType:
332335
"""Get the latest value of the autorun function."""
@@ -339,8 +342,8 @@ def subscribe(
339342
Event,
340343
SelectorOutput,
341344
ComparatorOutput,
342-
ReturnType,
343345
Args,
346+
ReturnType,
344347
],
345348
callback: Callable[[ReturnType], Any],
346349
*,
@@ -376,8 +379,8 @@ def __signature__(
376379
Event,
377380
SelectorOutput,
378381
ComparatorOutput,
379-
ReturnType,
380382
Args,
383+
ReturnType,
381384
],
382385
) -> inspect.Signature:
383386
"""Get the signature of the wrapped function."""

redux/main.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ def _call_listeners(self: Store[State, Action, Event], state: State) -> None:
124124
for listener_ in self._listeners.copy():
125125
if isinstance(listener_, weakref.ref):
126126
listener = listener_()
127-
if listener is None:
128-
self._listeners.discard(listener_)
129-
continue
127+
assert listener is not None # noqa: S101
130128
else:
131129
listener = listener_
132130
result = listener(state)
@@ -297,7 +295,6 @@ def _wait_for_store_to_finish(self: Store[State, Action, Event]) -> None:
297295
if self.store_options.on_finish:
298296
self.store_options.on_finish()
299297
break
300-
self.run()
301298

302299
def _handle_finish_event(self: Store[State, Action, Event]) -> None:
303300
Thread(target=self._wait_for_store_to_finish).start()

redux/with_state.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def __init__(
2727
],
2828
) -> None:
2929
"""Initialize the WithState instance."""
30+
if hasattr(func, '__name__'):
31+
self.__name__ = f'WithState:{func.__name__}'
32+
else:
33+
self.__name__ = f'WithState:{func}'
3034
self._store = store
3135
self._selector = selector
3236
self._func = func

tests/test_autorun.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def decorated(value: int) -> int:
8888
store_snapshot.take()
8989
return value
9090

91-
assert decorated.__name__ == 'decorated'
91+
assert decorated.__name__ == 'Autorun:decorated'
9292

9393

9494
def test_ignore_attribute_error_in_selector(store: StoreType) -> None:

uv.lock

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

0 commit comments

Comments
 (0)