Skip to content

Commit 22049aa

Browse files
authored
Remove the random state caching (#690)
1 parent ded2e54 commit 22049aa

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Changelog
33
=========
44

5+
Unreleased
6+
----------
7+
8+
* Removed the random state caching, which would grow without bound, leaking memory in long test runs.
9+
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.
10+
11+
`PR #690 <https://github.com/pytest-dev/pytest-randomly/issues/687>`__.
12+
513
4.0.0 (2025-09-10)
614
------------------
715

src/pytest_randomly/__init__.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,38 +140,28 @@ def pytest_configure_node(self, node: Item) -> None:
140140
node.workerinput["randomly_seed"] = seed # type: ignore [attr-defined]
141141

142142

143-
random_states: dict[int, tuple[Any, ...]] = {}
144-
np_random_states: dict[int, Any] = {}
145-
146-
147143
entrypoint_reseeds: list[Callable[[int], None]] | None = None
148144

149145

150146
def _reseed(config: Config, offset: int = 0) -> int:
151147
global entrypoint_reseeds
152148
seed: int = config.getoption("randomly_seed") + offset
153-
if seed not in random_states:
154-
random.seed(seed)
155-
random_states[seed] = random.getstate()
156-
else:
157-
random.setstate(random_states[seed])
149+
150+
random.seed(seed)
151+
random_state = random.getstate()
158152

159153
if have_factory_boy: # pragma: no branch
160-
factory_set_random_state(random_states[seed])
154+
factory_set_random_state(random_state)
161155

162156
if have_faker: # pragma: no branch
163-
faker_random.setstate(random_states[seed])
157+
faker_random.setstate(random_state)
164158

165159
if have_model_bakery: # pragma: no branch
166-
baker_random.setstate(random_states[seed])
160+
baker_random.setstate(random_state)
167161

168162
if have_numpy: # pragma: no branch
169163
numpy_seed = _truncate_seed_for_numpy(seed)
170-
if numpy_seed not in np_random_states:
171-
np_random.seed(numpy_seed)
172-
np_random_states[numpy_seed] = np_random.get_state()
173-
else:
174-
np_random.set_state(np_random_states[numpy_seed])
164+
np_random.seed(numpy_seed)
175165

176166
if entrypoint_reseeds is None:
177167
eps = entry_points(group="pytest_randomly.random_seeder")

0 commit comments

Comments
 (0)