Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Changelog
=========

Unreleased
----------

* Removed the random state caching, which would grow without bound, leaking memory in long test runs.
The caching was added to slightly speed up re-using the same (final) seed, but since the final seed is now different for each test, it has no effect.

`PR #690 <https://github.com/pytest-dev/pytest-randomly/issues/687>`__.

4.0.0 (2025-09-10)
------------------

Expand Down
24 changes: 7 additions & 17 deletions src/pytest_randomly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,28 @@ def pytest_configure_node(self, node: Item) -> None:
node.workerinput["randomly_seed"] = seed # type: ignore [attr-defined]


random_states: dict[int, tuple[Any, ...]] = {}
np_random_states: dict[int, Any] = {}


entrypoint_reseeds: list[Callable[[int], None]] | None = None


def _reseed(config: Config, offset: int = 0) -> int:
global entrypoint_reseeds
seed: int = config.getoption("randomly_seed") + offset
if seed not in random_states:
random.seed(seed)
random_states[seed] = random.getstate()
else:
random.setstate(random_states[seed])

random.seed(seed)
random_state = random.getstate()

if have_factory_boy: # pragma: no branch
factory_set_random_state(random_states[seed])
factory_set_random_state(random_state)

if have_faker: # pragma: no branch
faker_random.setstate(random_states[seed])
faker_random.setstate(random_state)

if have_model_bakery: # pragma: no branch
baker_random.setstate(random_states[seed])
baker_random.setstate(random_state)

if have_numpy: # pragma: no branch
numpy_seed = _truncate_seed_for_numpy(seed)
if numpy_seed not in np_random_states:
np_random.seed(numpy_seed)
np_random_states[numpy_seed] = np_random.get_state()
else:
np_random.set_state(np_random_states[numpy_seed])
np_random.seed(numpy_seed)

if entrypoint_reseeds is None:
eps = entry_points(group="pytest_randomly.random_seeder")
Expand Down