You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 918b125
Browse filesBrowse the repository at this point in the historyBrowse files
Support .filter(...) and .map(...) on stateful Bundles
Bundle now intercepts .filter and .map, storing the transformations and
applying them within a single draw: a throwaway SampledFromStrategy
subclass samples (position, reference) pairs from the bundle's current
contents, resolves values lazily, and retries rejected elements without
redrawing the whole step. consumes() preserves transformations, so
consumes(b).filter(fn) and consumes(b.filter(fn)) are both supported
and only ever remove the value which was actually drawn. Previously,
filtering a consuming bundle also popped every rejected value, and
consumes(b.filter(fn)) was a type error.
Design notes:
- The old BundleReferenceStrategy is inlined into Bundle, which draws
VarReferences for direct rule arguments so printed reproductions use
variable names; with a map transformation the passed value is not the
stored one, so the transformed value is drawn and printed directly.
- The per-draw sampler is what keeps bundle draws thread-safe: Bundle
and its Rule-created copies are shared by every instance (and thread)
of a machine class, so per-draw state never lives on them.
- Since Bundle is not a SampledFromStrategy, construction-time
optimizations like the unique-lists fast path and text() alphabet
inspection never see a bundle; lists(bundle, unique=True) keeps
using the generic unique-list strategy.
Informed by Reagan Lee's earlier implementation in upstream PR 4084
and Liam's review comments there.
Co-Authored-By: Zac Hatfield-Dodds <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fm2EfTyhtGU8GsiUBUN9df
Copy file name to clipboardExpand all lines: hypothesis/docs/stateful.rst
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,8 @@ A rule can, in place of a normal strategy, take a :class:`~hypothesis.stateful.B
25
25
26
26
Specifically, a rule that specifies ``target=a_bundle`` will cause its return value to be added to that bundle. A rule that specifies ``an_argument=a_bundle`` as a strategy will draw a value from that bundle. A rule can also specify that an argument chooses a value from a bundle and removes that value by using :func:`~hypothesis.stateful.consumes` as in ``an_argument=consumes(a_bundle)``.
27
27
28
+
Bundles can be filtered and mapped like any other strategy, and this composes with :func:`~hypothesis.stateful.consumes` in either order: ``consumes(a_bundle.filter(fn))`` and ``consumes(a_bundle).filter(fn)`` both draw a currently-matching value and remove only that value from the bundle.
29
+
28
30
.. note::
29
31
There is some overlap between what you can do with Bundles and what you can do with instance variables. Both represent state that rules can manipulate. If you do not need to draw values that depend on the machine's state, you can simply use instance variables. If you do need to draw values that depend on the machine's state, Bundles provide a fairly straightforward way to do this. If you need rules that draw values that depend on the machine's state in some more complicated way, you will have to abandon bundles. You can use :func:`~hypothesis.strategies.runner` and |.flatmap| to access the instance from a rule: the strategy ``runner().flatmap(lambda self: sampled_from(self.a_list))`` will draw from the instance variable ``a_list``. If you need something more complicated still, you can use |st.data| to draw data from the instance (or anywhere else) based on logic in the rule.
0 commit comments