Skip to content

Commit bd55761

Browse files
committed
Merge branch 'master' into enable-ellipses
2 parents 3b7f721 + f2a3966 commit bd55761

33 files changed

+1043
-562
lines changed

hypothesis-python/docs/changes.rst

+68
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,74 @@ Hypothesis 6.x
1818

1919
.. include:: ../RELEASE.rst
2020

21+
.. _v6.40.3:
22+
23+
-------------------
24+
6.40.3 - 2022-04-01
25+
-------------------
26+
27+
This patch simplifies the repr of the strategies namespace returned in
28+
:func:`~hypothesis.extra.array_api.make_strategies_namespace`, e.g.
29+
30+
.. code-block:: pycon
31+
32+
>>> from hypothesis.extra.array_api import make_strategies_namespace
33+
>>> from numpy import array_api as xp
34+
>>> xps = make_strategies_namespace(xp)
35+
>>> xps
36+
make_strategies_namespace(numpy.array_api)
37+
38+
.. _v6.40.2:
39+
40+
-------------------
41+
6.40.2 - 2022-04-01
42+
-------------------
43+
44+
Fixed :func:`~hypothesis.strategies.from_type` support for
45+
:pep:`604` union types, like ``int | None`` (:issue:`3255`).
46+
47+
.. _v6.40.1:
48+
49+
-------------------
50+
6.40.1 - 2022-04-01
51+
-------------------
52+
53+
Fixed an internal error when ``given()`` was passed a lambda.
54+
55+
.. _v6.40.0:
56+
57+
-------------------
58+
6.40.0 - 2022-03-29
59+
-------------------
60+
61+
:doc:`The Ghostwriter <ghostwriter>` can now write tests which check that
62+
two or more functions are equivalent on valid inputs, *or* raise the same
63+
type of exception for invalid inputs (:issue:`3267`).
64+
65+
.. _v6.39.6:
66+
67+
-------------------
68+
6.39.6 - 2022-03-27
69+
-------------------
70+
71+
This patch makes some quality-of-life improvements to the
72+
:doc:`Ghostwriter <ghostwriter>`: we guess the :func:`~hypothesis.strategies.text`
73+
strategy for arguments named ``text`` (...obvious in hindsight, eh?);
74+
and improved the error message if you accidentally left in a
75+
:func:`~hypothesis.strategies.nothing` or broke your :pypi:`rich` install.
76+
77+
.. _v6.39.5:
78+
79+
-------------------
80+
6.39.5 - 2022-03-26
81+
-------------------
82+
83+
This patch improves our error detection and message when Hypothesis is run
84+
on a Python implementation without support for ``-0.0``, which is required
85+
for the :func:`~hypothesis.strategies.floats` strategy but can be disabled by
86+
`unsafe compiler options <https://simonbyrne.github.io/notes/fastmath/>`__
87+
(:issue:`3265`).
88+
2189
.. _v6.39.4:
2290

2391
-------------------

hypothesis-python/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def local_file(name):
9696
"Documentation": "https://hypothesis.readthedocs.io",
9797
"Issues": "https://github.com/HypothesisWorks/hypothesis/issues",
9898
},
99-
license="MPL v2",
99+
license="MPL-2.0",
100100
description="A library for property-based testing",
101101
zip_safe=False,
102102
extras_require=extras,

hypothesis-python/src/hypothesis/core.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
)
102102
from hypothesis.statistics import describe_targets, note_statistics
103103
from hypothesis.strategies._internal.collections import TupleStrategy
104+
from hypothesis.strategies._internal.misc import NOTHING
104105
from hypothesis.strategies._internal.strategies import (
105106
Ex,
106107
MappedSearchStrategy,
@@ -249,9 +250,9 @@ def is_invalid_test(test, original_argspec, given_arguments, given_kwargs):
249250
only be reported for tests that actually ran.
250251
"""
251252

252-
def invalid(message):
253+
def invalid(message, *, exc=InvalidArgument):
253254
def wrapped_test(*arguments, **kwargs):
254-
raise InvalidArgument(message)
255+
raise exc(message)
255256

256257
wrapped_test.is_hypothesis_test = True
257258
wrapped_test.hypothesis = HypothesisHandle(
@@ -310,6 +311,18 @@ def wrapped_test(*arguments, **kwargs):
310311
)
311312
)
312313

314+
# This case would raise Unsatisfiable *anyway*, but by detecting it here we can
315+
# provide a much more helpful error message for people e.g. using the Ghostwriter.
316+
empty = [
317+
f"{s!r} (arg {idx})" for idx, s in enumerate(given_arguments) if s is NOTHING
318+
] + [f"{name}={s!r}" for name, s in given_kwargs.items() if s is NOTHING]
319+
if empty:
320+
strats = "strategies" if len(empty) > 1 else "strategy"
321+
return invalid(
322+
f"Cannot generate examples from empty {strats}: " + ", ".join(empty),
323+
exc=Unsatisfiable,
324+
)
325+
313326

314327
class ArtificialDataForExample(ConjectureData):
315328
"""Dummy object that pretends to be a ConjectureData object for the purposes of

hypothesis-python/src/hypothesis/extra/array_api.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,11 @@ def floating_dtypes(
863863
unsigned_integer_dtypes.__doc__ = _unsigned_integer_dtypes.__doc__
864864
floating_dtypes.__doc__ = _floating_dtypes.__doc__
865865

866-
return SimpleNamespace(
866+
class PrettySimpleNamespace(SimpleNamespace):
867+
def __repr__(self):
868+
return f"make_strategies_namespace({xp.__name__})"
869+
870+
return PrettySimpleNamespace(
867871
from_dtype=from_dtype,
868872
arrays=arrays,
869873
array_shapes=array_shapes,

hypothesis-python/src/hypothesis/extra/cli.py

+31-11
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,24 @@ def codemod(path):
182182
flag_value="equivalent",
183183
help="very useful when optimising or refactoring code",
184184
)
185-
@click.option("--idempotent", "writer", flag_value="idempotent")
186-
@click.option("--binary-op", "writer", flag_value="binary_operation")
185+
@click.option(
186+
"--errors-equivalent",
187+
"writer",
188+
flag_value="errors-equivalent",
189+
help="--equivalent, but also allows consistent errors",
190+
)
191+
@click.option(
192+
"--idempotent",
193+
"writer",
194+
flag_value="idempotent",
195+
help="check that f(x) == f(f(x))",
196+
)
197+
@click.option(
198+
"--binary-op",
199+
"writer",
200+
flag_value="binary_operation",
201+
help="associativity, commutativity, identity element",
202+
)
187203
# Note: we deliberately omit a --ufunc flag, because the magic()
188204
# detection of ufuncs is both precise and complete.
189205
@click.option(
@@ -218,22 +234,26 @@ def write(func, writer, except_, style): # noqa: D301 # \b disables autowrap
218234
# NOTE: if you want to call this function from Python, look instead at the
219235
# ``hypothesis.extra.ghostwriter`` module. Click-decorated functions have
220236
# a different calling convention, and raise SystemExit instead of returning.
237+
kwargs = {"except_": except_ or (), "style": style}
221238
if writer is None:
222239
writer = "magic"
223240
elif writer == "idempotent" and len(func) > 1:
224241
raise click.UsageError("Test functions for idempotence one at a time.")
225242
elif writer == "roundtrip" and len(func) == 1:
226243
writer = "idempotent"
227-
elif writer == "equivalent" and len(func) == 1:
244+
elif "equivalent" in writer and len(func) == 1:
228245
writer = "fuzz"
246+
if writer == "errors-equivalent":
247+
writer = "equivalent"
248+
kwargs["allow_same_errors"] = True
229249

230250
try:
231251
from hypothesis.extra import ghostwriter
232252
except ImportError:
233253
sys.stderr.write(MESSAGE.format("black"))
234254
sys.exit(1)
235255

236-
code = getattr(ghostwriter, writer)(*func, except_=except_ or (), style=style)
256+
code = getattr(ghostwriter, writer)(*func, **kwargs)
237257
try:
238258
from rich.console import Console
239259
from rich.syntax import Syntax
@@ -242,10 +262,10 @@ def write(func, writer, except_, style): # noqa: D301 # \b disables autowrap
242262
except ImportError:
243263
print(code)
244264
else:
245-
code = Syntax(
246-
code,
247-
"python",
248-
background_color="default",
249-
theme="default" if guess_background_color() == "light" else "monokai",
250-
)
251-
Console().print(code, soft_wrap=True)
265+
try:
266+
theme = "default" if guess_background_color() == "light" else "monokai"
267+
code = Syntax(code, "python", background_color="default", theme=theme)
268+
Console().print(code, soft_wrap=True)
269+
except Exception:
270+
print("# Error while syntax-highlighting code", file=sys.stderr)
271+
print(code)

0 commit comments

Comments
 (0)