Skip to content

Commit 18b3535

Browse files
committed
Document what the example stubs stand in for
The example class and its xfail() and via() methods had only comments, so it was not clear from the stub what via() does upstream (nothing at run time; it labels an example's provenance for tooling) or that an expected failure is strict. Docstrings now say what each one does in Hypothesis and how the stub replays it.
1 parent 662beae commit 18b3535

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

‎Lib/test/support/_hypothesis_stubs/__init__.py‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def test_function(self):
8282

8383

8484
class example:
85+
"""Stub for ``hypothesis.example``.
86+
87+
Upstream, ``@example(...)`` adds an explicit input that Hypothesis
88+
always runs before generating any, with the same positional-or-keyword
89+
rules as ``@given``. Here the explicit examples are all that runs:
90+
``given`` replays each one in a ``subTest``.
91+
"""
92+
8593
def __init__(self, *args, **kwargs):
8694
if bool(args) == bool(kwargs):
8795
raise ValueError("Must specify exactly one of *args or **kwargs")
@@ -103,15 +111,28 @@ def __call__(self, f):
103111
return f
104112

105113
def xfail(self, condition=True, *, reason="", raises=BaseException):
106-
# Like real Hypothesis, an expected failure is strict: the example
107-
# must raise one of the given exceptions, or the test fails.
114+
"""Mark this example as an expected failure, as upstream does.
115+
116+
Upstream, ``@example(...).xfail()`` means the example must raise
117+
one of the ``raises`` exceptions (any exception by default), like
118+
``pytest.mark.xfail(strict=True)``: a different exception
119+
propagates, and an unexpected pass fails the test, with ``reason``
120+
in the message. ``condition=False`` makes it an ordinary example.
121+
The stub replays it the same way.
122+
"""
108123
if condition:
109124
self._example = replace(self._example, raises=raises,
110125
reason=reason)
111126
return self
112127

113128
def via(self, whence, /):
114-
# A label for tooling; it does not change what runs.
129+
"""Accept and ignore an example's provenance label.
130+
131+
Upstream, ``@example(...).via("...")`` only records where the
132+
example came from, for tools that add or remove ``@example``
133+
decorators (HypoFuzz, for instance); it changes nothing at run
134+
time there either.
135+
"""
115136
return self
116137

117138

0 commit comments

Comments
 (0)