Skip to content

Commit c892588

Browse files
committed
fix: serialization class methods of Store use cls instead of Store for the sake of extensibility via inheritance
refactor: `pytest_addoption` moved to `test.py` to make reusable
1 parent 7d64e10 commit c892588

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

CHANGELOG.md

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

3+
## Version 0.12.4
4+
5+
- fix: serialization class methods of `Store` use `cls` instead of `Store` for the
6+
sake of extensibility via inheritance
7+
- refactor: `pytest_addoption` moved to `test.py` to make reusable
8+
39
## Version 0.12.3
410

511
- test: write tests for different features of the api

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-redux"
3-
version = "0.12.3"
3+
version = "0.12.4"
44
description = "Redux implementation for Python"
55
authors = ["Sassan Haradji <[email protected]>"]
66
license = "Apache-2.0"

redux/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ def serialize_value(cls: type[Store], obj: object | type) -> SnapshotAtom:
308308
if isinstance(obj, (int, float, str, bool, NoneType)):
309309
return obj
310310
if callable(obj):
311-
return Store.serialize_value(obj())
311+
return cls.serialize_value(obj())
312312
if isinstance(obj, (list, tuple)):
313-
return [Store.serialize_value(i) for i in obj]
313+
return [cls.serialize_value(i) for i in obj]
314314
if is_immutable(obj):
315-
return Store._serialize_dataclass_to_dict(obj)
315+
return cls._serialize_dataclass_to_dict(obj)
316316
msg = f'Unable to serialize object with type `{type(obj)}`.'
317317
raise TypeError(msg)
318318

@@ -323,6 +323,6 @@ def _serialize_dataclass_to_dict(
323323
) -> dict[str, Any]:
324324
result = {}
325325
for field in dataclasses.fields(obj):
326-
value = Store.serialize_value(getattr(obj, field.name))
326+
value = cls.serialize_value(getattr(obj, field.name))
327327
result[field.name] = value
328328
return result

redux/test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
from redux.main import Store
2020

2121

22+
def pytest_addoption(parser: pytest.Parser) -> None:
23+
"""Add options to the pytest command line."""
24+
parser.addoption('--override-store-snapshots', action='store_true')
25+
26+
2227
class StoreSnapshotContext:
2328
"""Context object for tests taking snapshots of the store."""
2429

@@ -89,7 +94,7 @@ def take(self: StoreSnapshotContext, *, title: str | None = None) -> None:
8994
mismatch_path.write_text( # pragma: no cover
9095
f'// MISMATCH: {filename}\n{new_snapshot}\n',
9196
)
92-
assert old_snapshot == new_snapshot, f'Snapshot mismatch: {title}'
97+
assert old_snapshot == new_snapshot, f'Store snapshot mismatch: {title}'
9398

9499
self.test_counter[title] += 1
95100

tests/conftest.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212

1313
pytest.register_assert_rewrite('redux.test')
1414

15-
from redux.test import store_snapshot # noqa: E402
15+
from redux.test import pytest_addoption, store_snapshot # noqa: E402
1616

1717
if TYPE_CHECKING:
1818
from logging import Logger
1919

20-
__all__ = ['store_snapshot']
21-
22-
23-
def pytest_addoption(parser: pytest.Parser) -> None:
24-
parser.addoption('--override-store-snapshots', action='store_true')
20+
__all__ = ('store_snapshot', 'pytest_addoption')
2521

2622

2723
@pytest.fixture()

0 commit comments

Comments
 (0)