Skip to content

Commit 4d00351

Browse files
committed
Fix single-return stateful printing
1 parent 887e227 commit 4d00351

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes a bug in stateful testing, where returning a single value
4+
wrapped in :func:`~hypothesis.stateful.multiple` would be printed such that
5+
the assigned variable was a tuple rather than the single element
6+
(:issue:`3236`).

hypothesis-python/src/hypothesis/stateful.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,16 @@ def invariants(cls):
333333

334334
def _print_step(self, rule, data, result):
335335
self.step_count = getattr(self, "step_count", 0) + 1
336-
# If the step has target bundles, and the result is a MultipleResults
337-
# then we want to assign to multiple variables.
338-
if isinstance(result, MultipleResults):
339-
n_output_vars = len(result.values)
340-
else:
341-
n_output_vars = 1
342-
if rule.targets and n_output_vars >= 1:
343-
output_assignment = ", ".join(self._last_names(n_output_vars)) + " = "
344-
else:
345-
output_assignment = ""
336+
output_assignment = ""
337+
if rule.targets:
338+
if isinstance(result, MultipleResults):
339+
if len(result.values) == 1:
340+
output_assignment = f"({self._last_names(1)[0]},) = "
341+
elif result.values:
342+
output_names = self._last_names(len(result.values))
343+
output_assignment = ", ".join(output_names) + " = "
344+
else:
345+
output_assignment = self._last_names(1)[0] + " = "
346346
report(
347347
"{}state.{}({})".format(
348348
output_assignment,

hypothesis-python/tests/cover/test_stateful.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,32 @@ def fail_fast(self):
228228
state.fail_fast()
229229

230230

231+
def test_multiple_variables_printed_single_element():
232+
# https://github.com/HypothesisWorks/hypothesis/issues/3236
233+
class ProducesMultiple(RuleBasedStateMachine):
234+
b = Bundle("b")
235+
236+
@initialize(target=b)
237+
def populate_bundle(self):
238+
return multiple(1)
239+
240+
@rule(b=b)
241+
def fail_fast(self, b):
242+
assert b != 1
243+
244+
with capture_out() as o, raises(AssertionError):
245+
run_state_machine_as_test(ProducesMultiple)
246+
247+
assignment_line = o.getvalue().split("\n")[2]
248+
assert assignment_line == "(v1,) = state.populate_bundle()"
249+
250+
state = ProducesMultiple()
251+
(v1,) = state.populate_bundle()
252+
state.fail_fast((v1,)) # passes if tuple not unpacked
253+
with raises(AssertionError):
254+
state.fail_fast(v1)
255+
256+
231257
def test_no_variables_printed():
232258
class ProducesNoVariables(RuleBasedStateMachine):
233259
b = Bundle("b")

0 commit comments

Comments
 (0)