From e9a4713c620996b07ce029370f1ecef6b21feae5 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 12 Sep 2025 16:05:31 +0100 Subject: [PATCH] Remove the random state caching --- CHANGELOG.rst | 8 ++++++++ src/pytest_randomly/__init__.py | 24 +++++++----------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index aa94463..4bf13fb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 `__. + 4.0.0 (2025-09-10) ------------------ diff --git a/src/pytest_randomly/__init__.py b/src/pytest_randomly/__init__.py index e877b1d..03ec284 100644 --- a/src/pytest_randomly/__init__.py +++ b/src/pytest_randomly/__init__.py @@ -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")