Skip to content

Commit

Permalink
fix: serialization class methods of Store use cls instead of `Sto…
Browse files Browse the repository at this point in the history
…re` for the sake of extensibility via inheritance

refactor: `pytest_addoption` moved to `test.py` to make reusable
  • Loading branch information
sassanh committed Mar 17, 2024
1 parent 7d64e10 commit c892588
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Version 0.12.4

- 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

## Version 0.12.3

- test: write tests for different features of the api
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-redux"
version = "0.12.3"
version = "0.12.4"
description = "Redux implementation for Python"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand Down
8 changes: 4 additions & 4 deletions redux/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,11 @@ def serialize_value(cls: type[Store], obj: object | type) -> SnapshotAtom:
if isinstance(obj, (int, float, str, bool, NoneType)):
return obj
if callable(obj):
return Store.serialize_value(obj())
return cls.serialize_value(obj())
if isinstance(obj, (list, tuple)):
return [Store.serialize_value(i) for i in obj]
return [cls.serialize_value(i) for i in obj]
if is_immutable(obj):
return Store._serialize_dataclass_to_dict(obj)
return cls._serialize_dataclass_to_dict(obj)
msg = f'Unable to serialize object with type `{type(obj)}`.'
raise TypeError(msg)

Expand All @@ -323,6 +323,6 @@ def _serialize_dataclass_to_dict(
) -> dict[str, Any]:
result = {}
for field in dataclasses.fields(obj):
value = Store.serialize_value(getattr(obj, field.name))
value = cls.serialize_value(getattr(obj, field.name))
result[field.name] = value
return result
7 changes: 6 additions & 1 deletion redux/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
from redux.main import Store


def pytest_addoption(parser: pytest.Parser) -> None:
"""Add options to the pytest command line."""
parser.addoption('--override-store-snapshots', action='store_true')


class StoreSnapshotContext:
"""Context object for tests taking snapshots of the store."""

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

self.test_counter[title] += 1

Expand Down
8 changes: 2 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@

pytest.register_assert_rewrite('redux.test')

from redux.test import store_snapshot # noqa: E402
from redux.test import pytest_addoption, store_snapshot # noqa: E402

if TYPE_CHECKING:
from logging import Logger

__all__ = ['store_snapshot']


def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption('--override-store-snapshots', action='store_true')
__all__ = ('store_snapshot', 'pytest_addoption')


@pytest.fixture()
Expand Down

0 comments on commit c892588

Please sign in to comment.