hypothesis - Man Page
Name
hypothesis — Hypothesis Documentation
Hypothesis is the property-based testing library for Python. With Hypothesis, you write tests which should pass for all inputs in whatever range you describe, and let Hypothesis randomly choose which of those inputs to check — including edge cases you might not have thought about. For example:
from hypothesis import given, strategies as st
@given(st.lists(st.integers() | st.floats()))
def test_sort_correctness_using_properties(lst):
result = my_sort(lst)
assert set(lst) == set(result)
assert all(a <= b for a, b in zip(result, result[1:]))You should start with the tutorial <>, or alternatively the more condensed quickstart <>.
Tutorial
An introduction to Hypothesis.
New users should start here, or with the more condensed quickstart <>.
How-to Guides
Practical guides for applying Hypothesis in specific scenarios.
Explanations
Commentary oriented towards deepening your understanding of Hypothesis.
API Reference
Technical API reference.
Quickstart
This is a lightning introduction to the most important features of Hypothesis; enough to get you started writing tests. The tutorial <> introduces these features (and more) in greater detail.
Install Hypothesis
pip install hypothesis
Write your first test
Create a new file called example.py, containing a simple test:
# contents of example.py
from hypothesis import given, strategies as st
@given(st.integers())
def test_integers(n):
print(f"called with {n}")
assert isinstance(n, int)
test_integers()@given <#hypothesis.given> is the standard entrypoint to Hypothesis. It takes a strategy, which describes the type of inputs you want the decorated function to accept. When we call test_integers, Hypothesis will generate random integers (because we used the integers() <#hypothesis.strategies.integers> strategy) and pass them as n. Let's see that in action now by running python example.py:
called with 0 called with -18588 called with -672780074 called with 32616 ...
We just called test_integers(), without passing a value for n, because Hypothesis generates random values of n for us.
Note:
By default, Hypothesis generates 100 random inputs. You can control this with the max_examples <#hypothesis.settings.max_examples> setting.
Running in a test suite
A Hypothesis test is still a regular python function, which means pytest or unittest will pick it up and run it in all the normal ways.
# contents of example.py
from hypothesis import given, strategies as st
@given(st.integers(0, 200))
def test_integers(n):
assert n < 50This test will clearly fail, which can be confirmed by running pytest example.py:
$ pytest example.py
...
@given(st.integers())
def test_integers(n):
> assert n < 50
E assert 50 < 50
E Falsifying example: test_integers(
E n=50,
E )Arguments to @given <#hypothesis.given>
You can pass multiple arguments to @given <#hypothesis.given>:
@given(st.integers(), st.text())
def test_integers(n, s):
assert isinstance(n, int)
assert isinstance(s, str)Or use keyword arguments:
@given(n=st.integers(), s=st.text())
def test_integers(n, s):
assert isinstance(n, int)
assert isinstance(s, str)- Note:
See @given <#hypothesis.given> for details about how @given <#hypothesis.given> handles different types of arguments.
Filtering inside a test
Sometimes, you need to remove invalid cases from your test. The best way to do this is with .filter() <#hypothesis.strategies.SearchStrategy.filter>:
@given(st.integers().filter(lambda n: n % 2 == 0))
def test_integers(n):
assert n % 2 == 0For more complicated conditions, you can use assume() <#hypothesis.assume>, which tells Hypothesis to discard any test case with a false-y argument:
@given(st.integers(), st.integers())
def test_integers(n1, n2):
assume(n1 != n2)
# n1 and n2 are guaranteed to be different here- Note:
You can learn more about .filter() <#hypothesis.strategies.SearchStrategy.filter> and assume() <#hypothesis.assume> in the Adapting strategies <> tutorial page.
Dependent generation
You may want an input to depend on the value of another input. For instance, you might want to generate two integers n1 and n2 where n1 <= n2.
You can do this using the @composite <#hypothesis.strategies.composite> strategy. @composite <#hypothesis.strategies.composite> lets you define a new strategy which is itself built by drawing values from other strategies, using the automatically-passed draw function.
@st.composite
def ordered_pairs(draw):
n1 = draw(st.integers())
n2 = draw(st.integers(min_value=n1))
return (n1, n2)
@given(ordered_pairs())
def test_pairs_are_ordered(pair):
n1, n2 = pair
assert n1 <= n2In more complex cases, you might need to interleave generation and test code. In this case, use data() <#hypothesis.strategies.data>.
@given(st.data(), st.text(min_size=1))
def test_string_characters_are_substrings(data, string):
assert isinstance(string, str)
index = data.draw(st.integers(0, len(string) - 1))
assert string[index] in stringCombining Hypothesis with pytest
Hypothesis works with pytest features, like pytest.mark.parametrize:
import pytest
from hypothesis import given, strategies as st
@pytest.mark.parametrize("operation", [reversed, sorted])
@given(st.lists(st.integers()))
def test_list_operation_preserves_length(operation, lst):
assert len(lst) == len(list(operation(lst)))Hypothesis also works with pytest fixtures:
import pytest
@pytest.fixture(scope="session")
def shared_mapping():
return {n: 0 for n in range(101)}
@given(st.integers(0, 100))
def test_shared_mapping_keys(shared_mapping, n):
assert n in shared_mappingTutorial
The Hypothesis tutorial introduces the main features of Hypothesis. We suggest reading through in order until completing Custom strategies <>, at which point you can choose to read what seems interesting to you.
If you're in a hurry, the quickstart <> is a much faster bare-bones version.
Introduction to Hypothesis
This page introduces two fundamental parts of Hypothesis (@given <#hypothesis.given>, and strategies) and shows how to test a selection sort implementation using Hypothesis.
Install Hypothesis
First, let's install Hypothesis:
pip install hypothesis
Defining a simple test
Hypothesis tests are defined using two things; @given <#hypothesis.given>, and a strategy, which is passed to @given <#hypothesis.given>. Here's a simple example:
from hypothesis import given, strategies as st
@given(st.integers())
def test_is_integer(n):
assert isinstance(n, int)Adding the @given <#hypothesis.given> decorator turns this function into a Hypothesis test. Passing integers() <#hypothesis.strategies.integers> to @given <#hypothesis.given> says that Hypothesis should generate random integers for the argument n when testing.
We can run this test by calling it:
from hypothesis import given, strategies as st
@given(st.integers())
def test_is_integer(n):
print(f"called with {n}")
assert isinstance(n, int)
test_is_integer()Note that we don't pass anything for n; Hypothesis handles generating that value for us. The resulting output looks like this:
called with 0 called with -18588 called with -672780074 called with 32616 ...
Testing a sorting algorithm
Suppose we have implemented a simple selection sort:
# contents of example.py
from hypothesis import given, strategies as st
def selection_sort(lst):
result = []
while lst:
smallest = min(lst)
result.append(smallest)
lst.remove(smallest)
return resultand want to make sure it's correct. We can write the following test by combining the integers() <#hypothesis.strategies.integers> and lists() <#hypothesis.strategies.lists> strategies:
...
@given(st.lists(st.integers()))
def test_sort_correct(lst):
print(f"called with {lst}")
assert selection_sort(lst.copy()) == sorted(lst)
test_sort_correct()When running test_sort_correct, Hypothesis uses the lists(integers()) strategy to generate random lists of integers. Feel free to run python example.py to get an idea of the kinds of lists Hypothesis generates (and to convince yourself that this test passes).
Adding floats to our test
This test is a good start. But selection_sort should be able to sort lists with floats, too. If we wanted to generate lists of either integers or floats, we can change our strategy:
# changes to example.py
@given(st.lists(st.integers() | st.floats()))
def test_sort_correct(lst):
passThe pipe operator | takes two strategies, and returns a new strategy which generates values from either of its strategies. So the strategy integers() | floats() can generate either an integer, or a float.
Note:
strategy1 | strategy2 is equivalent to st.one_of(strategy1, strategy2) <#hypothesis.strategies.one_of>.
Preventing floats() <#hypothesis.strategies.floats> from generating nan
Even though test_sort_correct passed when we used lists of integers, it actually fails now that we've added floats! If you run python example.py, you'll likely (but not always; this is random testing, after all) find that Hypothesis reports a counterexample to test_sort_correct. For me, that counterexample is [1.0, nan, 0]. It might be different for you.
The issue is that sorting in the presence of nan is not well defined. As a result, we may decide that we don't want to generate them while testing. We can pass floats(allow_nan=False) to tell Hypothesis not to generate nan:
# changes to example.py
@given(st.lists(st.integers() | st.floats(allow_nan=False)))
def test_sort_correct(lst):
passAnd now this test passes without issues.
Note:
You can use the .example() <#hypothesis.strategies.SearchStrategy.example> method to get an idea of the kinds of things a strategy will generate:
>>> st.lists(st.integers() | st.floats(allow_nan=False)).example() [-5.969063e-08, 15283673678, 18717, -inf]
Note that .example() <#hypothesis.strategies.SearchStrategy.example> is intended for interactive use only (i.e., in a REPL). It is not intended to be used inside tests.
Tests with multiple arguments
If we wanted to pass multiple arguments to a test, we can do this by passing multiple strategies to @given <#hypothesis.given>:
from hypothesis import given, strategies as st
@given(st.integers(), st.lists(st.floats()))
def test_multiple_arguments(n, lst):
assert isinstance(n, int)
assert isinstance(lst, list)
for f in lst:
assert isinstance(f, float)Keyword arguments
We can also pass strategies using keyword arguments:
@given(lst=st.lists(st.floats()), n=st.integers()) # <-- changed
def test_multiple_arguments(n, lst):
passNote that even though we changed the order the parameters to @given <#hypothesis.given> appear, we also explicitly told it which parameters to pass to by using keyword arguments, so the meaning of the test hasn't changed.
In general, you can think of positional and keyword arguments to @given <#hypothesis.given> as being forwarded to the test arguments.
Note:
One exception is that @given <#hypothesis.given> does not support mixing positional and keyword arguments. See the @given <#hypothesis.given> documentation for more about how it handles arguments.
Running Hypothesis tests
There are a few ways to run a Hypothesis test.
- Explicitly call it, like test_is_integer(), as we've seen. Hypothesis tests are just normal functions, except @given <#hypothesis.given> handles generating and passing values for the function arguments.
- Let a test runner such as pytest <https://pypi.org/project/pytest/> pick up on it (as long as the function name starts with test_).
Concretely, when running a Hypothesis test, Hypothesis will:
- generate 100 random inputs,
- run the body of the function for each input, and
- report any exceptions that get raised.
- Note:
The number of examples can be controlled with the max_examples <#hypothesis.settings.max_examples> setting. The default is 100.
When to use Hypothesis and property-based testing
Property-based testing is a powerful addition to unit testing. It is not always a replacement.
If you're having trouble coming up with a property in your code to test, we recommend trying the following:
- Look for round-trip properties: encode/decode, serialize/deserialize, etc. These property-based tests tend to be both powerful and easy to write.
- Look for @pytest.mark.parametrize in your existing tests. This is sometimes a good hint you can replace the parametrization with a strategy. For instance, @pytest.mark.parametrize("n", range(0, 100)) could be replaced by @given(st.integers(0, 100 - 1)).
- Simply call your code with random inputs (of the correct shape) from Hypothesis! You might be surprised how often this finds crashes. This can be especially valuable for projects with a single entrypoint interface to a lot of underlying code.
Other examples of properties include:
- An optimized implementation is equivalent to a slower, but clearly correct, implementation.
- A sequence of transactions in a financial system always "balances"; money never gets lost.
- The derivative of a polynomial of order n has order n - 1.
- A type-checker, linter, formatter, or compiler does not crash when called on syntactically valid code.
- And more <https://fsharpforfunandprofit.com/posts/property-based-testing-2/>.
Built-in strategies
This page shows some of the strategies that Hypothesis provides for you.
Strategies provided by Hypothesis
Here is a selection of strategies provided by Hypothesis that may be useful to know:
- integers() <#hypothesis.strategies.integers>
Generates integers.
- floats() <#hypothesis.strategies.floats>
Generates floats.
- booleans() <#hypothesis.strategies.booleans>
Generates booleans.
- text() <#hypothesis.strategies.text>
Generates unicode strings (i.e., instances of python:str). Can be constrained to ASCII with st.text(st.characters(codec="ascii")).
- lists() <#hypothesis.strategies.lists>
Generates lists with elements from the strategy passed to it. st.lists(st.integers()) generates lists of integers.
- tuples() <#hypothesis.strategies.tuples>
Generates tuples of a fixed length. st.tuples(st.integers(), st.floats()) generates tuples with two elements, where the first element is an integer and the second is a float.
- one_of() <#hypothesis.strategies.one_of>
Generates from any of the strategies passed to it. st.one_of(st.integers(), st.floats()) generates either integers or floats. You can also use | to construct one_of() <#hypothesis.strategies.one_of>, like st.integers() | st.floats().
- builds() <#hypothesis.strategies.builds>
Generates instances of a class (or other callable) by specifying a strategy for each argument, like st.builds(Person, name=st.text(), age=st.integers()).
- just() <#hypothesis.strategies.just>
Generates the exact value passed to it. st.just("a") generates the exact string "a". This is useful when something expects to be passed a strategy. For instance, st.lists(st.integers() | st.just("a")) generates lists whose elements are either integers or the string "a".
- sampled_from() <#hypothesis.strategies.sampled_from>
Generates a random value from a list. st.sampled_from(["a", 1]) is roughly equivalent to st.just("a") | st.just(1).
- none() <#hypothesis.strategies.none>
Generates None. Useful for parameters that can be optional, like st.integers() | st.none().
See also:
See the strategies API reference <> for a full list of strategies provided by Hypothesis.
Adapting strategies
This page discusses ways to adapt strategies to your needs, either by transforming them inline with .map() <#hypothesis.strategies.SearchStrategy.map>, or filtering out unwanted inputs with .filter() <#hypothesis.strategies.SearchStrategy.filter> and assume() <#hypothesis.assume>.
Mapping strategy inputs
Sometimes you want to apply a simple transformation to a strategy. For instance, we know that we can generate lists of integers with lists(integers()). But maybe we wanted to instead generate sorted lists. We could use an inline .map() <#hypothesis.strategies.SearchStrategy.map> to achieve this:
>>> lists(integers()).map(sorted).example() [-25527, -24245, -93, -70, -7, 0, 39, 65, 112, 6189, 19469, 32526, 1566924430]
In general, strategy.map(f) returns a new strategy which transforms all the examples generated by strategy by calling f on them.
Filtering strategy inputs
Many strategies in Hypothesis offer some control over the kinds of values that get generated. For instance, integers(min_value=0) generates positive integers, and integers(100, 200) generates integers between 100 and 200.
Sometimes, you need more control than this. The inputs from a strategy may not match exactly what you need, and you just need to filter out a few bad cases.
For instance, suppose we have written a simple test involving the modulo operator %:
from hypothesis import given, strategies as st
@given(st.integers(), st.integers())
def test_remainder_magnitude(a, b):
# the remainder after division is always less than
# the divisor
assert abs(a % b) < abs(b)Hypothesis will quickly report a failure for this test: ZeroDivisionError: integer modulo by zero. Just like division, modulo isn't defined for 0. The case of b == 0 isn't interesting for the test, and we would like to get rid of it.
The best way to do this is with the .filter() <#hypothesis.strategies.SearchStrategy.filter> method:
from hypothesis import assume, given, strategies as st
@given(st.integers(), st.integers().filter(lambda n: n != 0))
def test_remainder_magnitude(a, b):
# b is guaranteed to be nonzero here, thanks to the filter
assert abs(a % b) < abs(b)This test now passes cleanly.
Calling .filter() <#hypothesis.strategies.SearchStrategy.filter> on a strategy creates a new strategy with that filter applied at generation-time. For instance, integers().filter(lambda n: n != 0) is a strategy which generates nonzero integers.
Assuming away test cases
.filter() <#hypothesis.strategies.SearchStrategy.filter> lets you filter test inputs from a single strategy. Hypothesis also provides an assume() <#hypothesis.assume> function for when you need to filter an entire test case, based on an arbitrary condition.
The assume() <#hypothesis.assume> function skips test cases where some condition evaluates to True. You can use it anywhere in your test. We could have expressed our .filter() <#hypothesis.strategies.SearchStrategy.filter> example above using assume() <#hypothesis.assume> as well:
from hypothesis import assume, given, strategies as st
@given(st.integers(), st.integers())
def test_remainder_magnitude(a, b):
assume(b != 0)
# b will be nonzero here
assert abs(a % b) < abs(b)assume() <#hypothesis.assume> vs .filter() <#hypothesis.strategies.SearchStrategy.filter>
Where possible, you should use .filter() <#hypothesis.strategies.SearchStrategy.filter>. Hypothesis can often rewrite simple filters into more efficient sampling methods than rejection sampling, and will retry filters several times instead of aborting the entire test case (as with assume() <#hypothesis.assume>).
For more complex relationships that can't be expressed with .filter() <#hypothesis.strategies.SearchStrategy.filter>, use assume() <#hypothesis.assume>.
Here's an example of a test where we want to filter out two different types of examples:
from hypothesis import assume, given, strategies as st
@given(st.integers(), st.integers())
def test_floor_division_lossless_when_b_divides_a(a, b):
# we want to assume that:
# * b is nonzero, and
# * b divides a
assert (a // b) * b == aWe could start by using assume() <#hypothesis.assume> for both:
from hypothesis import assume, given, strategies as st
@given(st.integers(), st.integers())
def test_floor_division_lossless_when_b_divides_a(a, b):
assume(b != 0)
assume(a % b == 0)
assert (a // b) * b == aAnd then notice that the b != 0 condition can be moved into the strategy definition as a .filter() <#hypothesis.strategies.SearchStrategy.filter> call:
from hypothesis import assume, given, strategies as st
@given(st.integers(), st.integers().filter(lambda n: n != 0))
def test_floor_division_lossless_when_b_divides_a(a, b):
assume(a % b == 0)
assert (a // b) * b == aHowever, the a % b == 0 condition has to stay as an assume() <#hypothesis.assume>, because it expresses a more complicated relationship between a and b.
assume() <#hypothesis.assume> vs early-returning
One other way we could have avoided the divide-by-zero error inside the test function is to early-return when b == 0:
from hypothesis import assume, given, strategies as st
@given(st.integers(), st.integers())
def test_remainder_magnitude(a, b):
if b == 0:
# bad plan - test "passes" without checking anything!
return
assert abs(a % b) < abs(b)While this would have avoided the divide-by-zero, early-returning is not the same as using assume() <#hypothesis.assume>. With assume() <#hypothesis.assume>, Hypothesis knows that a test case has been filtered out, and will not count it towards the max_examples <#hypothesis.settings.max_examples> limit. In contrast, early-returns are counted as a passing test, even though the assertions didn't run! In more complicted cases, this could end up testing your code less than you expect, because many test cases get discarded without Hypothesis knowing about it.
In addition, assume() <#hypothesis.assume> lets you skip the test case at any point in the test, even inside arbitrarily deep nestings of functions.
Custom strategies
This page describes how to write a custom strategy, for when the built-in strategies don't quite fit your needs.
Writing helper functions
Sometimes you might find it useful to write helper functions, to more concisely express a common pattern for your project. For example, it's much easier to write (and read!) response=json() than to have the whole implementation inline:
def json(*, finite_only=True):
"""Helper function to describe JSON objects, with optional inf and nan."""
numbers = st.floats(allow_infinity=not finite_only, allow_nan=not finite_only)
return st.recursive(
st.none() | st.booleans() | st.integers() | numbers | st.text(),
extend=lambda xs: st.lists(xs) | st.dictionaries(st.text(), xs),
)Writing your own strategy
If a strategy in Hypothesis doesn't match what you need, you can write your own strategy.
For instance, suppose we want to generate a list of floats which sum to 1. We might start implementing this by generating lists of integers between 0 and 1 with lists(floats(0, 1)). But now we're a bit stuck, and can't go any further with the standard strategies.
One way to define a new strategy is using the @composite <#hypothesis.strategies.composite> decorator. @composite <#hypothesis.strategies.composite> lets you define a new strategy that uses arbitrary Python code. For instance, to implement the above:
from hypothesis import strategies as st
@st.composite
def sums_to_one(draw):
l = draw(st.lists(st.floats(0, 1)))
return [f / sum(l) for f in l]@composite <#hypothesis.strategies.composite> passes a draw function to the decorated function as its first argument. draw is used to draw a random value from another strategy. We return from sums_to_one a value of the form we wanted to generate; in this case, a list that sums to one.
Let's see this new strategy in action:
import pytest
from hypothesis import given, strategies as st
@st.composite
def sums_to_one(draw):
lst = draw(st.lists(st.floats(0.001, 1), min_size=1))
return [f / sum(lst) for f in lst]
@given(sums_to_one())
def test(lst):
# ignore floating point errors
assert sum(lst) == pytest.approx(1)- Note:
Just like all other strategies, we called sums_to_one before passing it to @given <#hypothesis.given>. @composite <#hypothesis.strategies.composite> should be thought of as turning its decorated function into a function which returns a strategy when called. This is actually the same as existing strategies in Hypothesis; integers() <#hypothesis.strategies.integers> is really a function, which returns a strategy for integers when called.
Combining @composite <#hypothesis.strategies.composite> with parameters
You can add parameters to functions decorated with @composite <#hypothesis.strategies.composite>, including keyword-only arguments. These work as you would normally expect.
For instance, suppose we wanted to generalize our sums_to_one function to sums_to_n. We can add a parameter n:
import pytest
from hypothesis import assume, given, strategies as st
@st.composite
def sums_to_n(draw, n=1): # <-- changed
lst = draw(st.lists(st.floats(0, 1), min_size=1))
assume(sum(lst) > 0)
return [f / sum(lst) * n for f in lst] # <-- changed
@given(sums_to_n(10))
def test(lst):
assert sum(lst) == pytest.approx(10)And we could just as easily have made n a keyword-only argument instead:
import pytest
from hypothesis import assume, given, strategies as st
@st.composite
def sums_to_n(draw, *, n=1): # <-- changed
lst = draw(st.lists(st.floats(0, 1), min_size=1))
assume(sum(lst) > 0)
return [f / sum(lst) * n for f in lst]
@given(sums_to_n(n=10)) # <-- changed
def test(lst):
assert sum(lst) == pytest.approx(10)Dependent generation with @composite <#hypothesis.strategies.composite>
Another scenario where @composite <#hypothesis.strategies.composite> is useful is when generating a value that depends on a value from another strategy. For instance, suppose we wanted to generate two integers n1 and n2 with n1 <= n2. We can do this using @composite <#hypothesis.strategies.composite>:
@st.composite
def ordered_pairs(draw):
n1 = draw(st.integers())
n2 = draw(st.integers(min_value=n1))
return (n1, n2)
@given(ordered_pairs())
def test_pairs_are_ordered(pair):
n1, n2 = pair
assert n1 <= n2- Note:
We could also have written this particular strategy as st.tuples(st.integers(), st.integers()).map(sorted) (see Adapting strategies <>). Some prefer this inline approach, while others prefer defining well-named helper functions with @composite <#hypothesis.strategies.composite>. Our suggestion is simply that you prioritize ease of understanding when choosing which to use.
Mixing data generation and test code
When using @composite <#hypothesis.strategies.composite>, you have to finish generating the entire input before running your test. But maybe you don't want to generate all of the input until you're sure some initial test assertions have passed. Or maybe you have some complicated control flow which makes it necessary to generate something in the middle of the test.
data() <#hypothesis.strategies.data> lets you to do this. It's similar to @composite <#hypothesis.strategies.composite>, except it lets you mix test code and generation code.
Note:
The downside of this power is that data() <#hypothesis.strategies.data> is incompatible @example <#hypothesis.example>, and that Hypothesis cannot print a nice representation of values generated from data() <#hypothesis.strategies.data> when reporting failing examples, because the draws are spread out. Where possible, prefer @composite <#hypothesis.strategies.composite> to data() <#hypothesis.strategies.data>.
For instance, here's how we would write our earlier @composite <#hypothesis.strategies.composite> example using data() <#hypothesis.strategies.data>:
import pytest
from hypothesis import given, strategies as st
@given(st.data())
def test(data):
lst = data.draw(st.lists(st.floats(0.001, 1), min_size=1))
lst = [f / sum(lst) for f in lst]
# ignore floating point errors
assert sum(lst) == pytest.approx(1)Configuring test settings
This page discusses how to configure the behavior of a single Hypothesis test, or of an entire test suite.
Configuring a single test
Hypothesis lets you configure the default behavior of a test using the @settings <#hypothesis.settings> decorator. You can use settings to configure how many examples Hypothesis generates, how Hypothesis replays failing examples, and the verbosity level of the test, among others.
Using @settings <#hypothesis.settings> on a single test looks like this:
from hypothesis import given, settings, strategies as st
@given(st.integers())
@settings(max_examples=200)
def runs_200_times_instead_of_100(n):
passYou can put @settings <#hypothesis.settings> either before or after @given <#hypothesis.given>. Both are equivalent.
Changing the number of examples
If you have a test which is very expensive or very cheap to run, you can change the number of examples (inputs) Hypothesis generates with the max_examples <#hypothesis.settings.max_examples> setting:
from hypothesis import given, settings, strategies as st
@given(st.integers())
@settings(max_examples=5)
def test(n):
print("prints five times")The default is 100 examples.
Note:
See How many times will Hypothesis run my test? <> for details on how max_examples <#hypothesis.settings.max_examples> interacts with other parts of Hypothesis.
Other settings options
Here are a few of the more commonly used setting values:
- settings.phases <#hypothesis.settings.phases> controls which phases of Hypothesis run, like replaying from the database or generating new inputs.
- settings.database <#hypothesis.settings.database> controls how and if Hypothesis replays failing examples.
- settings.verbosity <#hypothesis.settings.verbosity> can print debug information.
- settings.derandomize <#hypothesis.settings.derandomize> makes Hypothesis deterministic. (Two kinds of testing <https://blog.nelhage.com/post/two-kinds-of-testing/> discusses when and why you might want that).
- Note:
See the settings <#hypothesis.settings> reference for a full list of possible settings.
Changing settings across your test suite
In addition to configuring individual test functions with @settings <#hypothesis.settings>, you can configure test behavior across your test suite using a settings profile. This might be useful for creating a development settings profile which runs fewer examples, or a settings profile in CI which connects to a separate database.
To create a settings profile, use register_profile() <#hypothesis.settings.register_profile>:
from hypothesis import HealthCheck, settings
settings.register_profile("fast", max_examples=10)You can place this code in any file which gets loaded before your tests get run. This includes an __init__.py file in the test directory or any of the test files themselves. If using pytest, the standard location to place this code is in a confest.py file (though an __init__.py or test file will also work).
Note that registering a new profile will not affect tests until it is loaded with load_profile() <#hypothesis.settings.load_profile>:
from hypothesis import HealthCheck, settings
settings.register_profile("fast", max_examples=10)
# any tests executed before loading this profile will still use the
# default active profile of 100 examples.
settings.load_profile("fast")
# any tests executed after this point will use the active fast
# profile of 10 examples.There is no limit to the number of settings profiles you can create. Hypothesis creates a profile called "default", which is active by default. You can also explicitly make it active at any time using settings.load_profile("default"), if for instance you wanted to revert a custom profile you had previously loaded.
Loading profiles from environment variables
Using an environment variable to load a settings profile is a useful trick for choosing a settings profile depending on the environment:
>>> import os
>>> from hypothesis import settings, Verbosity
>>> settings.register_profile("long", max_examples=1000)
>>> settings.register_profile("fast", max_examples=10)
>>> settings.register_profile("debug", max_examples=10, verbosity=Verbosity.verbose)
>>> settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "default"))If using pytest, you can also easily select the active profile with --hypothesis-profile:
$ pytest --hypothesis-profile fast
See the Hypothesis pytest plugin <#pytest-plugin>.
Replaying failed tests
Replaying failures found by your Hypothesis tests is almost as important as finding failures in the first place. Hypothesis therefore contains several ways to replay failures: they are automatically saved to (and replayed from) a local ExampleDatabase <#hypothesis.database.ExampleDatabase>, and can be manually replayed via @example <#hypothesis.example> or @reproduce_failure <#hypothesis.reproduce_failure>.
The Hypothesis database
When a test fails, Hypothesis automatically saves the failure so it can be replayed later. For instance, the first time you run the following code, it will take up to a few seconds to fail:
import time
from hypothesis import strategies as st
@given(st.integers())
def f(n):
assert n < 50
time.sleep(0.1)
f()But the next time you run this code, it will fail instantly. When Hypothesis saw the failure the first time, it automatically saved that failing input. On future runs, Hypothesis retries any failing inputs (in Phase.explain <#hypothesis.Phase.explain>) before generating new inputs (in Phase.generate <#hypothesis.Phase.generate>)
Hypothesis saves failures to the settings.database <#hypothesis.settings.database> setting. By default, this is a DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> in the local .hypothesis directory.
Disabling the database
You can disable the database by passing database=None to @settings <#hypothesis.settings>:
@settings(database=None) def f(n): ...
Using @example <#hypothesis.example> to run a specific input
If you want Hypothesis to always run a specific input, you can use @example <#hypothesis.example>. @example <#hypothesis.example> adds an explicit input which Hypothesis will run every time, in addition to the randomly generated examples. You can think of @example <#hypothesis.example> as combining unit-testing with property-based testing.
For instance, suppose we write a test using integers() <#hypothesis.strategies.integers>, but want to make sure we try a few special prime numbers every time we run the test. We can add these inputs with @example <#hypothesis.example>:
# two mersenne primes
@example(2**17 - 1)
@example(2**19 - 1)
@given(st.integers())
def test_integers(n):
pass
test_integers()Hypothesis runs all explicit examples first, in the Phase.explicit <#hypothesis.Phase.explicit> phase, before generating additional random examples in the Phase.generate <#hypothesis.Phase.generate> phase.
Inputs from @example <#hypothesis.example> do not shrink
Note that unlike examples generated by Hypothesis, examples provided using @example <#hypothesis.example> do not shrink. We can see this by adding a failing assertion:
@example(2**17 - 1)
@given(st.integers())
def test_something_with_integers(n):
assert n < 100Hypothesis will print Falsifying explicit example: test_something_with_integers(n=131071), instead of shrinking to n=100.
Prefer @example <#hypothesis.example> over the database for correctness
While the database is useful for quick local iteration, Hypothesis may invalidate it when upgrading (because e.g. the internal format may have changed). Changes to the source code of a test function may also change its database key, invalidating its stored entries. We therefore recommend against relying on the database for the correctness of your tests. If you want to ensure an input is run every time, use @example <#hypothesis.example>.
Replaying examples with @reproduce_failure <#hypothesis.reproduce_failure>
If settings.print_blob <#hypothesis.settings.print_blob> is set to True (the default in the ci settings profile), and a test fails, Hypothesis will print an @reproduce_failure <#hypothesis.reproduce_failure> decorator containing an opaque blob as part of the error message:
>>> from hypothesis import settings, given
>>> import hypothesis.strategies as st
>>> @given(st.floats())
... @settings(print_blob=True)
... def test(f):
... assert f == f
...
>>> test()
...
Falsifying example: test(
f=nan,
)
You can reproduce this example by temporarily adding @reproduce_failure('6.131.23', b'ACh/+AAAAAAAAA==') as a decorator on your test caseYou can add this decorator to your test to reproduce the failure. This can be useful for locally replaying failures found by CI. Note that the binary blob is not stable across Hypothesis versions, so you should not leave this decorator on your tests permanently. Use @example <#hypothesis.example> with an explicit input instead.
Sharing failures with the database
If you work with multiple developers, or want to share failures across environments (such as locally replaying a failure found by CI), another option is to share the Hypothesis database.
For instance, by setting settings.database <#hypothesis.settings.database> to an instance of a networked database like RedisExampleDatabase <#hypothesis.extra.redis.RedisExampleDatabase>, any developer connecting to that networked database will automatically replay any failures found by other developers.
Similarly, setting settings.database <#hypothesis.settings.database> to GitHubArtifactDatabase <#hypothesis.database.GitHubArtifactDatabase> will automatically replay any failures found by the connected CI artifact.
Adding notes
When a test fails, Hypothesis will normally print output that looks like this:
Falsifying example: test_a_thing(x=1, y="foo")
Sometimes you want to add some additional information to a failure, such as the output of some intermediate step in your test. The note() <#hypothesis.note> function lets you do this:
>>> from hypothesis import given, note, strategies as st
>>> @given(st.lists(st.integers()), st.randoms())
... def test_shuffle_is_noop(ls, r):
... ls2 = list(ls)
... r.shuffle(ls2)
... note(f"Shuffle: {ls2!r}")
... assert ls == ls2
...
>>> try:
... test_shuffle_is_noop()
... except AssertionError:
... print("ls != ls2")
...
Falsifying example: test_shuffle_is_noop(ls=[0, 1], r=RandomWithSeed(1))
Shuffle: [1, 0]
ls != ls2note() <#hypothesis.note> is like a print statement that gets attached to the falsifying example reported by Hypothesis. It's also reported by observability <#observability>, and shown for all examples (if settings.verbosity <#hypothesis.settings.verbosity> is set to Verbosity.verbose <#hypothesis.Verbosity.verbose> or higher).
Note:
event() <#hypothesis.event> is a similar function which tells Hypothesis to count the number of test cases which reported each distinct value you pass, for inclusion in test statistics <#statistics> and observability reports <#observability>.
Flaky failures
Have you ever had a test fail, and then you re-run it, only for the test to magically pass? That is a flaky test. A flaky test is one which might behave differently when called again. You can think of it as a test which is not deterministic.
Any test can be flaky, but because Hypothesis runs your test many times, Hypothesis tests are particularly likely to uncover flaky behavior.
Note that Hypothesis does not require tests to be fully deterministic. Only the sequence of calls to Hypothesis APIs like draw from @composite <#hypothesis.strategies.composite> and the outcome of the test (pass or fail) need to be deterministic. This means you can use randomness, threads, or nondeterminism in your test, as long as it doesn't impact anything Hypothesis can see.
Why is flakiness bad?
Hypothesis raises an exception when it detects flakiness. This might seem extreme, relative to a simple warning. But there are good reasons to consider flakiness a fatal error.
- Hypothesis relies on deterministic behavior for the database to work.
- Flakiness makes debugging failures substantially harder if the failing input reported by Hypothesis only flakily reproduces.
- Flakiness makes effectively exploration of the test's behavior space by Hypothesis difficult or impossible.
Common sources of flakiness
Here is a quick and non-exhaustive enumeration of some reasons you might encounter flakiness:
- Decisions based on global state.
- Explicit dependencies between inputs.
- Test depends on filesystem or database state which isn't reset between inputs.
- Un-managed sources of randomness. This includes standard PRNGs (see also register_random() <#hypothesis.register_random>), but also thread scheduling, network timing, etc.
- Note:
If your tests depend on global state, consider replacing that state with shared() <#hypothesis.strategies.shared>. This is a common way to refactor your test to bring conceptually-global state under the control and visibility of Hypothesis.
Types of flakiness
When Hypothesis detects that a test is flaky, it will raise one of two Flaky <#hypothesis.errors.Flaky> exceptions.
Flaky failure
The most common form of flakiness is that Hypothesis finds a failure, but then replaying that input does not reproduce the failure. For example, here is a contrived test which only fails the first time it is called:
called = False
@given(st.integers())
def test_fails_flakily(n):
global called
if not called:
called = True
assert FalseThe first time Hypothesis generates an input, this test will fail. But when Hypothesis tries replaying that failure—by generating the same input—the test will succeed. This test is flaky.
As a result, running test_fails_flakily() will raise FlakyFailure <#hypothesis.errors.FlakyFailure>. FlakyFailure <#hypothesis.errors.FlakyFailure> is an ExceptionGroup, which contains the origin failure as a sub-exception:
+ Exception Group Traceback (most recent call last): | hypothesis.errors.FlakyFailure: Hypothesis test_fails_flakily(n=0) produces unreliable results: Falsified on the first call but did not on a subsequent one (1 sub-exception) | Falsifying example: test_fails_flakily( | n=0, | ) | Failed to reproduce exception. Expected: | Traceback (most recent call last): | File "/Users/tybug/Desktop/sandbox2.py", line 13, in test_fails_flakily | assert False | ^^^^^ | AssertionError +-+---------------- 1 ---------------- | ... | Traceback (most recent call last): | File "/Users/tybug/Desktop/sandbox2.py", line 13, in test_fails_flakily | assert False | ^^^^^ | AssertionError +------------------------------------
The solution to seeing FlakyFailure <#hypothesis.errors.FlakyFailure> is to refactor the test to not depend on external state. In this case, the external state is the variable called.
Flaky strategy definition
Each strategy must 'do the same thing' (again, as seen by Hypothesis) if we replay a previously-seen input. Failing to do so is a more subtle but equally serious form of flakiness, which leaves us unable to shrink to a minimal failing input, or even reliably report the failure in future runs.
One easy way for this to occur is if a strategy depends on external state. For example, this strategy filters out previously-generated integers, including those seen in any previous test case:
seen = set()
@st.composite
def unique_ints(draw):
while (n := draw(st.integers())) in seen:
pass
seen.add(n)
return n
@given(unique_ints(), unique_ints())
def test_ints(x, y): ...By using seen, this test is relying on outside state! In the first test case where integers() <#hypothesis.strategies.integers> generates 0, unique_ints draws only one integer. But if in the next test case integers() <#hypothesis.strategies.integers> generates 0, unique_ints has to draw two integers because 0 is already in seen. This means data generation is not deterministic.
As a result, running test_ints() will raise FlakyStrategyDefinition <#hypothesis.errors.FlakyStrategyDefinition>. The solution is to refactor the strategy to not depend on external state. One way to do this is using shared() <#hypothesis.strategies.shared>:
@st.composite
def unique_ints(draw):
seen_this_test = draw(st.shared(st.builds(set), key="seen_ints"))
while (n := draw(st.integers())) in seen_this_test:
pass
seen_this_test.add(n)
return nHow-to guides
These how-to pages are practical guides for applying Hypothesis in specific scenarios. Each page addresses a particular question about using Hypothesis.
Suppress a health check everywhere
Hypothesis sometimes raises a HealthCheck <#hypothesis.HealthCheck> to indicate that your test may be less effective than you expect, slower than you expect, unlikely to generate effective examples, or otherwise has silently degraded performance.
While HealthCheck <#hypothesis.HealthCheck> can be useful to proactively identify issues, you may not care about certain classes of them. If you want to disable a HealthCheck <#hypothesis.HealthCheck> everywhere, you can register and load a settings profile with register_profile() <#hypothesis.settings.register_profile> and load_profile() <#hypothesis.settings.load_profile>. Place the following code in any file which is loaded before running your tests (or in conftest.py, if using pytest):
from hypothesis import HealthCheck, settings
settings.register_profile(
"my_profile", suppress_health_check=[HealthCheck.filter_too_much]
)
settings.load_profile("my_profile")This profile in particular suppresses the HealthCheck.filter_too_much <#hypothesis.HealthCheck.filter_too_much> health check for all tests. The exception is if a test has a @settings <#hypothesis.settings> which explicitly sets a different value for suppress_health_check, in which case the profile value will be overridden by the local settings value.
I want to suppress all health checks!
- Warning:
We strongly recommend that you suppress health checks as you encounter them, rather than using a blanket suppression. Several health checks check for subtle interactions that may save you hours of debugging, such as HealthCheck.function_scoped_fixture <#hypothesis.HealthCheck.function_scoped_fixture> and HealthCheck.differing_executors <#hypothesis.HealthCheck.differing_executors>.
If you really want to suppress all health checks, for instance to speed up interactive prototyping, you can:
from hypothesis import HealthCheck, settings
settings.register_profile("my_profile", suppress_health_check=list(HealthCheck))
settings.load_profile("my_profile")Write type hints for strategies
Hypothesis provides type hints for all strategies and functions which return a strategy:
from hypothesis import strategies as st reveal_type(st.integers()) # SearchStrategy[int] reveal_type(st.lists(st.integers())) # SearchStrategy[list[int]]
SearchStrategy <#hypothesis.strategies.SearchStrategy> is the type of a strategy. It is parametrized by the type of the example it generates. You can use it to write type hints for your functions which return a strategy:
from hypothesis import strategies as st
from hypothesis.strategies import SearchStrategy
# returns a strategy for "normal" numbers
def numbers() -> SearchStrategy[int | float]:
return st.integers() | st.floats(allow_nan=False, allow_infinity=False)It's worth pointing out the distinction between a strategy, and a function that returns a strategy. integers() <#hypothesis.strategies.integers> is a function which returns a strategy, and that strategy has type SearchStrategy[int]. The function st.integers therefore has type Callable[..., SearchStrategy[int]], while the value s = st.integers() has type SearchStrategy[int].
Type hints for @composite <#hypothesis.strategies.composite>
When writing type hints for strategies defined with @composite <#hypothesis.strategies.composite>, use the type of the returned value (not SearchStrategy):
@st.composite
def ordered_pairs(draw) -> tuple[int, int]:
n1 = draw(st.integers())
n2 = draw(st.integers(min_value=n1))
return (n1, n2)Type variance of SearchStrategy <#hypothesis.strategies.SearchStrategy>
SearchStrategy <#hypothesis.strategies.SearchStrategy> is covariant, meaning that if B < A then SearchStrategy[B] < SearchStrategy[A]. In other words, the strategy st.from_type(Dog) is a subtype of the strategy st.from_type(Animal).
Write a custom Hypothesis database
To define your own ExampleDatabase <#hypothesis.database.ExampleDatabase> class, implement the save() <#hypothesis.database.ExampleDatabase.save>, fetch() <#hypothesis.database.ExampleDatabase.fetch>, and delete() <#hypothesis.database.ExampleDatabase.delete> methods.
For example, here's a simple database class that uses sqlite as the backing data store:
import sqlite3
from collections.abc import Iterable
from hypothesis.database import ExampleDatabase
class SQLiteExampleDatabase(ExampleDatabase):
def __init__(self, db_path: str):
self.conn = sqlite3.connect(db_path)
self.conn.execute(
"""
CREATE TABLE examples (
key BLOB,
value BLOB,
UNIQUE (key, value)
)
"""
)
def save(self, key: bytes, value: bytes) -> None:
self.conn.execute(
"INSERT OR IGNORE INTO examples VALUES (?, ?)",
(key, value),
)
def fetch(self, key: bytes) -> Iterable[bytes]:
cursor = self.conn.execute("SELECT value FROM examples WHERE key = ?", (key,))
yield from [value[0] for value in cursor.fetchall()]
def delete(self, key: bytes, value: bytes) -> None:
self.conn.execute(
"DELETE FROM examples WHERE key = ? AND value = ?",
(key, value),
)Database classes are not required to implement move() <#hypothesis.database.ExampleDatabase.move>. The default implementation of a move is a delete() <#hypothesis.database.ExampleDatabase.delete> of the value in the old key, followed by a save() <#hypothesis.database.ExampleDatabase.save> of the value in the new key. You can override move() <#hypothesis.database.ExampleDatabase.move> to override this behavior, if for instance the backing store offers a more efficient move implementation.
Change listening
To support change listening in a database class, you should call _broadcast_change() <#hypothesis.database.ExampleDatabase._broadcast_change> whenever a value is saved, deleted, or moved in the backing database store. How you track this depends on the details of the database class. For instance, in DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase>, Hypothesis installs a filesystem monitor via watchdog <https://pypi.org/project/watchdog/> in order to broadcast change events.
Two useful related methods are _start_listening() <#hypothesis.database.ExampleDatabase._start_listening> and _stop_listening() <#hypothesis.database.ExampleDatabase._stop_listening>, which a database class can override to know when to start or stop expensive listening operations. See documentation for details.
Detect Hypothesis tests
How to dynamically determine whether a test function has been defined with Hypothesis.
Via is_hypothesis_test
The most straightforward way is to use is_hypothesis_test() <#hypothesis.is_hypothesis_test>:
from hypothesis import is_hypothesis_test @given(st.integers()) def f(n): ... assert is_hypothesis_test(f)
This works for stateful tests as well:
from hypothesis import is_hypothesis_test from hypothesis.stateful import RuleBasedStateMachine class MyStateMachine(RuleBasedStateMachine): ... assert is_hypothesis_test(MyStateMachine.TestCase().runTest)
Via pytest
If you're working with pytest <https://pypi.org/project/pytest/>, the Hypothesis pytest plugin <#pytest-plugin> automatically adds the @pytest.mark.hypothesis mark to all Hypothesis tests. You can use node.get_closest_marker("hypothesis") or similar methods to detect the existence of this mark.
Use Hypothesis with an external fuzzer
Sometimes you might want to point a traditional fuzzer like python-afl <https://github.com/jwilk/python-afl> or Google's atheris <https://pypi.org/project/atheris/> at your code, to get coverage-guided exploration of native C extensions. The associated tooling is often much less mature than property-based testing libraries though, so you might want to use Hypothesis strategies to describe your input data, and our world-class shrinking and observability <#observability> tools to wrangle the results. That's exactly what this how-to guide is about!
Note:
If you already have Hypothesis tests and want to fuzz them, or are targeting pure Python code, we strongly recommend the purpose-built HypoFuzz <https://hypofuzz.com/>. This page is about writing traditional 'fuzz harnesses' with an external fuzzer, using parts of Hypothesis.
In order to support this workflow, Hypothesis exposes the fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input> method. fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input> takes a bytestring, parses it into a test case, and executes the corresponding test once. This means you can treat each of your Hypothesis tests as a traditional fuzz target, by pointing the fuzzer at fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input>.
For example:
from hypothesis import given, strategies as st
@given(st.integers())
def test_ints(n):
pass
# this parses the bytestring into a test case using st.integers(),
# and then executes `test_ints` once.
test_ints.hypothesis.fuzz_one_input(b"\x00" * 50)Note that fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input> bypasses the standard test lifecycle. In a standard test run, Hypothesis is responsible for managing the lifecycle of a test, for example by moving between each Phase <#hypothesis.Phase>. In contrast, fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input> executes one test case, independent of this lifecycle.
See the documentation of fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input> for details of how it interacts with other features of Hypothesis, such as @settings <#hypothesis.settings>.
Worked example: using Atheris
Here is an example that uses fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input> with the Atheris <https://github.com/google/atheris> coverage-guided fuzzer (which is built on top of libFuzzer <https://llvm.org/docs/LibFuzzer.html>):
import json
import sys
import atheris
from hypothesis import given, strategies as st
@given(
st.recursive(
st.none() | st.booleans() | st.integers() | st.floats() | st.text(),
lambda j: st.lists(j) | st.dictionaries(st.text(), j),
)
)
def test_json_dumps_valid_json(value):
json.dumps(value)
atheris.Setup(sys.argv, test_json_dumps_valid_json.hypothesis.fuzz_one_input)
atheris.Fuzz()Generating valid JSON objects based only on Atheris' FuzzDataProvider interface would be considerably more difficult.
You may also want to use atheris.instrument_all or atheris.instrument_imports in order to add coverage instrumentation to Atheris. See the Atheris <https://github.com/google/atheris> documentation for full details.
Explanations
These explanation pages are oriented towards deepening your understanding of Hypothesis, including its design philosophy.
Domain and distribution
- Note:
This page is primarily for users who may be familiar with other property-based testing libraries, and who expect control over the distribution of inputs in Hypothesis, via e.g. a scale parameter for size or a frequency parameter for relative probabilities.
Hypothesis makes a distinction between the domain of a strategy, and the distribution of a strategy.
The domain is the set of inputs that should be possible to generate. For instance, in lists(integers()), the domain is lists of integers. For other strategies, particularly those that use @composite <#hypothesis.strategies.composite> or assume() <#hypothesis.assume> in their definition, the domain might be more complex.
The distribution is the probability with which different elements in the domain should be generated. For lists(integers()), should Hypothesis generate many small lists? Large lists? More positive or more negative numbers? etc.
Hypothesis takes a philosophical stance that while users may be responsible for selecting the domain, the property-based testing library—not the user—should be responsible for selecting the distribution. As an intentional design choice, Hypothesis therefore lets you control the domain of inputs to your test, but not the distribution.
How should I choose a domain for my test?
We recommend using the most-general strategy for your test, so that it can in principle generate any edge case for which the test should pass. Limiting the size of generated inputs, and especially limiting the variety of inputs, can all too easily exclude the bug-triggering values from consideration - and be the difference between a test which finds the bug, or fails to do so.
Sometimes size limits are necessary for performance reasons, but we recommend limiting your strategies only after you've seen substantial slowdowns without limits. Far better to find bugs slowly, than not find them at all - and you can manage performance with the phases <#hypothesis.settings.phases> or max_examples <#hypothesis.settings.max_examples> settings rather than weakening the test.
Why not let users control the distribution?
There are a few reasons Hypothesis doesn't let users control the distribution.
- Humans are pretty bad at choosing bug-finding distributions! Some bugs are "known unknowns": you suspected that a part of the codebase was buggy in a particular way. Others are "unknown unknowns": you didn't know that a bug was possible until stumbling across it. Humans tend to overtune distributions for the former kind of bug, and not enough for the latter.
- The ideal strategy distribution depends not only on the codebase, but also on the property being tested. A strategy used in many places may have a good distribution for one property, but not another.
- The distribution of inputs is a deeply internal implementation detail. We sometimes change strategy distributions, either explicitly, or implicitly from other work on the Hypothesis engine. Exposing this would lock us into a public API that may make improvements to Hypothesis more difficult.
Finally, we think distribution control is better handled with alternative backends <#alternative-backends>. If existing backends like hypofuzz and crosshair don't suit your needs, you can also write your own. Backends can automatically generalize and adapt to the strategy and property being tested and avoid most of the problems above.
We're not saying that control over the distribution isn't useful! We occasionally receive requests to expose the distribution in Hypothesis (e.g. <https://github.com/HypothesisWorks/hypothesis/issues/4205>), and users wouldn't be asking for it if it wasn't. However, adding this to the public strategy API would make it easy for users to unknowingly weaken their tests, and would add maintenance overhead to Hypothesis, and so we haven't yet done so.
Okay, but what is the distribution?
An exact answer depends on both the strategy or strategies for the tests, and the code being tested - but we can make some general remarks, starting with "it's actually really complicated".
Hypothesis' default configuration uses a distribution which is tuned to maximize the chance of finding bugs, in as few executions as possible. We explicitly don't aim for a uniform distribution, nor for a 'realistic' distribution of inputs; Hypothesis' goal is to search the domain for a failing input as efficiently as possible.
The test case distribution remains an active area of research and development, and we change it whenever we think that would be a net improvement for users. Today, Hypothesis' default distribution is shaped by a wide variety of techniques and heuristics:
- some are statically designed into strategies - for example, integers() <#hypothesis.strategies.integers> upweights range endpoints, and samples from a mixed distribution over integer bit-widths.
- some are dynamic features of the engine - like replaying prior examples with subsections of the input 'cloned' or otherwise altered, for bugs which trigger only when different fields have the same value (which is otherwise exponentially unlikely).
- some vary depending on the code under test - we collect interesting-looking constants from imported source files as seeds for test cases.
And as if that wasn't enough, alternative backends <#alternative-backends> can radically change the distribution again - for example hypofuzz <https://pypi.org/project/hypofuzz/> uses runtime feedback to modify the distribution of inputs as the test runs, to maximize the rate at which we trigger new behaviors in that particular test and code. If Hypothesis' defaults aren't strong enough, we recommend trying Hypofuzz!
How many times will Hypothesis run my test?
This is a trickier question than you might expect. The short answer is "exactly max_examples <#hypothesis.settings.max_examples> times", with the following exceptions:
- Less than max_examples <#hypothesis.settings.max_examples> times, if Hypothesis exhausts the search space early.
More than max_examples <#hypothesis.settings.max_examples> times, if Hypothesis retries some examples because either:
- They failed an assume() <#hypothesis.assume> or .filter() <#hypothesis.strategies.SearchStrategy.filter> condition, or
- They were too large to continue generating.
- Either less or more than max_examples <#hypothesis.settings.max_examples> times, if Hypothesis finds a failing example.
Read on for details.
Search space exhaustion
If Hypothesis detects that there are no more examples left to try, it may stop generating examples before it hits max_examples <#hypothesis.settings.max_examples>. For example:
from hypothesis import given, strategies as st
calls = 0
@given(st.integers(0, 19))
def test_function(n):
global calls
calls += 1
test_function()
assert calls == 20This runs test_function 20 times, not 100, since there are only 20 unique integers to try.
The search space tracking in Hypothesis is good, but not perfect. We treat this more as a bonus than something to strive for.
assume() <#hypothesis.assume> and .filter() <#hypothesis.strategies.SearchStrategy.filter>
If an example fails to satisfy an assume() <#hypothesis.assume> or .filter() <#hypothesis.strategies.SearchStrategy.filter> condition, Hypothesis will retry generating that example and will not count it towards the max_examples <#hypothesis.settings.max_examples> limit. For instance:
from hypothesis import assume, given, strategies as st
@given(st.integers())
def test_function(n):
assume(n % 2 == 0)will run roughly 200 times, since half of the examples are discarded from the assume() <#hypothesis.assume>.
Note that while failing an assume() <#hypothesis.assume> triggers an immediate retry of the entire example, Hypothesis will try several times in the same example to satisfy a .filter() <#hypothesis.strategies.SearchStrategy.filter> condition. This makes expressing the same condition using .filter() <#hypothesis.strategies.SearchStrategy.filter> more efficient than assume() <#hypothesis.assume>.
Also note that even if your code does not explicitly use assume() <#hypothesis.assume> or .filter() <#hypothesis.strategies.SearchStrategy.filter>, a builtin strategy may still use them and cause retries. We try to directly satisfy conditions where possible instead of relying on rejection sampling, so this should be relatively uncommon.
Examples which are too large
For performance reasons, Hypothesis places an internal limit on the size of a single example. If an example exceeds this size limit, we will retry generating it and will not count it towards the max_examples <#hypothesis.settings.max_examples> limit. (And if we see too many of these large examples, we will raise HealthCheck.data_too_large <#hypothesis.HealthCheck.data_too_large>, unless suppressed with settings.suppress_health_check <#hypothesis.settings.suppress_health_check>).
The specific value of this size limit is an undocumented implementation detail. The majority of Hypothesis tests do not come close to hitting it.
Failing examples
If Hypothesis finds a failing example, it stops generation early, and may call the test function additional times during the Phase.shrink <#hypothesis.Phase.shrink> and Phase.explain <#hypothesis.Phase.explain> phases. Sometimes, Hypothesis determines that the initial failing example was already as simple as possible, in which case Phase.shrink <#hypothesis.Phase.shrink> will not result in additional test executions (but Phase.explain <#hypothesis.Phase.explain> might).
Regardless of whether Hypothesis runs the test during the shrinking and explain phases, it will always run the minimal failing example one additional time to check for flakiness. For instance, the following trivial test runs with n=0 twice, even though it only uses the Phase.generate <#hypothesis.Phase.generate> phase:
from hypothesis import Phase, given, settings, strategies as st
@given(st.integers())
@settings(phases=[Phase.generate])
def test_function(n):
print(f"called with {n}")
assert n != 0
test_function()The first execution finds the initial failure with n=0, and the second execution replays n=0 to ensure the failure is not flaky.
API Reference
The technical API reference for Hypothesis is split into four pages:
- API Reference <>. Non-strategy Hypothesis objects, classes, and functions. @given <#hypothesis.given> and others live here.
- Strategies Reference <>. Hypothesis strategies, including for extras <>.
- Integrations Reference <>. Features with a defined interface, but no code API.
- Hypothesis internals <>. Internal APIs for developers building tools, libraries, or research on top of Hypothesis.
API Reference
Reference for non-strategy objects that are part of the Hypothesis API. For documentation on strategies, see the strategies reference <>.
@given
- hypothesis.given(*_given_arguments, **_given_kwargs)
The @given decorator turns a function into a Hypothesis test. This is the main entry point to Hypothesis.
See also:
See also the Introduction to Hypothesis <> tutorial, which introduces defining Hypothesis tests with @given.
Arguments to @given
Arguments to @given may be either positional or keyword arguments:
@given(st.integers(), st.floats())
def test_one(x, y):
pass
@given(x=st.integers(), y=st.floats())
def test_two(x, y):
passIf using keyword arguments, the arguments may appear in any order, as with standard Python functions:
# different order, but still equivalent to before
@given(y=st.floats(), x=st.integers())
def test(x, y):
assert isinstance(x, int)
assert isinstance(y, float)If @given is provided fewer positional arguments than the decorated test, the test arguments are filled in on the right side, leaving the leftmost positional arguments unfilled:
@given(st.integers(), st.floats())
def test(manual_string, y, z):
assert manual_string == "x"
assert isinstance(y, int)
assert isinstance(z, float)
# `test` is now a callable which takes one argument `manual_string`
test("x")
# or equivalently:
test(manual_string="x")The reason for this "from the right" behavior is to support using @given with instance methods, by automatically passing through self:
class MyTest(TestCase):
@given(st.integers())
def test(self, x):
assert isinstance(self, MyTest)
assert isinstance(x, int)If (and only if) using keyword arguments, @given may be combined with **kwargs or *args:
@given(x=integers(), y=integers())
def test(x, **kwargs):
assert "y" in kwargs
@given(x=integers(), y=integers())
def test(x, *args, **kwargs):
assert args == ()
assert "x" not in kwargs
assert "y" in kwargsIt is an error to:
- Mix positional and keyword arguments to @given.
- Use @given with a function that has a default value for an argument.
- Use @given with positional arguments with a function that uses *args, **kwargs, or keyword-only arguments.
The function returned by given has all the same arguments as the original test, minus those that are filled in by @given. See the notes on framework compatibility <#framework-compatibility> for how this interacts with features of other testing libraries, such as pytest <https://pypi.org/project/pytest/> fixtures.
- hypothesis.infer
An alias for ... (python:Ellipsis). infer can be passed to @given or builds() <#hypothesis.strategies.builds> to indicate that a strategy for that parameter should be inferred from its type annotations.
In all cases, using infer is equivalent to using ....
Inferred strategies
In some cases, Hypothesis can work out what to do when you omit arguments. This is based on introspection, not magic, and therefore has well-defined limits.
builds() <#hypothesis.strategies.builds> will check the signature of the target (using python:inspect.signature()). If there are required arguments with type annotations and no strategy was passed to builds() <#hypothesis.strategies.builds>, from_type() <#hypothesis.strategies.from_type> is used to fill them in. You can also pass the value ... (Ellipsis) as a keyword argument, to force this inference for arguments with a default value.
>>> def func(a: int, b: str): ... return [a, b] ... >>> builds(func).example() [-6993, '']
@given does not perform any implicit inference for required arguments, as this would break compatibility with pytest fixtures. ... (python:Ellipsis), can be used as a keyword argument to explicitly fill in an argument from its type annotation. You can also use the infer alias if writing a literal ... seems too weird.
@given(a=...) # or @given(a=infer)
def test(a: int):
pass
# is equivalent to
@given(a=from_type(int))
def test(a):
pass@given(...) can also be specified to fill all arguments from their type annotations.
@given(...)
def test(a: int, b: str):
pass
# is equivalent to
@given(a=..., b=...)
def test(a, b):
passLimitations
Hypothesis does not inspect PEP 484 <https://peps.python.org/pep-0484/> type comments at runtime. While from_type() <#hypothesis.strategies.from_type> will work as usual, inference in builds() <#hypothesis.strategies.builds> and @given will only work if you manually create the __annotations__ attribute (e.g. by using @annotations(...) and @returns(...) decorators).
The python:typing module changes between different Python releases, including at minor versions. These are all supported on a best-effort basis, but you may encounter problems. Please report them to us, and consider updating to a newer version of Python as a workaround.
Explicit inputs
See also:
See also the Replaying failed tests <> tutorial, which discusses using explicit inputs to reproduce failures.
- class hypothesis.example(*args, **kwargs)
Add an explicit input to a Hypothesis test, which Hypothesis will always try before generating random inputs. This combines the randomized nature of Hypothesis generation with a traditional parametrized test.
For example:
@example("Hello world") @example("some string with special significance") @given(st.text()) def test_strings(s): passwill call test_strings("Hello World") and test_strings("some string with special significance") before generating any random inputs. @example may be placed in any order relative to @given and @settings.
Explicit inputs from @example are run in the Phase.explicit phase. Explicit inputs do not count towards settings.max_examples. Note that explicit inputs added by @example do not shrink. If an explicit input fails, Hypothesis will stop and report the failure without generating any random inputs.
@example can also be used to easily reproduce a failure. For instance, if Hypothesis reports that f(n=[0, math.nan]) fails, you can add @example(n=[0, math.nan]) to your test to quickly reproduce that failure.
Arguments to @example
Arguments to @example have the same behavior and restrictions as arguments to @given. This means they may be either positional or keyword arguments (but not both in the same @example):
@example(1, 2)
@example(x=1, y=2)
@given(st.integers(), st.integers())
def test(x, y):
passNoting that while arguments to @given are strategies (like integers() <#hypothesis.strategies.integers>), arguments to @example are values instead (like 1).
See the Arguments to @given section for full details.
- example.xfail(condition=True, *, reason='', raises=<class 'BaseException'>)
Mark this example as an expected failure, similarly to pytest.mark.xfail(strict=True).
Expected-failing examples allow you to check that your test does fail on some examples, and therefore build confidence that passing tests are because your code is working, not because the test is missing something.
@example(...).xfail() @example(...).xfail(reason="Prices must be non-negative") @example(...).xfail(raises=(KeyError, ValueError)) @example(...).xfail(sys.version_info[:2] >= (3, 12), reason="needs py 3.12") @example(...).xfail(condition=sys.platform != "linux", raises=OSError) def test(x): pass- Note:
Expected-failing examples are handled separately from those generated by strategies, so you should usually ensure that there is no overlap.
@example(x=1, y=0).xfail(raises=ZeroDivisionError) @given(x=st.just(1), y=st.integers()) # Missing `.filter(bool)`! def test_fraction(x, y): # This test will try the explicit example and see it fail as # expected, then go on to generate more examples from the # strategy. If we happen to generate y=0, the test will fail # because only the explicit example is treated as xfailing. x / y
- example.via(whence, /)
Attach a machine-readable label noting what the origin of this example was. example.via is completely optional and does not change runtime behavior.
example.via is intended to support self-documenting behavior, as well as tooling which might add (or remove) @example decorators automatically. For example:
# Annotating examples is optional and does not change runtime behavior @example(...) @example(...).via("regression test for issue #42") @example(...).via("discovered failure") def test(x): pass- Note:
HypoFuzz <https://hypofuzz.com/> uses example.via to tag examples in the patch of its high-coverage set of explicit inputs, on the patches page <https://hypofuzz.com/example-dashboard/#/patches>.
Reproducing inputs
See also:
See also the Replaying failed tests <> tutorial.
- hypothesis.reproduce_failure(version, blob)
Run the example corresponding to the binary blob in order to reproduce a failure. blob is a serialized version of the internal input representation of Hypothesis.
A test decorated with @reproduce_failure always runs exactly one example, which is expected to cause a failure. If the provided blob does not cause a failure, Hypothesis will raise DidNotReproduce.
Hypothesis will print an @reproduce_failure decorator if settings.print_blob is True (which is the default in CI).
@reproduce_failure is intended to be temporarily added to your test suite in order to reproduce a failure. It is not intended to be a permanent addition to your test suite. Because of this, no compatibility guarantees are made across Hypothesis versions, and @reproduce_failure will error if used on a different Hypothesis version than it was created for.
See also:
See also the Replaying failed tests <> tutorial.
- hypothesis.seed(seed)
Seed the randomness for this test.
seed may be any hashable object. No exact meaning for seed is provided other than that for a fixed seed value Hypothesis will produce the same examples (assuming that there are no other sources of nondeterminisim, such as timing, hash randomization, or external state).
For example, the following test function and RuleBasedStateMachine will each generate the same series of examples each time they are executed:
@seed(1234) @given(st.integers()) def test(n): ... @seed(6789) class MyMachine(RuleBasedStateMachine): ...
If using pytest, you can alternatively pass --hypothesis-seed on the command line.
Setting a seed overrides settings.derandomize, which is designed to enable deterministic CI tests rather than reproducing observed failures.
Hypothesis will only print the seed which would reproduce a failure if a test fails in an unexpected way, for instance inside Hypothesis internals.
Control
Functions that can be called from anywhere inside a test, to either modify how Hypothesis treats the current test case, or to give Hypothesis more information about the current test case.
- hypothesis.assume(condition)
Calling assume is like an assert that marks the example as bad, rather than failing the test.
This allows you to specify properties that you assume will be true, and let Hypothesis try to avoid similar examples in future.
- hypothesis.note(value)
Report this value for the minimal failing example.
- hypothesis.event(value, payload='')
Record an event that occurred during this test. Statistics on the number of test runs with each event will be reported at the end if you run Hypothesis in statistics reporting mode.
Event values should be strings or convertible to them. If an optional payload is given, it will be included in the string for Test statistics <#statistics>.
You can mark custom events in a test using event():
from hypothesis import event, given, strategies as st
@given(st.integers().filter(lambda x: x % 2 == 0))
def test_even_integers(i):
event(f"i mod 3 = {i%3}")These events appear in observability <#observability> output, as well as the output of our pytest plugin <#pytest-plugin> when run with --hypothesis-show-statistics.
For instance, in the latter case, you would see output like:
test_even_integers:
- during generate phase (0.09 seconds):
- Typical runtimes: < 1ms, ~ 59% in data generation
- 100 passing examples, 0 failing examples, 32 invalid examples
- Events:
* 54.55%, Retried draw from integers().filter(lambda x: x % 2 == 0) to satisfy filter
* 31.06%, i mod 3 = 2
* 28.79%, i mod 3 = 0
* 24.24%, Aborted test because unable to satisfy integers().filter(lambda x: x % 2 == 0)
* 15.91%, i mod 3 = 1
- Stopped because settings.max_examples=100Arguments to event can be any hashable type, but two events will be considered the same if they are the same when converted to a string with python:str.
Targeted property-based testing
Targeted property-based testing combines the advantages of both search-based and property-based testing. Instead of being completely random, targeted PBT uses a search-based component to guide the input generation towards values that have a higher probability of falsifying a property. This explores the input space more effectively and requires fewer tests to find a bug or achieve a high confidence in the system being tested than random PBT. (Löscher and Sagonas <http://proper.softlab.ntua.gr/Publications.html>)
This is not always a good idea - for example calculating the search metric might take time better spent running more uniformly-random test cases, or your target metric might accidentally lead Hypothesis away from bugs - but if there is a natural metric like "floating-point error", "load factor" or "queue length", we encourage you to experiment with targeted testing.
We recommend that users also skim the papers introducing targeted PBT; from ISSTA 2017 <http://proper.softlab.ntua.gr/papers/issta2017.pdf> and ICST 2018 <http://proper.softlab.ntua.gr/papers/icst2018.pdf>. For the curious, the initial implementation in Hypothesis uses hill-climbing search via a mutating fuzzer, with some tactics inspired by simulated annealing to avoid getting stuck and endlessly mutating a local maximum.
from hypothesis import given, strategies as st, target
@given(st.floats(0, 1e100), st.floats(0, 1e100), st.floats(0, 1e100))
def test_associativity_with_target(a, b, c):
ab_c = (a + b) + c
a_bc = a + (b + c)
difference = abs(ab_c - a_bc)
target(difference) # Without this, the test almost always passes
assert difference < 2.0- hypothesis.target(observation, *, label='')
Calling this function with an int or float observation gives it feedback with which to guide our search for inputs that will cause an error, in addition to all the usual heuristics. Observations must always be finite.
Hypothesis will try to maximize the observed value over several examples; almost any metric will work so long as it makes sense to increase it. For example, -abs(error) is a metric that increases as error approaches zero.
Example metrics:
- Number of elements in a collection, or tasks in a queue
- Mean or maximum runtime of a task (or both, if you use label)
- Compression ratio for data (perhaps per-algorithm or per-level)
- Number of steps taken by a state machine
The optional label argument can be used to distinguish between and therefore separately optimise distinct observations, such as the mean and standard deviation of a dataset. It is an error to call target() with any label more than once per test case.
Note:
The more examples you run, the better this technique works.
As a rule of thumb, the targeting effect is noticeable above max_examples=1000, and immediately obvious by around ten thousand examples per label used by your test.
Test statistics <#statistics> include the best score seen for each label, which can help avoid the threshold problem <https://hypothesis.works/articles/threshold-problem/> when the minimal example shrinks right down to the threshold of failure (issue #2180 <https://github.com/HypothesisWorks/hypothesis/issues/2180>).
Settings
See also:
See also the tutorial for settings <>.
- class hypothesis.settings(parent=None, *, max_examples=not_set, derandomize=not_set, database=not_set, verbosity=not_set, phases=not_set, stateful_step_count=not_set, report_multiple_bugs=not_set, suppress_health_check=not_set, deadline=not_set, print_blob=not_set, backend=not_set)
A settings object controls the following aspects of test behavior: max_examples, derandomize, database, verbosity, phases, stateful_step_count, report_multiple_bugs, suppress_health_check, deadline, print_blob, and backend.
A settings object can be applied as a decorator to a test function, in which case that test function will use those settings. A test may only have one settings object applied to it. A settings object can also be passed to register_profile() or as a parent to another settings.
Attribute inheritance
Settings objects are immutable once created. When a settings object is created, it uses the value specified for each attribute. Any attribute which is not specified will inherit from its value in the parent settings object. If parent is not passed, any attributes which are not specified will inherit from the current settings profile instead.
For instance, settings(max_examples=10) will have a max_examples of 10, and the value of all other attributes will be equal to its value in the current settings profile.
Changes made from activating a new settings profile with load_profile() will be reflected in settings objects created after the profile was loaded, but not in existing settings objects.
Built-in profiles
While you can register additional profiles with register_profile(), Hypothesis comes with two built-in profiles: default and ci.
By default, the default profile is active. If the CI environment variable is set to any value, the ci profile is active by default. Hypothesis also automatically detects various vendor-specific CI environment variables.
The attributes of the currently active settings profile can be retrieved with settings() (so settings().max_examples is the currently active default for settings.max_examples).
The settings attributes for the built-in profiles are as follows:
default = settings.register_profile(
"default",
max_examples=100,
derandomize=False,
database=not_set, # see settings.database for default behavior
verbosity=Verbosity.normal,
phases=tuple(Phase),
stateful_step_count=50,
report_multiple_bugs=True,
suppress_health_check=(),
deadline=duration(milliseconds=200),
print_blob=False,
backend="hypothesis",
)
ci = settings.register_profile(
"ci",
parent=default,
derandomize=True,
deadline=None,
database=None,
print_blob=True,
suppress_health_check=[HealthCheck.too_slow],
)You can replace either of the built-in profiles with register_profile():
# run more examples in CI
settings.register_profile(
"ci",
settings.get_profile("ci"),
max_examples=1000,
)- property max_examples
Once this many satisfying examples have been considered without finding any counter-example, Hypothesis will stop looking.
Note that we might call your test function fewer times if we find a bug early or can tell that we've exhausted the search space; or more if we discard some examples due to use of .filter(), assume(), or a few other things that can prevent the test case from completing successfully.
The default value is chosen to suit a workflow where the test will be part of a suite that is regularly executed locally or on a CI server, balancing total running time against the chance of missing a bug.
If you are writing one-off tests, running tens of thousands of examples is quite reasonable as Hypothesis may miss uncommon bugs with default settings. For very complex code, we have observed Hypothesis finding novel bugs after several million examples while testing SymPy <https://pypi.org/project/sympy/>. If you are running more than 100k examples for a test, consider using our integration for coverage-guided fuzzing - it really shines when given minutes or hours to run.
The default max examples is 100.
- property derandomize
If True, seed Hypothesis' random number generator using a hash of the test function, so that every run will test the same set of examples until you update Hypothesis, Python, or the test function.
This allows you to check for regressions and look for bugs <https://blog.nelhage.com/post/two-kinds-of-testing/> using separate settings profiles - for example running quick deterministic tests on every commit, and a longer non-deterministic nightly testing run.
The default is False. If running on CI, the default is True instead.
- property database
An instance of ExampleDatabase that will be used to save examples to and load previous examples from.
If not set, a DirectoryBasedExampleDatabase is created in the current working directory under .hypothesis/examples. If this location is unusable, e.g. due to the lack of read or write permissions, Hypothesis will emit a warning and fall back to an InMemoryExampleDatabase.
If None, no storage will be used.
See the database documentation for a list of database classes, and how to define custom database classes.
- property verbosity
Control the verbosity level of Hypothesis messages.
To see what's going on while Hypothesis runs your tests, you can turn up the verbosity setting.
>>> from hypothesis import settings, Verbosity >>> from hypothesis.strategies import lists, integers >>> @given(lists(integers())) ... @settings(verbosity=Verbosity.verbose) ... def f(x): ... assert not any(x) ... f() Trying example: [] Falsifying example: [-1198601713, -67, 116, -29578] Shrunk example to [-1198601713] Shrunk example to [-128] Shrunk example to [32] Shrunk example to [1] [1]
The four levels are Verbosity.quiet, Verbosity.normal, Verbosity.verbose, and Verbosity.debug. Verbosity.normal is the default. For Verbosity.quiet, Hypothesis will not print anything out, not even the final falsifying example. Verbosity.debug is basically Verbosity.verbose but a bit more so. You probably don't want it.
Verbosity can be passed either as a Verbosity enum value, or as the corresponding string value, or as the corresponding integer value. For example:
# these three are equivalent settings(verbosity=Verbosity.verbose) settings(verbosity="verbose")
If you are using pytest <https://pypi.org/project/pytest/>, you may also need to disable output capturing for passing tests to see verbose output as tests run.
- property phases
Control which phases should be run.
Hypothesis divides tests into logically distinct phases.
- Phase.explicit: Running explicit examples from @example.
- Phase.reuse: Running examples from the database which previously failed.
- Phase.generate: Generating new random examples.
- Phase.target: Mutating examples for targeted property-based testing. Requires Phase.generate.
- Phase.shrink: Shrinking failing examples.
- Phase.explain: Attempting to explain why a failure occurred. Requires Phase.shrink.
The phases argument accepts a collection with any subset of these. E.g. settings(phases=[Phase.generate, Phase.shrink]) will generate new examples and shrink them, but will not run explicit examples or reuse previous failures, while settings(phases=[Phase.explicit]) will only run explicit examples from @example.
Phases can be passed either as a Phase enum value, or as the corresponding string value. For example:
# these two are equivalent settings(phases=[Phase.explicit]) settings(phases=["explicit"])
Following the first failure, Hypothesis will (usually, depending on which Phase is enabled) track which lines of code are always run on failing but never on passing inputs. On 3.12+, this uses sys.monitoring, while 3.11 and earlier uses python:sys.settrace(). For python 3.11 and earlier, we therefore automatically disable the explain phase on PyPy, or if you are using coverage <https://pypi.org/project/coverage/> or a debugger. If there are no clearly suspicious lines of code, we refuse the temptation to guess <https://peps.python.org/pep-0020/>.
After shrinking to a minimal failing example, Hypothesis will try to find parts of the example -- e.g. separate args to @given -- which can vary freely without changing the result of that minimal failing example. If the automated experiments run without finding a passing variation, we leave a comment in the final report:
test_x_divided_by_y( x=0, # or any other generated value y=0, )Just remember that the lack of an explanation sometimes just means that Hypothesis couldn't efficiently find one, not that no explanation (or simpler failing example) exists.
- property stateful_step_count
The maximum number of times to call an additional @rule method in stateful testing before we give up on finding a bug.
Note that this setting is effectively multiplicative with max_examples, as each example will run for a maximum of stateful_step_count steps.
The default stateful step count is 50.
- property report_multiple_bugs
Because Hypothesis runs the test many times, it can sometimes find multiple bugs in a single run. Reporting all of them at once is usually very useful, but replacing the exceptions can occasionally clash with debuggers. If disabled, only the exception with the smallest minimal example is raised.
The default value is True.
- property suppress_health_check
Suppress the given HealthCheck exceptions. Those health checks will not be raised by Hypothesis. To suppress all health checks, you can pass suppress_health_check=list(HealthCheck).
Health checks can be passed either as a HealthCheck enum value, or as the corresponding string value. For example:
# these two are equivalent settings(suppress_health_check=[HealthCheck.filter_too_much]) settings(suppress_health_check=["filter_too_much"])
Health checks are proactive warnings, not correctness errors, so we encourage suppressing health checks where you have evaluated they will not pose a problem, or where you have evaluated that fixing the underlying issue is not worthwhile.
See also:
See also the Suppress a health check everywhere <> how-to.
- property deadline
The maximum allowed duration of an individual test case, in milliseconds. You can pass an integer, float, or timedelta. If None, the deadline is disabled entirely.
We treat the deadline as a soft limit in some cases, where that would avoid flakiness due to timing variability.
The default deadline is 200 milliseconds. If running on CI, the default is None instead.
- property print_blob
If set to True, Hypothesis will print code for failing examples that can be used with @reproduce_failure to reproduce the failing example.
The default value is False. If running on CI, the default is True instead.
- property backend
- Warning:
EXPERIMENTAL AND UNSTABLE - see Alternative backends for Hypothesis <#alternative-backends>.
The importable name of a backend which Hypothesis should use to generate primitive types. We support heuristic-random, solver-based, and fuzzing-based backends.
- static register_profile(name, parent=None, **kwargs)
Register a settings object as a settings profile, under the name name. The parent and kwargs arguments to this method are as for settings.
If a settings profile already exists under name, it will be overwritten. Registering a profile with the same name as the currently active profile will cause those changes to take effect in the active profile immediately, and do not require reloading the profile.
Registered settings profiles can be retrieved later by name with get_profile().
- static get_profile(name)
Returns the settings profile registered under name. If no settings profile is registered under name, raises InvalidArgument.
- static load_profile(name)
Makes the settings profile registered under name the active profile.
If no settings profile is registered under name, raises InvalidArgument.
- static get_current_profile_name()
The name of the current settings profile. For example:
>>> settings.load_profile("myprofile") >>> settings.get_current_profile_name() 'myprofile'
- class hypothesis.Phase(*values)
Options for the settings.phases argument to @settings.
- explicit = 'explicit'
Controls whether explicit examples are run.
- reuse = 'reuse'
Controls whether previous examples will be reused.
- generate = 'generate'
Controls whether new examples will be generated.
- target = 'target'
Controls whether examples will be mutated for targeting.
- shrink = 'shrink'
Controls whether examples will be shrunk.
- explain = 'explain'
Controls whether Hypothesis attempts to explain test failures.
The explain phase has two parts, each of which is best-effort - if Hypothesis can't find a useful explanation, we'll just print the minimal failing example.
- class hypothesis.Verbosity(*values)
Options for the settings.verbosity argument to @settings.
- quiet = 'quiet'
Hypothesis will not print any output, not even the final falsifying example.
- normal = 'normal'
Standard verbosity. Hypothesis will print the falsifying example, alongside any notes made with note() (only for the falsfying example).
- verbose = 'verbose'
Increased verbosity. In addition to everything in Verbosity.normal, Hypothesis will:
- Print each test case as it tries it
- Print any notes made with note() for each test case
- Print each shrinking attempt
- Print all explicit failing examples when using @example, instead of only the simplest one
- debug = 'debug'
Even more verbosity. Useful for debugging Hypothesis internals. You probably don't want this.
- class hypothesis.HealthCheck(*values)
A HealthCheck is proactively raised by Hypothesis when Hypothesis detects that your test has performance problems, which may result in less rigorous testing than you expect. For example, if your test takes a long time to generate inputs, or filters away too many inputs using assume() or .filter() <#hypothesis.strategies.SearchStrategy.filter>, Hypothesis will raise a corresponding health check.
A health check is a proactive warning, not an error. We encourage suppressing health checks where you have evaluated they will not pose a problem, or where you have evaluated that fixing the underlying issue is not worthwhile.
With the exception of HealthCheck.function_scoped_fixture and HealthCheck.differing_executors, all health checks warn about performance problems, not correctness errors.
Disabling health checks
Health checks can be disabled by settings.suppress_health_check. To suppress all health checks, you can pass suppress_health_check=list(HealthCheck).
See also:
See also the Suppress a health check everywhere <> how-to.
Correctness health checks
Some health checks report potential correctness errors, rather than performance problems.
- HealthCheck.function_scoped_fixture indicates that a function-scoped pytest fixture is used by an @given test. Many Hypothesis users expect function-scoped fixtures to reset once per input, but they actually reset once per test. We proactively raise HealthCheck.function_scoped_fixture to ensure you have considered this case.
- HealthCheck.differing_executors indicates that the same @given test has been executed multiple times with multiple distinct executors.
We recommend treating these particular health checks with more care, as suppressing them may result in an unsound test.
- data_too_large = 'data_too_large'
Checks if too many examples are aborted for being too large.
This is measured by the number of random choices that Hypothesis makes in order to generate something, not the size of the generated object. For example, choosing a 100MB object from a predefined list would take only a few bits, while generating 10KB of JSON from scratch might trigger this health check.
- filter_too_much = 'filter_too_much'
Check for when the test is filtering out too many examples, either through use of assume() or .filter() <#hypothesis.strategies.SearchStrategy.filter>, or occasionally for Hypothesis internal reasons.
- too_slow = 'too_slow'
Check for when input generation is very slow. Since Hypothesis generates 100 (by default) inputs per test execution, a slowdown in generating each input can result in very slow tests overall.
- return_value = 'return_value'
Deprecated; we always error if a test returns a non-None value.
- large_base_example = 'large_base_example'
Checks if the smallest natural input to your test is very large. This makes it difficult for Hypothesis to generate good inputs, especially when trying to shrink failing inputs.
- not_a_test_method = 'not_a_test_method'
Deprecated; we always error if @given is applied to a method defined by python:unittest.TestCase (i.e. not a test).
- function_scoped_fixture = 'function_scoped_fixture'
Checks if @given has been applied to a test with a pytest function-scoped fixture. Function-scoped fixtures run once for the whole function, not once per example, and this is usually not what you want.
Because of this limitation, tests that need to set up or reset state for every example need to do so manually within the test itself, typically using an appropriate context manager.
Suppress this health check only in the rare case that you are using a function-scoped fixture that does not need to be reset between individual examples, but for some reason you cannot use a wider fixture scope (e.g. session scope, module scope, class scope).
This check requires the Hypothesis pytest plugin <#pytest-plugin>, which is enabled by default when running Hypothesis inside pytest.
- differing_executors = 'differing_executors'
Checks if @given has been applied to a test which is executed by different executors. If your test function is defined as a method on a class, that class will be your executor, and subclasses executing an inherited test is a common way for things to go wrong.
The correct fix is often to bring the executor instance under the control of hypothesis by explicit parametrization over, or sampling from, subclasses, or to refactor so that @given is specified on leaf subclasses.
- nested_given = 'nested_given'
Checks if @given is used inside another @given. This results in quadratic generation and shrinking behavior, and can usually be expressed more cleanly by using data() <#hypothesis.strategies.data> to replace the inner @given.
Nesting @given can be appropriate if you set appropriate limits for the quadratic behavior and cannot easily reexpress the inner function with data() <#hypothesis.strategies.data>. To suppress this health check, set suppress_health_check=[HealthCheck.nested_given] on the outer @given. Setting it on the inner @given has no effect. If you have more than one level of nesting, add a suppression for this health check to every @given except the innermost one.
Database
- class hypothesis.database.ExampleDatabase
A Hypothesis database, for use in settings.database.
Hypothesis automatically saves failures to the database set in settings.database. The next time the test is run, Hypothesis will replay any failures from the database in settings.database for that test (in Phase.reuse).
The database is best thought of as a cache that you never need to invalidate. Entries may be transparently dropped when upgrading your Hypothesis version or changing your test. Do not rely on the database for correctness; to ensure Hypothesis always tries an input, use @example.
A Hypothesis database is a simple mapping of bytes to sets of bytes. Hypothesis provides several concrete database subclasses. To write your own database class, see Write a custom Hypothesis database <>.
Change listening
An optional extension to ExampleDatabase is change listening. On databases which support change listening, calling add_listener() adds a function as a change listener, which will be called whenever a value is added, deleted, or moved inside the database. See add_listener() for details.
All databases in Hypothesis support change listening. Custom database classes are not required to support change listening, though they will not be compatible with features that require change listening until they do so.
Note:
While no Hypothesis features currently require change listening, change listening is required by HypoFuzz <https://hypofuzz.com/>.
Database methods
Required methods:
- save()
- fetch()
- delete()
Optional methods:
- move()
Change listening methods:
- add_listener()
- remove_listener()
- clear_listeners()
- _start_listening()
- _stop_listening()
- _broadcast_change()
- abstractmethod save(key, value)
Save value under key.
If value is already present in key, silently do nothing.
- abstractmethod fetch(key)
Return an iterable over all values matching this key.
- abstractmethod delete(key, value)
Remove value from key.
If value is not present in key, silently do nothing.
- move(src, dest, value)
Move value from key src to key dest.
Equivalent to delete(src, value) followed by save(src, value), but may have a more efficient implementation.
Note that value will be inserted at dest regardless of whether it is currently present at src.
- add_listener(f, /)
Add a change listener. f will be called whenever a value is saved, deleted, or moved in the database.
f can be called with two different event values:
- ("save", (key, value))
- ("delete", (key, value))
where key and value are both bytes.
There is no move event. Instead, a move is broadcasted as a delete event followed by a save event.
For the delete event, value may be None. This might occur if the database knows that a deletion has occurred in key, but does not know what value was deleted.
- remove_listener(f, /)
Removes f from the list of change listeners.
If f is not in the list of change listeners, silently do nothing.
- clear_listeners()
Remove all change listeners.
- _broadcast_change(event)
Called when a value has been either added to or deleted from a key in the underlying database store. The possible values for event are:
- ("save", (key, value))
- ("delete", (key, value))
value may be None for the delete event, indicating we know that some value was deleted under this key, but not its exact value.
Note that you should not assume your instance is the only reference to the underlying database store. For example, if two instances of DirectoryBasedExampleDatabase reference the same directory, _broadcast_change should be called whenever a file is added or removed from the directory, even if that database was not responsible for changing the file.
- _start_listening()
Called when the database adds a change listener, and did not previously have any change listeners. Intended to allow databases to wait to start expensive listening operations until necessary.
_start_listening and _stop_listening are guaranteed to alternate, so you do not need to handle the case of multiple consecutive _start_listening calls without an intermediate _stop_listening call.
- _stop_listening()
Called whenever no change listeners remain on the database.
_stop_listening and _start_listening are guaranteed to alternate, so you do not need to handle the case of multiple consecutive _stop_listening calls without an intermediate _start_listening call.
- class hypothesis.database.InMemoryExampleDatabase
A non-persistent example database, implemented in terms of an in-memory dictionary.
This can be useful if you call a test function several times in a single session, or for testing other database implementations, but because it does not persist between runs we do not recommend it for general use.
- class hypothesis.database.DirectoryBasedExampleDatabase(path)
Use a directory to store Hypothesis examples as files.
Each test corresponds to a directory, and each example to a file within that directory. While the contents are fairly opaque, a DirectoryBasedExampleDatabase can be shared by checking the directory into version control, for example with the following .gitignore:
# Ignore files cached by Hypothesis... .hypothesis/* # except for the examples directory !.hypothesis/examples/
Note however that this only makes sense if you also pin to an exact version of Hypothesis, and we would usually recommend implementing a shared database with a network datastore - see ExampleDatabase, and the MultiplexedDatabase helper.
- class hypothesis.database.GitHubArtifactDatabase(owner, repo, artifact_name='hypothesis-example-db', cache_timeout=datetime.timedelta(days=1), path=None)
A file-based database loaded from a GitHub Actions <https://docs.github.com/en/actions> artifact.
You can use this for sharing example databases between CI runs and developers, allowing the latter to get read-only access to the former. This is particularly useful for continuous fuzzing (i.e. with HypoFuzz <https://hypofuzz.com/>), where the CI system can help find new failing examples through fuzzing, and developers can reproduce them locally without any manual effort.
Note:
You must provide GITHUB_TOKEN as an environment variable. In CI, Github Actions provides this automatically, but it needs to be set manually for local usage. In a developer machine, this would usually be a Personal Access Token <https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens>. If the repository is private, it's necessary for the token to have repo scope in the case of a classic token, or actions:read in the case of a fine-grained token.
In most cases, this will be used through the MultiplexedDatabase, by combining a local directory-based database with this one. For example:
local = DirectoryBasedExampleDatabase(".hypothesis/examples") shared = ReadOnlyDatabase(GitHubArtifactDatabase("user", "repo")) settings.register_profile("ci", database=local) settings.register_profile("dev", database=MultiplexedDatabase(local, shared)) # We don't want to use the shared database in CI, only to populate its local one. # which the workflow should then upload as an artifact. settings.load_profile("ci" if os.environ.get("CI") else "dev")- Note:
Because this database is read-only, you always need to wrap it with the ReadOnlyDatabase.
A setup like this can be paired with a GitHub Actions workflow including something like the following:
- name: Download example database uses: dawidd6/action-download-artifact@v9 with: name: hypothesis-example-db path: .hypothesis/examples if_no_artifact_found: warn workflow_conclusion: completed - name: Run tests run: pytest - name: Upload example database uses: actions/upload-artifact@v3 if: always() with: name: hypothesis-example-db path: .hypothesis/examplesIn this workflow, we use dawidd6/action-download-artifact <https://github.com/dawidd6/action-download-artifact> to download the latest artifact given that the official actions/download-artifact <https://github.com/actions/download-artifact> does not support downloading artifacts from previous workflow runs.
The database automatically implements a simple file-based cache with a default expiration period of 1 day. You can adjust this through the cache_timeout property.
For mono-repo support, you can provide a unique artifact_name (e.g. hypofuzz-example-db-frontend).
- class hypothesis.database.ReadOnlyDatabase(db)
A wrapper to make the given database read-only.
The implementation passes through fetch, and turns save, delete, and move into silent no-ops.
Note that this disables Hypothesis' automatic discarding of stale examples. It is designed to allow local machines to access a shared database (e.g. from CI servers), without propagating changes back from a local or in-development branch.
- class hypothesis.database.MultiplexedDatabase(*dbs)
A wrapper around multiple databases.
Each save, fetch, move, or delete operation will be run against all of the wrapped databases. fetch does not yield duplicate values, even if the same value is present in two or more of the wrapped databases.
This combines well with a ReadOnlyDatabase, as follows:
local = DirectoryBasedExampleDatabase("/tmp/hypothesis/examples/") shared = CustomNetworkDatabase() settings.register_profile("ci", database=shared) settings.register_profile( "dev", database=MultiplexedDatabase(local, ReadOnlyDatabase(shared)) ) settings.load_profile("ci" if os.environ.get("CI") else "dev")So your CI system or fuzzing runs can populate a central shared database; while local runs on development machines can reproduce any failures from CI but will only cache their own failures locally and cannot remove examples from the shared database.
- class hypothesis.database.BackgroundWriteDatabase(db)
A wrapper which defers writes on the given database to a background thread.
Calls to fetch() wait for any enqueued writes to finish before fetching from the database.
- class hypothesis.extra.redis.RedisExampleDatabase(redis, *, expire_after=datetime.timedelta(days=8), key_prefix=b'hypothesis-example:', listener_channel='hypothesis-changes')
Store Hypothesis examples as sets in the given Redis datastore.
This is particularly useful for shared databases, as per the recipe for a MultiplexedDatabase.
Note:
If a test has not been run for expire_after, those examples will be allowed to expire. The default time-to-live persists examples between weekly runs.
Stateful tests
- class hypothesis.stateful.RuleBasedStateMachine
A RuleBasedStateMachine gives you a structured way to define state machines.
The idea is that a state machine carries the system under test and some supporting data. This data can be stored in instance variables or divided into Bundles. The state machine has a set of rules which may read data from bundles (or just from normal strategies), push data onto bundles, change the state of the machine, or verify properties. At any given point a random applicable rule will be executed.
Rules
- hypothesis.stateful.rule(*, targets=(), target=None, **kwargs)
Decorator for RuleBasedStateMachine. Any Bundle present in target or targets will define where the end result of this function should go. If both are empty then the end result will be discarded.
target must be a Bundle, or if the result should be replicated to multiple bundles you can pass a tuple of them as the targets argument. It is invalid to use both arguments for a single rule. If the result should go to exactly one of several bundles, define a separate rule for each case.
kwargs then define the arguments that will be passed to the function invocation. If their value is a Bundle, or if it is consumes(b) where b is a Bundle, then values that have previously been produced for that bundle will be provided. If consumes is used, the value will also be removed from the bundle.
Any other kwargs should be strategies and values from them will be provided.
- hypothesis.stateful.consumes(bundle)
When introducing a rule in a RuleBasedStateMachine, this function can be used to mark bundles from which each value used in a step with the given rule should be removed. This function returns a strategy object that can be manipulated and combined like any other.
For example, a rule declared with
@rule(value1=b1, value2=consumes(b2), value3=lists(consumes(b3)))
will consume a value from Bundle b2 and several values from Bundle b3 to populate value2 and value3 each time it is executed.
- hypothesis.stateful.multiple(*args)
This function can be used to pass multiple results to the target(s) of a rule. Just use return multiple(result1, result2, ...) in your rule.
It is also possible to use return multiple() with no arguments in order to end a rule without passing any result.
- class hypothesis.stateful.Bundle(name, *, consume=False, draw_references=True)
A collection of values for use in stateful testing.
Bundles are a kind of strategy where values can be added by rules, and (like any strategy) used as inputs to future rules.
The name argument they are passed is the they are referred to internally by the state machine; no two bundles may have the same name. It is idiomatic to use the attribute being assigned to as the name of the Bundle:
class MyStateMachine(RuleBasedStateMachine): keys = Bundle("keys")Bundles can contain the same value more than once; this becomes relevant when using consumes() to remove values again.
If the consume argument is set to True, then all values that are drawn from this bundle will be consumed (as above) when requested.
- hypothesis.stateful.initialize(*, targets=(), target=None, **kwargs)
Decorator for RuleBasedStateMachine.
An initialize decorator behaves like a rule, but all @initialize() decorated methods will be called before any @rule() decorated methods, in an arbitrary order. Each @initialize() method will be called exactly once per run, unless one raises an exception - after which only the .teardown() method will be run. @initialize() methods may not have preconditions.
- hypothesis.stateful.precondition(precond)
Decorator to apply a precondition for rules in a RuleBasedStateMachine. Specifies a precondition for a rule to be considered as a valid step in the state machine, which is more efficient than using assume() within the rule. The precond function will be called with the instance of RuleBasedStateMachine and should return True or False. Usually it will need to look at attributes on that instance.
For example:
class MyTestMachine(RuleBasedStateMachine): state = 1 @precondition(lambda self: self.state != 0) @rule(numerator=integers()) def divide_with(self, numerator): self.state = numerator / self.stateIf multiple preconditions are applied to a single rule, it is only considered a valid step when all of them return True. Preconditions may be applied to invariants as well as rules.
- hypothesis.stateful.invariant(*, check_during_init=False)
Decorator to apply an invariant for rules in a RuleBasedStateMachine. The decorated function will be run after every rule and can raise an exception to indicate failed invariants.
For example:
class MyTestMachine(RuleBasedStateMachine): state = 1 @invariant() def is_nonzero(self): assert self.state != 0By default, invariants are only checked after all @initialize() rules have been run. Pass check_during_init=True for invariants which can also be checked during initialization.
Running state machines
If you want to bypass the TestCase infrastructure you can invoke these manually. The stateful module exposes run_state_machine_as_test(), which takes an arbitrary function returning a RuleBasedStateMachine and an optional settings parameter and does the same as the class based runTest provided.
- hypothesis.stateful.run_state_machine_as_test(state_machine_factory, *, settings=None, _min_steps=0)
Run a state machine definition as a test, either silently doing nothing or printing a minimal breaking program and raising an exception.
state_machine_factory is anything which returns an instance of RuleBasedStateMachine when called with no arguments - it can be a class or a function. settings will be used to control the execution of the test.
Hypothesis exceptions
Custom exceptions raised by Hypothesis.
- class hypothesis.errors.HypothesisException
Generic parent class for exceptions thrown by Hypothesis.
- class hypothesis.errors.HypothesisDeprecationWarning
A deprecation warning issued by Hypothesis.
Actually inherits from FutureWarning, because DeprecationWarning is hidden by the default warnings filter.
You can configure the python:warnings module to handle these warnings differently to others, either turning them into errors or suppressing them entirely. Obviously we would prefer the former!
- class hypothesis.errors.NonInteractiveExampleWarning
Emitted when .example() <#hypothesis.strategies.SearchStrategy.example> is used outside of interactive use.
.example() <#hypothesis.strategies.SearchStrategy.example> is intended for exploratory and interactive work, not to be run as part of a test suite.
- class hypothesis.errors.Flaky
Base class for indeterministic failures. Usually one of the more specific subclasses (FlakyFailure or FlakyStrategyDefinition) is raised.
See also:
See also the flaky failures tutorial <>.
- class hypothesis.errors.FlakyStrategyDefinition
This function appears to cause inconsistent data generation.
- Common causes for this problem are:
- 1.
The strategy depends on external state. e.g. it uses an external random number generator. Try to make a version that passes all the relevant state in from Hypothesis.
See also:
See also the flaky failures tutorial <>.
- class hypothesis.errors.FlakyFailure(msg, group)
This function appears to fail non-deterministically: We have seen it fail when passed this example at least once, but a subsequent invocation did not fail, or caused a distinct error.
- Common causes for this problem are:
- The function depends on external state. e.g. it uses an external random number generator. Try to make a version that passes all the relevant state in from Hypothesis.
- The function is suffering from too much recursion and its failure depends sensitively on where it's been called from.
- The function is timing sensitive and can fail or pass depending on how long it takes. Try breaking it up into smaller functions which don't do that and testing those instead.
See also:
See also the flaky failures tutorial <>.
- class hypothesis.errors.FlakyBackendFailure(msg, group)
A failure was reported by an alternative backend <#alternative-backends>, but this failure did not reproduce when replayed under the Hypothesis backend.
When an alternative backend reports a failure, Hypothesis first replays it under the standard Hypothesis backend to check for flakiness. If the failure does not reproduce, Hypothesis raises this exception.
- class hypothesis.errors.InvalidArgument
Used to indicate that the arguments to a Hypothesis function were in some manner incorrect.
- class hypothesis.errors.ResolutionFailed
Hypothesis had to resolve a type to a strategy, but this failed.
Type inference is best-effort, so this only happens when an annotation exists but could not be resolved for a required argument to the target of builds(), or where the user passed ....
- class hypothesis.errors.Unsatisfiable
We ran out of time or examples before we could find enough examples which satisfy the assumptions of this hypothesis.
This could be because the function is too slow. If so, try upping the timeout. It could also be because the function is using assume in a way that is too hard to satisfy. If so, try writing a custom strategy or using a better starting point (e.g if you are requiring a list has unique values you could instead filter out all duplicate values from the list)
class hypothesis.errors.DidNotReproduce
- class hypothesis.errors.DeadlineExceeded(runtime, deadline)
Raised when an input takes too long to run, relative to the settings.deadline setting.
Django
See also:
See the Django strategies reference <#django-strategies> for documentation on strategies in the hypothesis.extra.django module.
Hypothesis offers a number of features specific for Django testing, available in the hypothesis[django] extra <>. This is tested against each supported series with mainstream or extended support - if you're still getting security patches, you can test with Hypothesis.
class hypothesis.extra.django.TestCase(*args, **kwargs)
Using it is quite straightforward: All you need to do is subclass hypothesis.extra.django.TestCase or hypothesis.extra.django.SimpleTestCase or hypothesis.extra.django.TransactionTestCase or LiveServerTestCase or StaticLiveServerTestCase and you can use @given as normal, and the transactions will be per example rather than per test function as they would be if you used @given with a normal django test suite (this is important because your test function will be called multiple times and you don't want them to interfere with each other). Test cases on these classes that do not use @given will be run as normal for django:django.test.TestCase or django:django.test.TransactionTestCase.
class hypothesis.extra.django.SimpleTestCase(*args, **kwargs)
class hypothesis.extra.django.TransactionTestCase(*args, **kwargs)
class hypothesis.extra.django.LiveServerTestCase(*args, **kwargs)
class hypothesis.extra.django.StaticLiveServerTestCase(*args, **kwargs)
We recommend avoiding TransactionTestCase unless you really have to run each test case in a database transaction. Because Hypothesis runs this in a loop, the performance problems django:django.test.TransactionTestCase normally has are significantly exacerbated and your tests will be really slow. If you are using TransactionTestCase, you may need to use @settings(suppress_health_check=[HealthCheck.too_slow]) to avoid a HealthCheck error due to slow example generation.
Having set up a test class, you can now pass @given a strategy for Django models with from_model() <#hypothesis.extra.django.from_model>. For example, using the trivial django project we have for testing <https://github.com/HypothesisWorks/hypothesis/blob/master/hypothesis-python/tests/django/toystore/models.py>:
>>> from hypothesis.extra.django import from_model >>> from toystore.models import Customer >>> c = from_model(Customer).example() >>> c <Customer: Customer object> >>> c.email 'jaime.urbina@gmail.com' >>> c.name '\U00109d3d\U000e07be\U000165f8\U0003fabf\U000c12cd\U000f1910\U00059f12\U000519b0\U0003fabf\U000f1910\U000423fb\U000423fb\U00059f12\U000e07be\U000c12cd\U000e07be\U000519b0\U000165f8\U0003fabf\U0007bc31' >>> c.age -873375803
Hypothesis has just created this with whatever the relevant type of data is.
Obviously the customer's age is implausible, which is only possible because we have not used (eg) MinValueValidator to set the valid range for this field (or used a PositiveSmallIntegerField, which would only need a maximum value validator).
If you do have validators attached, Hypothesis will only generate examples that pass validation. Sometimes that will mean that we fail a HealthCheck because of the filtering, so let's explicitly pass a strategy to skip validation at the strategy level:
>>> from hypothesis.strategies import integers >>> c = from_model(Customer, age=integers(min_value=0, max_value=120)).example() >>> c <Customer: Customer object> >>> c.age 5
Custom field types
If you have a custom Django field type you can register it with Hypothesis's model deriving functionality by registering a default strategy for it:
>>> from toystore.models import CustomishField, Customish
>>> from_model(Customish).example()
hypothesis.errors.InvalidArgument: Missing arguments for mandatory field
customish for model Customish
>>> from hypothesis.extra.django import register_field_strategy
>>> from hypothesis.strategies import just
>>> register_field_strategy(CustomishField, just("hi"))
>>> x = from_model(Customish).example()
>>> x.customish
'hi'Note that this mapping is on exact type. Subtypes will not inherit it.
Generating child models
For the moment there's no explicit support in hypothesis-django for generating dependent models. i.e. a Company model will generate no Shops. However if you want to generate some dependent models as well, you can emulate this by using the .flatmap() <#hypothesis.strategies.SearchStrategy.flatmap> function as follows:
from hypothesis.strategies import just, lists
def generate_with_shops(company):
return lists(from_model(Shop, company=just(company))).map(lambda _: company)
company_with_shops_strategy = from_model(Company).flatmap(generate_with_shops)Let's unpack what this is doing:
The way flatmap works is that we draw a value from the original strategy, then apply a function to it which gives us a new strategy. We then draw a value from that strategy. So in this case we're first drawing a company, and then we're drawing a list of shops belonging to that company: The just() <#hypothesis.strategies.just> strategy is a strategy such that drawing it always produces the individual value, so from_model(Shop, company=just(company)) is a strategy that generates a Shop belonging to the original company.
So the following code would give us a list of shops all belonging to the same company:
from_model(Company).flatmap(lambda c: lists(from_model(Shop, company=just(c))))
The only difference from this and the above is that we want the company, not the shops. This is where the inner map comes in. We build the list of shops and then throw it away, instead returning the company we started for. This works because the models that Hypothesis generates are saved in the database, so we're essentially running the inner strategy purely for the side effect of creating those children in the database.
Generating primary key values
If your model includes a custom primary key that you want to generate using a strategy (rather than a default auto-increment primary key) then Hypothesis has to deal with the possibility of a duplicate primary key.
If a model strategy generates a value for the primary key field, Hypothesis will create the model instance with update_or_create(), overwriting any existing instance in the database for this test case with the same primary key.
On the subject of MultiValueField
Django forms feature the MultiValueField which allows for several fields to be combined under a single named field, the default example of this is the SplitDateTimeField.
class CustomerForm(forms.Form):
name = forms.CharField()
birth_date_time = forms.SplitDateTimeField()from_form() <#hypothesis.extra.django.from_form> supports MultiValueField subclasses directly, however if you want to define your own strategy be forewarned that Django binds data for a MultiValueField in a peculiar way. Specifically each sub-field is expected to have its own entry in data addressed by the field name (e.g. birth_date_time) and the index of the sub-field within the MultiValueField, so form data for the example above might look like this:
{
"name": "Samuel John",
"birth_date_time_0": "2018-05-19", # the date, as the first sub-field
"birth_date_time_1": "15:18:00", # the time, as the second sub-field
}Thus, if you want to define your own strategies for such a field you must address your sub-fields appropriately:
from_form(CustomerForm, birth_date_time_0=just("2018-05-19"))External fuzzers
- hypothesis.core.HypothesisHandle.fuzz_one_input = <property object>
Run the test as a fuzz target, driven with the buffer of bytes.
Depending on the passed buffer one of three things will happen:
- If the bytestring was invalid, for example because it was too short or was filtered out by assume() or .filter() <#hypothesis.strategies.SearchStrategy.filter>, fuzz_one_input() returns None.
- If the bytestring was valid and the test passed, fuzz_one_input() returns a canonicalised and pruned bytestring which will replay that test case. This is provided as an option to improve the performance of mutating fuzzers, but can safely be ignored.
- If the test failed, i.e. raised an exception, fuzz_one_input() will add the pruned buffer to the Hypothesis example database and then re-raise that exception. All you need to do to reproduce, minimize, and de-duplicate all the failures found via fuzzing is run your test suite!
To reduce the performance impact of database writes, fuzz_one_input() only records failing inputs which would be valid shrinks for a known failure - meaning writes are somewhere between constant and log(N) rather than linear in runtime. However, this tracking only works within a persistent fuzzing process; for forkserver fuzzers we recommend database=None for the main run, and then replaying with a database enabled if you need to analyse failures.
Note that the interpretation of both input and output bytestrings is specific to the exact version of Hypothesis you are using and the strategies given to the test, just like the database and @reproduce_failure.
Interaction with @settings
fuzz_one_input() uses just enough of Hypothesis' internals to drive your test function with a bytestring, and most settings therefore have no effect in this mode. We recommend running your tests the usual way before fuzzing to get the benefits of health checks, as well as afterwards to replay, shrink, deduplicate, and report whatever errors were discovered.
- settings.database is used by fuzz_one_input() - adding failures to the database to be replayed when you next run your tests is our preferred reporting mechanism and response to the 'fuzzer taming' problem <https://blog.regehr.org/archives/925>.
- settings.verbosity and settings.stateful_step_count work as usual.
- The deadline, derandomize, max_examples, phases, print_blob, report_multiple_bugs, and suppress_health_check settings do not affect fuzz_one_input().
Example Usage
@given(st.text()) def test_foo(s): ... # This is a traditional fuzz target - call it with a bytestring, # or a binary IO object, and it runs the test once. fuzz_target = test_foo.hypothesis.fuzz_one_input # For example: fuzz_target(b"\x00\x00\x00\x00\x00\x00\x00\x00") fuzz_target(io.BytesIO(b"\x01"))
- Tip:
If you expect to discover many failures while using fuzz_one_input(), consider wrapping your database with BackgroundWriteDatabase, for low-overhead writes of failures.
Tip:
Want an integrated workflow for your team's local tests, CI, and continuous fuzzing? Use HypoFuzz <https://hypofuzz.com/> to fuzz your whole test suite, and find more bugs with the same tests!
See also:
See also the Use Hypothesis with an external fuzzer <> how-to.
Custom function execution
Hypothesis provides you with a hook that lets you control how it runs examples.
This lets you do things like set up and tear down around each example, run examples in a subprocess, transform coroutine tests into normal tests, etc. For example, TransactionTestCase in the Django extra runs each example in a separate database transaction.
The way this works is by introducing the concept of an executor. An executor is essentially a function that takes a block of code and run it. The default executor is:
def default_executor(function):
return function()You define executors by defining a method execute_example on a class. Any test methods on that class with @given used on them will use self.execute_example as an executor with which to run tests. For example, the following executor runs all its code twice:
from unittest import TestCase
class TestTryReallyHard(TestCase):
@given(integers())
def test_something(self, i):
perform_some_unreliable_operation(i)
def execute_example(self, f):
f()
return f()Note: The functions you use in map, etc. will run inside the executor. i.e. they will not be called until you invoke the function passed to execute_example.
An executor must be able to handle being passed a function which returns None, otherwise it won't be able to run normal test cases. So for example the following executor is invalid:
from unittest import TestCase
class TestRunTwice(TestCase):
def execute_example(self, f):
return f()()and should be rewritten as:
from unittest import TestCase
class TestRunTwice(TestCase):
def execute_example(self, f):
result = f()
if callable(result):
result = result()
return resultAn alternative hook is provided for use by test runner extensions such as pytest-trio <https://pypi.org/project/pytest-trio/>, which cannot use the execute_example method. This is not recommended for end-users - it is better to write a complete test function directly, perhaps by using a decorator to perform the same transformation before applying @given.
@given(x=integers()) @pytest.mark.trio async def test(x): ... # Illustrative code, inside the pytest-trio plugin test.hypothesis.inner_test = lambda x: trio.run(test, x)
For authors of test runners however, assigning to the inner_test attribute of the hypothesis attribute of the test will replace the interior test.
Note:
The new inner_test must accept and pass through all the *args and **kwargs expected by the original test.
If the end user has also specified a custom executor using the execute_example method, it - and all other execution-time logic - will be applied to the new inner test assigned by the test runner.
Detection
- hypothesis.is_hypothesis_test(f)
Returns True if f represents a test function that has been defined with Hypothesis. This is true for:
- Functions decorated with @given
- The runTest method of stateful tests
For example:
@given(st.integers()) def f(n): ... class MyStateMachine(RuleBasedStateMachine): ... assert is_hypothesis_test(f) assert is_hypothesis_test(MyStateMachine.TestCase().runTest)
See also:
See also the Detect Hypothesis tests <> how-to.
- hypothesis.currently_in_test_context()
Return True if the calling code is currently running inside an @given or stateful test, and False otherwise.
This is useful for third-party integrations and assertion helpers which may be called from either traditional or property-based tests, and can only use e.g. assume() or target() in the latter case.
Strategies Reference
Strategies are the way Hypothesis describes the values for @given <#hypothesis.given> to generate. For instance, passing the strategy st.lists(st.integers(), min_size=1) to @given <#hypothesis.given> tells Hypothesis to generate lists of integers with at least one element.
This reference page lists all of Hypothesis' first-party functions which return a strategy. There are also many provided by third-party libraries <>. Note that we often say "strategy" when we mean "function returning a strategy"; it's usually clear from context which one we mean.
Strategies can be passed to other strategies as arguments, combined using combinator strategies, or modified using .filter(), .map(), or .flatmap().
Primitives
- hypothesis.strategies.none()
Return a strategy which only generates None.
Examples from this strategy do not shrink (because there is only one).
- hypothesis.strategies.nothing()
This strategy never successfully draws a value and will always reject on an attempt to draw.
Examples from this strategy do not shrink (because there are none).
- hypothesis.strategies.just(value)
Return a strategy which only generates value.
Note: value is not copied. Be wary of using mutable values.
If value is the result of a callable, you can use builds(callable) instead of just(callable()) to get a fresh value each time.
Examples from this strategy do not shrink (because there is only one).
- hypothesis.strategies.booleans()
Returns a strategy which generates instances of python:bool.
Examples from this strategy will shrink towards False (i.e. shrinking will replace True with False where possible).
Numeric
See also:
See also the separate sections for Numpy strategies, Pandas strategies, and Array API strategies.
- hypothesis.strategies.integers(min_value=None, max_value=None)
Returns a strategy which generates integers.
If min_value is not None then all values will be >= min_value. If max_value is not None then all values will be <= max_value
Examples from this strategy will shrink towards zero, and negative values will also shrink towards positive (i.e. -n may be replaced by +n).
- hypothesis.strategies.floats(min_value=None, max_value=None, *, allow_nan=None, allow_infinity=None, allow_subnormal=None, width=64, exclude_min=False, exclude_max=False)
Returns a strategy which generates floats.
- If min_value is not None, all values will be >= min_value (or > min_value if exclude_min).
- If max_value is not None, all values will be <= max_value (or < max_value if exclude_max).
- If min_value or max_value is not None, it is an error to enable allow_nan.
- If both min_value and max_value are not None, it is an error to enable allow_infinity.
- If inferred values range does not include subnormal values, it is an error to enable allow_subnormal.
Where not explicitly ruled out by the bounds, subnormals <https://en.wikipedia.org/wiki/Subnormal_number>, infinities, and NaNs are possible values generated by this strategy.
The width argument specifies the maximum number of bits of precision required to represent the generated float. Valid values are 16, 32, or 64. Passing width=32 will still use the builtin 64-bit python:float class, but always for values which can be exactly represented as a 32-bit float.
The exclude_min and exclude_max argument can be used to generate numbers from open or half-open intervals, by excluding the respective endpoints. Excluding either signed zero will also exclude the other. Attempting to exclude an endpoint which is None will raise an error; use allow_infinity=False to generate finite floats. You can however use e.g. min_value=-math.inf, exclude_min=True to exclude only one infinite endpoint.
Examples from this strategy have a complicated and hard to explain shrinking behaviour, but it tries to improve "human readability". Finite numbers will be preferred to infinity and infinity will be preferred to NaN.
- hypothesis.strategies.complex_numbers(*, min_magnitude=0, max_magnitude=None, allow_infinity=None, allow_nan=None, allow_subnormal=True, width=128)
Returns a strategy that generates python:complex numbers.
This strategy draws complex numbers with constrained magnitudes. The min_magnitude and max_magnitude parameters should be non-negative Real numbers; a value of None corresponds an infinite upper bound.
If min_magnitude is nonzero or max_magnitude is finite, it is an error to enable allow_nan. If max_magnitude is finite, it is an error to enable allow_infinity.
allow_infinity, allow_nan, and allow_subnormal are applied to each part of the complex number separately, as for floats().
The magnitude constraints are respected up to a relative error of (around) floating-point epsilon, due to implementation via the system sqrt function.
The width argument specifies the maximum number of bits of precision required to represent the entire generated complex number. Valid values are 32, 64 or 128, which correspond to the real and imaginary components each having width 16, 32 or 64, respectively. Passing width=64 will still use the builtin 128-bit python:complex class, but always for values which can be exactly represented as two 32-bit floats.
Examples from this strategy shrink by shrinking their real and imaginary parts, as floats().
If you need to generate complex numbers with particular real and imaginary parts or relationships between parts, consider using builds(complex, ...) or @composite respectively.
- hypothesis.strategies.decimals(min_value=None, max_value=None, *, allow_nan=None, allow_infinity=None, places=None)
Generates instances of python:decimal.Decimal, which may be:
- A finite rational number, between min_value and max_value.
- Not a Number, if allow_nan is True. None means "allow NaN, unless min_value and max_value are not None".
- Positive or negative infinity, if max_value and min_value respectively are None, and allow_infinity is not False. None means "allow infinity, unless excluded by the min and max values".
Note that where floats have one NaN value, Decimals have four: signed, and either quiet or signalling. See the decimal module docs <https://docs.python.org/3/library/decimal.html#special-values> for more information on special values.
If places is not None, all finite values drawn from the strategy will have that number of digits after the decimal place.
Examples from this strategy do not have a well defined shrink order but try to maximize human readability when shrinking.
- hypothesis.strategies.fractions(min_value=None, max_value=None, *, max_denominator=None)
Returns a strategy which generates Fractions.
If min_value is not None then all generated values are no less than min_value. If max_value is not None then all generated values are no greater than max_value. min_value and max_value may be anything accepted by the Fraction constructor.
If max_denominator is not None then the denominator of any generated values is no greater than max_denominator. Note that max_denominator must be None or a positive integer.
Examples from this strategy shrink towards smaller denominators, then closer to zero.
Strings
- Note:
The uuids() and ip_addresses() strategies generate instances of UUID and IPAddress respectively. You can generate corresponding string values by using .map(), such as st.uuids().map(str).
- hypothesis.strategies.text(alphabet=characters(codec='utf-8'), *, min_size=0, max_size=None)
Generates strings with characters drawn from alphabet, which should be a collection of length one strings or a strategy generating such strings.
The default alphabet strategy can generate the full unicode range but excludes surrogate characters because they are invalid in the UTF-8 encoding. You can use characters() without arguments to find surrogate-related bugs such as bpo-34454 <https://bugs.python.org/issue34454>.
min_size and max_size have the usual interpretations. Note that Python measures string length by counting codepoints: U+00C5 Å is a single character, while U+0041 U+030A Å is two - the A, and a combining ring above.
Examples from this strategy shrink towards shorter strings, and with the characters in the text shrinking as per the alphabet strategy. This strategy does not normalize() examples, so generated strings may be in any or none of the 'normal forms'.
- hypothesis.strategies.characters(*, codec=None, min_codepoint=None, max_codepoint=None, categories=None, exclude_categories=None, exclude_characters=None, include_characters=None)
Generates characters, length-one python:strings, following specified filtering rules.
- When no filtering rules are specified, any character can be produced.
- If min_codepoint or max_codepoint is specified, then only characters having a codepoint in that range will be produced.
- If categories is specified, then only characters from those Unicode categories will be produced. This is a further restriction, characters must also satisfy min_codepoint and max_codepoint.
- If exclude_categories is specified, then any character from those categories will not be produced. You must not pass both categories and exclude_categories; these arguments are alternative ways to specify exactly the same thing.
- If include_characters is specified, then any additional characters in that list will also be produced.
- If exclude_characters is specified, then any characters in that list will be not be produced. Any overlap between include_characters and exclude_characters will raise an exception.
- If codec is specified, only characters in the specified codec encodings <https://docs.python.org/3/library/codecs.html#encodings-and-unicode> will be produced.
The _codepoint arguments must be integers between zero and python:sys.maxunicode. The _characters arguments must be collections of length-one unicode strings, such as a unicode string.
The _categories arguments must be used to specify either the one-letter Unicode major category or the two-letter Unicode general category <https://en.wikipedia.org/wiki/Unicode_character_property>. For example, ('Nd', 'Lu') signifies "Number, decimal digit" and "Letter, uppercase". A single letter ('major category') can be given to match all corresponding categories, for example 'P' for characters in any punctuation category.
We allow codecs from the codecs module and their aliases, platform specific and user-registered codecs if they are available, and python-specific text encodings <https://docs.python.org/3/library/codecs.html#python-specific-encodings> (but not text or binary transforms). include_characters which cannot be encoded using this codec will raise an exception. If non-encodable codepoints or categories are explicitly allowed, the codec argument will exclude them without raising an exception.
Examples from this strategy shrink towards the codepoint for '0', or the first allowable codepoint after it if '0' is excluded.
- hypothesis.strategies.from_regex(regex, *, fullmatch=False, alphabet=None)
Generates strings that contain a match for the given regex (i.e. ones for which python:re.search() will return a non-None result).
regex may be a pattern or compiled regex. Both byte-strings and unicode strings are supported, and will generate examples of the same type.
You can use regex flags such as python:re.IGNORECASE or python:re.DOTALL to control generation. Flags can be passed either in compiled regex or inside the pattern with a (?iLmsux) group.
Some regular expressions are only partly supported - the underlying strategy checks local matching and relies on filtering to resolve context-dependent expressions. Using too many of these constructs may cause health-check errors as too many examples are filtered out. This mainly includes (positive or negative) lookahead and lookbehind groups.
If you want the generated string to match the whole regex you should use boundary markers. So e.g. r"\A.\Z" will return a single character string, while "." will return any string, and r"\A.$" will return a single character optionally followed by a "\n". Alternatively, passing fullmatch=True will ensure that the whole string is a match, as if you had used the \A and \Z markers.
The alphabet= argument constrains the characters in the generated string, as for text(), and is only supported for unicode strings.
Examples from this strategy shrink towards shorter strings and lower character values, with exact behaviour that may depend on the pattern.
- hypothesis.strategies.binary(*, min_size=0, max_size=None)
Generates python:bytes.
The generated python:bytes will have a length of at least min_size and at most max_size. If max_size is None there is no upper limit.
Examples from this strategy shrink towards smaller strings and lower byte values.
- hypothesis.strategies.emails(*, domains=domains())
A strategy for generating email addresses as unicode strings. The address format is specified in RFC 5322 Section 3.4.1 <https://datatracker.ietf.org/doc/html/rfc5322.html#section-3.4.1>. Values shrink towards shorter local-parts and host domains.
If domains is given then it must be a strategy that generates domain names for the emails, defaulting to domains().
This strategy is useful for generating "user data" for tests, as mishandling of email addresses is a common source of bugs.
- hypothesis.provisional.domains(*, max_length=255, max_element_length=63)
Generate RFC 1035 <https://datatracker.ietf.org/doc/html/rfc1035.html> compliant fully qualified domain names.
- Warning:
The domains() strategy is provisional. Its interface may be changed in a minor release, without being subject to our deprecation policy <#deprecation-policy>. That said, we expect it to be relatively stable.
- hypothesis.provisional.urls()
A strategy for RFC 3986 <https://datatracker.ietf.org/doc/html/rfc3986.html>, generating http/https URLs.
The generated URLs could, at least in theory, be passed to an HTTP client and fetched.
- Warning:
The urls() strategy is provisional. Its interface may be changed in a minor release, without being subject to our deprecation policy <#deprecation-policy>. That said, we expect it to be relatively stable.
Collections
- hypothesis.strategies.lists(elements, *, min_size=0, max_size=None, unique_by=None, unique=False)
Returns a list containing values drawn from elements with length in the interval [min_size, max_size] (no bounds in that direction if these are None). If max_size is 0, only the empty list will be drawn.
If unique is True (or something that evaluates to True), we compare direct object equality, as if unique_by was lambda x: x. This comparison only works for hashable types.
If unique_by is not None it must be a callable or tuple of callables returning a hashable type when given a value drawn from elements. The resulting list will satisfy the condition that for i != j, unique_by(result[i]) != unique_by(result[j]).
If unique_by is a tuple of callables the uniqueness will be respective to each callable.
For example, the following will produce two columns of integers with both columns being unique respectively.
>>> twoints = st.tuples(st.integers(), st.integers()) >>> st.lists(twoints, unique_by=(lambda x: x[0], lambda x: x[1]))
Examples from this strategy shrink by trying to remove elements from the list, and by shrinking each individual element of the list.
- hypothesis.strategies.tuples(*args)
Return a strategy which generates a tuple of the same length as args by generating the value at index i from args[i].
e.g. tuples(integers(), integers()) would generate a tuple of length two with both values an integer.
Examples from this strategy shrink by shrinking their component parts.
- hypothesis.strategies.sets(elements, *, min_size=0, max_size=None)
This has the same behaviour as lists, but returns sets instead.
Note that Hypothesis cannot tell if values are drawn from elements are hashable until running the test, so you can define a strategy for sets of an unhashable type but it will fail at test time.
Examples from this strategy shrink by trying to remove elements from the set, and by shrinking each individual element of the set.
- hypothesis.strategies.frozensets(elements, *, min_size=0, max_size=None)
This is identical to the sets function but instead returns frozensets.
- hypothesis.strategies.dictionaries(keys, values, *, dict_class=<class 'dict'>, min_size=0, max_size=None)
Generates dictionaries of type dict_class with keys drawn from the keys argument and values drawn from the values argument.
The size parameters have the same interpretation as for lists().
Examples from this strategy shrink by trying to remove keys from the generated dictionary, and by shrinking each generated key and value.
- hypothesis.strategies.fixed_dictionaries(mapping, *, optional=None)
Generates a dictionary of the same type as mapping with a fixed set of keys mapping to strategies. mapping must be a dict subclass.
Generated values have all keys present in mapping, in iteration order, with the corresponding values drawn from mapping[key].
If optional is passed, the generated value may or may not contain each key from optional and a value drawn from the corresponding strategy. Generated values may contain optional keys in an arbitrary order.
Examples from this strategy shrink by shrinking each individual value in the generated dictionary, and omitting optional key-value pairs.
- hypothesis.strategies.iterables(elements, *, min_size=0, max_size=None, unique_by=None, unique=False)
This has the same behaviour as lists, but returns iterables instead.
Some iterables cannot be indexed (e.g. sets) and some do not have a fixed length (e.g. generators). This strategy produces iterators, which cannot be indexed and do not have a fixed length. This ensures that you do not accidentally depend on sequence behaviour.
Datetime
- hypothesis.strategies.dates(min_value=datetime.date.min, max_value=datetime.date.max)
A strategy for dates between min_value and max_value.
Examples from this strategy shrink towards January 1st 2000.
- hypothesis.strategies.times(min_value=datetime.time.min, max_value=datetime.time.max, *, timezones=none())
A strategy for times between min_value and max_value.
The timezones argument is handled as for datetimes().
Examples from this strategy shrink towards midnight, with the timezone component shrinking as for the strategy that provided it.
- hypothesis.strategies.datetimes(min_value=datetime.datetime.min, max_value=datetime.datetime.max, *, timezones=none(), allow_imaginary=True)
A strategy for generating datetimes, which may be timezone-aware.
This strategy works by drawing a naive datetime between min_value and max_value, which must both be naive (have no timezone).
timezones must be a strategy that generates either None, for naive datetimes, or tzinfo objects for 'aware' datetimes. You can construct your own, though we recommend using one of these built-in strategies:
- with the standard library: hypothesis.strategies.timezones();
- with dateutil <https://pypi.org/project/python-dateutil/>: hypothesis.extra.dateutil.timezones(); or
- with pytz <https://pypi.org/project/pytz/>: hypothesis.extra.pytz.timezones().
You may pass allow_imaginary=False to filter out "imaginary" datetimes which did not (or will not) occur due to daylight savings, leap seconds, timezone and calendar adjustments, etc. Imaginary datetimes are allowed by default, because malformed timestamps are a common source of bugs.
Examples from this strategy shrink towards midnight on January 1st 2000, local time.
- hypothesis.strategies.timezones(*, no_cache=False)
A strategy for python:zoneinfo.ZoneInfo objects.
If no_cache=True, the generated instances are constructed using ZoneInfo.no_cache instead of the usual constructor. This may change the semantics of your datetimes in surprising ways, so only use it if you know that you need to!
Note:
The tzdata package is required on Windows <https://docs.python.org/3/library/zoneinfo.html#data-sources>. pip install hypothesis[zoneinfo] installs it, if and only if needed.
- hypothesis.strategies.timezone_keys(*, allow_prefix=True)
A strategy for IANA timezone names <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones>.
As well as timezone names like "UTC", "Australia/Sydney", or "America/New_York", this strategy can generate:
- Aliases such as "Antarctica/McMurdo", which links to "Pacific/Auckland".
- Deprecated names such as "Antarctica/South_Pole", which also links to "Pacific/Auckland". Note that most but not all deprecated timezone names are also aliases.
- Timezone names with the "posix/" or "right/" prefixes, unless allow_prefix=False.
These strings are provided separately from Tzinfo objects - such as ZoneInfo instances from the timezones() strategy - to facilitate testing of timezone logic without needing workarounds to access non-canonical names.
Note:
The tzdata package is required on Windows <https://docs.python.org/3/library/zoneinfo.html#data-sources>. pip install hypothesis[zoneinfo] installs it, if and only if needed.
On Windows, you may need to access IANA timezone data via the tzdata <https://pypi.org/project/tzdata/> package. For non-IANA timezones, such as Windows-native names or GNU TZ strings, we recommend using sampled_from() with the dateutil <https://pypi.org/project/python-dateutil/> package, e.g. dateutil:dateutil.tz.tzwin.list().
- hypothesis.strategies.timedeltas(min_value=datetime.timedelta.min, max_value=datetime.timedelta.max)
A strategy for timedeltas between min_value and max_value.
Examples from this strategy shrink towards zero.
Recursive
- hypothesis.strategies.recursive(base, extend, *, min_leaves=None, max_leaves=100)
base: A strategy to start from.
extend: A function which takes a strategy and returns a new strategy.
min_leaves: The minimum number of elements to be drawn from base on a given run.
max_leaves: The maximum number of elements to be drawn from base on a given run.
This returns a strategy S such that S = extend(base | S). That is, values may be drawn from base, or from any strategy reachable by mixing applications of | and extend.
An example may clarify: recursive(booleans(), lists) would return a strategy that may return arbitrarily nested and mixed lists of booleans. So e.g. False, [True], [False, []], and [[[[True]]]] are all valid values to be drawn from that strategy.
Examples from this strategy shrink by trying to reduce the amount of recursion and by shrinking according to the shrinking behaviour of base and the result of extend.
- hypothesis.strategies.deferred(definition)
A deferred strategy allows you to write a strategy that references other strategies that have not yet been defined. This allows for the easy definition of recursive and mutually recursive strategies.
The definition argument should be a zero-argument function that returns a strategy. It will be evaluated the first time the strategy is used to produce an example.
Example usage:
>>> import hypothesis.strategies as st >>> x = st.deferred(lambda: st.booleans() | st.tuples(x, x)) >>> x.example() (((False, (True, True)), (False, True)), (True, True)) >>> x.example() True
Mutual recursion also works fine:
>>> a = st.deferred(lambda: st.booleans() | b) >>> b = st.deferred(lambda: st.tuples(a, a)) >>> a.example() True >>> b.example() (False, (False, ((False, True), False)))
Examples from this strategy shrink as they normally would from the strategy returned by the definition.
Random
- hypothesis.strategies.randoms(*, note_method_calls=False, use_true_random=False)
Generates instances of random.Random. The generated Random instances are of a special HypothesisRandom subclass.
- If note_method_calls is set to True, Hypothesis will print the randomly drawn values in any falsifying test case. This can be helpful for debugging the behaviour of randomized algorithms.
- If use_true_random is set to True then values will be drawn from their usual distribution, otherwise they will actually be Hypothesis generated values (and will be shrunk accordingly for any failing test case). Setting use_true_random=False will tend to expose bugs that would occur with very low probability when it is set to True, and this flag should only be set to True when your code relies on the distribution of values for correctness.
For managing global state, see the random_module() strategy and register_random() function.
- hypothesis.strategies.random_module()
Hypothesis always seeds global PRNGs before running a test, and restores the previous state afterwards.
If having a fixed seed would unacceptably weaken your tests, and you cannot use a random.Random instance provided by randoms(), this strategy calls python:random.seed() with an arbitrary integer and passes you an opaque object whose repr displays the seed value for debugging. If numpy.random is available, that state is also managed, as is anything managed by hypothesis.register_random().
Examples from these strategy shrink to seeds closer to zero.
- hypothesis.register_random(r)
Register (a weakref to) the given Random-like instance for management by Hypothesis.
You can pass instances of structural subtypes of random.Random (i.e., objects with seed, getstate, and setstate methods) to register_random(r) to have their states seeded and restored in the same way as the global PRNGs from the random and numpy.random modules.
All global PRNGs, from e.g. simulation or scheduling frameworks, should be registered to prevent flaky tests. Hypothesis will ensure that the PRNG state is consistent for all test runs, always seeding them to zero and restoring the previous state after the test, or, reproducibly varied if you choose to use the random_module() strategy.
register_random only makes weakrefs <https://docs.python.org/3/library/weakref.html#module-weakref> to r, thus r will only be managed by Hypothesis as long as it has active references elsewhere at runtime. The pattern register_random(MyRandom()) will raise a ReferenceError to help protect users from this issue. This check does not occur for the PyPy interpreter. See the following example for an illustration of this issue
def my_BROKEN_hook(): r = MyRandomLike() # `r` will be garbage collected after the hook resolved # and Hypothesis will 'forget' that it was registered register_random(r) # Hypothesis will emit a warning rng = MyRandomLike() def my_WORKING_hook(): register_random(rng)
Combinators
- hypothesis.strategies.one_of(*args)
Return a strategy which generates values from any of the argument strategies.
This may be called with one iterable argument instead of multiple strategy arguments, in which case one_of(x) and one_of(*x) are equivalent.
Examples from this strategy will generally shrink to ones that come from strategies earlier in the list, then shrink according to behaviour of the strategy that produced them. In order to get good shrinking behaviour, try to put simpler strategies first. e.g. one_of(none(), text()) is better than one_of(text(), none()).
This is especially important when using recursive strategies. e.g. x = st.deferred(lambda: st.none() | st.tuples(x, x)) will shrink well, but x = st.deferred(lambda: st.tuples(x, x) | st.none()) will shrink very badly indeed.
- hypothesis.strategies.builds(target, /, *args, **kwargs)
Generates values by drawing from args and kwargs and passing them to the callable (provided as the first positional argument) in the appropriate argument position.
e.g. builds(target, integers(), flag=booleans()) would draw an integer i and a boolean b and call target(i, flag=b).
If the callable has type annotations, they will be used to infer a strategy for required arguments that were not passed to builds. You can also tell builds to infer a strategy for an optional argument by passing ... (python:Ellipsis) as a keyword argument to builds, instead of a strategy for that argument to the callable.
If the callable is a class defined with attrs <https://pypi.org/project/attrs/>, missing required arguments will be inferred from the attribute on a best-effort basis, e.g. by checking attrs standard validators. Dataclasses are handled natively by the inference from type hints.
Examples from this strategy shrink by shrinking the argument values to the callable.
- hypothesis.strategies.composite(f)
Defines a strategy that is built out of potentially arbitrarily many other strategies.
@composite provides a callable draw as the first parameter to the decorated function, which can be used to dynamically draw a value from any strategy. For example:
from hypothesis import strategies as st, given @st.composite def values(draw): n1 = draw(st.integers()) n2 = draw(st.integers(min_value=n1)) return (n1, n2) @given(values()) def f(value): (n1, n2) = value assert n1 <= n2@composite cannot mix test code and generation code. If you need that, use data().
If @composite is used to decorate a method or classmethod, the draw argument must come before self or cls. While we therefore recommend writing strategies as standalone functions and using register_type_strategy() to associate them with a class, methods are supported and the @composite decorator may be applied either before or after @classmethod or @staticmethod. See issue #2578 <https://github.com/HypothesisWorks/hypothesis/issues/2578> and pull request #2634 <https://github.com/HypothesisWorks/hypothesis/pull/2634> for more details.
Examples from this strategy shrink by shrinking the output of each draw call.
- hypothesis.strategies.data()
Provides an object data with a data.draw function which acts like the draw callable provided by @composite, in that it can be used to dynamically draw values from strategies. data() is more powerful than @composite, because it allows you to mix generation and test code.
Here's an example of dynamically generating values using data():
from hypothesis import strategies as st, given @given(st.data()) def test_values(data): n1 = data.draw(st.integers()) n2 = data.draw(st.integers(min_value=n1)) assert n1 + 1 <= n2If the test fails, each draw will be printed with the falsifying example. e.g. the above is wrong (it has a boundary condition error), so will print:
Falsifying example: test_values(data=data(...)) Draw 1: 0 Draw 2: 0
Optionally, you can provide a label to identify values generated by each call to data.draw(). These labels can be used to identify values in the output of a falsifying example.
For instance:
@given(st.data()) def test_draw_sequentially(data): x = data.draw(st.integers(), label="First number") y = data.draw(st.integers(min_value=x), label="Second number") assert x < ywill produce:
Falsifying example: test_draw_sequentially(data=data(...)) Draw 1 (First number): 0 Draw 2 (Second number): 0
Examples from this strategy shrink by shrinking the output of each draw call.
Typing
- hypothesis.strategies.from_type(thing)
Looks up the appropriate search strategy for the given type.
from_type() is used internally to fill in missing arguments to builds() and can be used interactively to explore what strategies are available or to debug type resolution.
You can use register_type_strategy() to handle your custom types, or to globally redefine certain strategies - for example excluding NaN from floats, or use timezone-aware instead of naive time and datetime strategies.
from_type() looks up a strategy in the following order:
- If thing is in the default lookup mapping or user-registered lookup, return the corresponding strategy. The default lookup covers all types with Hypothesis strategies, including extras where possible.
- If thing is from the python:typing module, return the corresponding strategy (special logic).
- If thing has one or more subtypes in the merged lookup, return the union of the strategies for those types that are not subtypes of other elements in the lookup.
- Finally, if thing has type annotations for all required arguments, and is not an abstract class, it is resolved via builds().
- Because abstract types cannot be instantiated, we treat abstract types as the union of their concrete subclasses. Note that this lookup works via inheritance but not via register, so you may still need to use register_type_strategy().
There is a valuable recipe for leveraging from_type() to generate "everything except" values from a specified type. I.e.
def everything_except(excluded_types): return ( from_type(type) .flatmap(from_type) .filter(lambda x: not isinstance(x, excluded_types)) )For example, everything_except(int) returns a strategy that can generate anything that from_type() can ever generate, except for instances of python:int, and excluding instances of types added via register_type_strategy().
This is useful when writing tests which check that invalid input is rejected in a certain way.
- hypothesis.strategies.register_type_strategy(custom_type, strategy)
Add an entry to the global type-to-strategy lookup.
This lookup is used in builds() and @given <#hypothesis.given>.
builds() will be used automatically for classes with type annotations on __init__ , so you only need to register a strategy if one or more arguments need to be more tightly defined than their type-based default, or if you want to supply a strategy for an argument with a default value.
strategy may be a search strategy, or a function that takes a type and returns a strategy (useful for generic types). The function may return NotImplemented to conditionally not provide a strategy for the type (the type will still be resolved by other methods, if possible, as if the function was not registered).
Note that you may not register a parametrised generic type (such as MyCollection[int]) directly, because the resolution logic does not handle this case correctly. Instead, you may register a function for MyCollection and inspect the type parameters within that function <https://stackoverflow.com/q/48572831>.
Hypothesis
- hypothesis.strategies.runner(*, default=not_set)
A strategy for getting "the current test runner", whatever that may be. The exact meaning depends on the entry point, but it will usually be the associated 'self' value for it.
If you are using this in a rule for stateful testing, this strategy will return the instance of the RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine> that the rule is running for.
If there is no current test runner and a default is provided, return that default. If no default is provided, raises InvalidArgument.
Examples from this strategy do not shrink (because there is only one).
- hypothesis.strategies.shared(base, *, key=None)
Returns a strategy that draws a single shared value per run, drawn from base. Any two shared instances with the same key will share the same value, otherwise the identity of this strategy will be used. That is:
>>> s = integers() # or any other strategy >>> x = shared(s) >>> y = shared(s)
In the above x and y may draw different (or potentially the same) values. In the following they will always draw the same:
>>> x = shared(s, key="hi") >>> y = shared(s, key="hi")
Examples from this strategy shrink as per their base strategy.
Misc
- hypothesis.strategies.functions(*, like=lambda : ..., returns=..., pure=False)
A strategy for functions, which can be used in callbacks.
The generated functions will mimic the interface of like, which must be a callable (including a class, method, or function). The return value for the function is drawn from the returns argument, which must be a strategy. If returns is not passed, we attempt to infer a strategy from the return-type annotation if present, falling back to none().
If pure=True, all arguments passed to the generated function must be hashable, and if passed identical arguments the original return value will be returned again - not regenerated, so beware mutable values.
If pure=False, generated functions do not validate their arguments, and may return a different value if called again with the same arguments.
Generated functions can only be called within the scope of the @given which created them.
- hypothesis.strategies.slices(size)
Generates slices that will select indices up to the supplied size
Generated slices will have start and stop indices that range from -size to size - 1 and will step in the appropriate direction. Slices should only produce an empty selection if the start and end are the same.
Examples from this strategy shrink toward 0 and smaller values
- hypothesis.strategies.uuids(*, version=None, allow_nil=False)
Returns a strategy that generates UUIDs.
If the optional version argument is given, value is passed through to UUID and only UUIDs of that version will be generated.
If allow_nil is True, generate the nil UUID much more often. Otherwise, all returned values from this will be unique, so e.g. if you do lists(uuids()) the resulting list will never contain duplicates.
Examples from this strategy don't have any meaningful shrink order.
- hypothesis.strategies.ip_addresses(*, v=None, network=None)
Generate IP addresses - v=4 for IPv4Addresses, v=6 for IPv6Addresses, or leave unspecified to allow both versions.
network may be an IPv4Network or IPv6Network, or a string representing a network such as "127.0.0.0/24" or "2001:db8::/32". As well as generating addresses within a particular routable network, this can be used to generate addresses from a reserved range listed in the IANA <https://www.iana.org/assignments/iana-ipv4-special-registry/> registries <https://www.iana.org/assignments/iana-ipv6-special-registry/>.
If you pass both v and network, they must be for the same version.
- hypothesis.strategies.sampled_from(elements)
Returns a strategy which generates any value present in elements.
Note that as with just(), values will not be copied and thus you should be careful of using mutable data.
sampled_from supports ordered collections, as well as Enum objects. Flag objects may also generate any combination of their members.
Examples from this strategy shrink by replacing them with values earlier in the list. So e.g. sampled_from([10, 1]) will shrink by trying to replace 1 values with 10, and sampled_from([1, 10]) will shrink by trying to replace 10 values with 1.
It is an error to sample from an empty sequence, because returning nothing() makes it too easy to silently drop parts of compound strategies. If you need that behaviour, use sampled_from(seq) if seq else nothing().
- hypothesis.strategies.permutations(values)
Return a strategy which returns permutations of the ordered collection values.
Examples from this strategy shrink by trying to become closer to the original order of values.
Related
- class hypothesis.strategies.DrawFn
This type only exists so that you can write type hints for functions decorated with @composite.
def draw(strategy: SearchStrategy[Ex], label: object = None) -> Ex: ... @composite def list_and_index(draw: DrawFn) -> tuple[int, str]: i = draw(integers()) # type of `i` inferred as 'int' s = draw(text()) # type of `s` inferred as 'str' return i, s- class hypothesis.strategies.DataObject
This type only exists so that you can write type hints for tests using the data() strategy. Do not use it directly!
- draw(strategy, label=None)
Like DrawFn.
- class hypothesis.strategies.SearchStrategy
A SearchStrategy tells Hypothesis how to generate that kind of input.
This class is only part of the public API for use in type annotations, so that you can write e.g. -> SearchStrategy[Foo] for your function which returns builds(Foo, ...). Do not inherit from or directly instantiate this class.
- example()
Provide an example of the sort of value that this strategy generates.
This method is designed for use in a REPL, and will raise an error if called from inside @given <#hypothesis.given> or a strategy definition. For serious use, see @composite or data().
- filter(condition)
Returns a new strategy that generates values from this strategy which satisfy the provided condition.
Note that if the condition is too hard to satisfy this might result in your tests failing with an Unsatisfiable exception. A basic version of the filtering logic would look something like:
@st.composite def filter_like(draw, strategy, condition): for _ in range(3): value = draw(strategy) if condition(value): return value assume(False)- map(pack)
Returns a new strategy which generates a value from this one, and then returns pack(value). For example, integers().map(str) could generate str(5) == "5".
- flatmap(expand)
Old syntax for a special case of @composite:
@st.composite def flatmap_like(draw, base_strategy, expand): value = draw(base_strategy) new_strategy = expand(value) return draw(new_strategy)We find that the greater readability of @composite usually outweighs the verbosity, with a few exceptions for simple cases or recipes like from_type(type).flatmap(from_type) ("pick a type, get a strategy for any instance of that type, and then generate one of those").
NumPy
Hypothesis offers a number of strategies for NumPy <https://numpy.org/> testing, available in the hypothesis[numpy] extra <>. It lives in the hypothesis.extra.numpy package.
The centerpiece is the arrays() strategy, which generates arrays with any dtype, shape, and contents you can specify or give a strategy for. To make this as useful as possible, strategies are provided to generate array shapes and generate all kinds of fixed-size or compound dtypes.
- hypothesis.extra.numpy.array_dtypes(subtype_strategy=scalar_dtypes(), *, min_size=1, max_size=5, allow_subarrays=False)
Return a strategy for generating array (compound) dtypes, with members drawn from the given subtype strategy.
- hypothesis.extra.numpy.array_shapes(*, min_dims=1, max_dims=None, min_side=1, max_side=None)
Return a strategy for array shapes (tuples of int >= 1).
- min_dims is the smallest length that the generated shape can possess.
- max_dims is the largest length that the generated shape can possess, defaulting to min_dims + 2.
- min_side is the smallest size that a dimension can possess.
- max_side is the largest size that a dimension can possess, defaulting to min_side + 5.
- hypothesis.extra.numpy.arrays(dtype, shape, *, elements=None, fill=None, unique=False)
Returns a strategy for generating numpy:numpy.ndarrays.
- dtype may be any valid input to dtype (this includes dtype objects), or a strategy that generates such values.
- shape may be an integer >= 0, a tuple of such integers, or a strategy that generates such values.
- elements is a strategy for generating values to put in the array. If it is None a suitable value will be inferred based on the dtype, which may give any legal value (including eg NaN for floats). If a mapping, it will be passed as **kwargs to from_dtype()
- fill is a strategy that may be used to generate a single background value for the array. If None, a suitable default will be inferred based on the other arguments. If set to nothing() then filling behaviour will be disabled entirely and every element will be generated independently.
- unique specifies if the elements of the array should all be distinct from one another. Note that in this case multiple NaN values may still be allowed. If fill is also set, the only valid values for it to return are NaN values (anything for which numpy:numpy.isnan returns True. So e.g. for complex numbers nan+1j is also a valid fill). Note that if unique is set to True the generated values must be hashable.
Arrays of specified dtype and shape are generated for example like this:
>>> import numpy as np >>> arrays(np.int8, (2, 3)).example() array([[-8, 6, 3], [-6, 4, 6]], dtype=int8) >>> arrays(np.float, 3, elements=st.floats(0, 1)).example() array([ 0.88974794, 0.77387938, 0.1977879 ])Array values are generated in two parts:
- Some subset of the coordinates of the array are populated with a value drawn from the elements strategy (or its inferred form).
- If any coordinates were not assigned in the previous step, a single value is drawn from the fill strategy and is assigned to all remaining places.
You can set fill=nothing() to disable this behaviour and draw a value for every element.
If fill=None, then it will attempt to infer the correct behaviour automatically. If unique is True, no filling will occur by default. Otherwise, if it looks safe to reuse the values of elements across multiple coordinates (this will be the case for any inferred strategy, and for most of the builtins, but is not the case for mutable values or strategies built with flatmap, map, composite, etc) then it will use the elements strategy as the fill, else it will default to having no fill.
Having a fill helps Hypothesis craft high quality examples, but its main importance is when the array generated is large: Hypothesis is primarily designed around testing small examples. If you have arrays with hundreds or more elements, having a fill value is essential if you want your tests to run in reasonable time.
- hypothesis.extra.numpy.basic_indices(shape, *, min_dims=0, max_dims=None, allow_newaxis=False, allow_ellipsis=True)
Return a strategy for basic indexes of arrays with the specified shape, which may include dimensions of size zero.
It generates tuples containing some mix of integers, python:slice objects, ... (an Ellipsis), and None. When a length-one tuple would be generated, this strategy may instead return the element which will index the first axis, e.g. 5 instead of (5,).
- shape is the shape of the array that will be indexed, as a tuple of positive integers. This must be at least two-dimensional for a tuple to be a valid index; for one-dimensional arrays use slices() instead.
- min_dims is the minimum dimensionality of the resulting array from use of the generated index. When min_dims == 0, scalars and zero-dimensional arrays are both allowed.
- max_dims is the maximum dimensionality of the resulting array, defaulting to len(shape) if not allow_newaxis else max(len(shape), min_dims) + 2.
- allow_newaxis specifies whether None is allowed in the index.
- allow_ellipsis specifies whether ... is allowed in the index.
- hypothesis.extra.numpy.boolean_dtypes()
Return a strategy for boolean dtypes.
- hypothesis.extra.numpy.broadcastable_shapes(shape, *, min_dims=0, max_dims=None, min_side=1, max_side=None)
Return a strategy for shapes that are broadcast-compatible with the provided shape.
Examples from this strategy shrink towards a shape with length min_dims. The size of an aligned dimension shrinks towards size 1. The size of an unaligned dimension shrink towards min_side.
- shape is a tuple of integers.
- min_dims is the smallest length that the generated shape can possess.
- max_dims is the largest length that the generated shape can possess, defaulting to max(len(shape), min_dims) + 2.
- min_side is the smallest size that an unaligned dimension can possess.
- max_side is the largest size that an unaligned dimension can possess, defaulting to 2 plus the size of the largest aligned dimension.
The following are some examples drawn from this strategy.
>>> [broadcastable_shapes(shape=(2, 3)).example() for i in range(5)] [(1, 3), (), (2, 3), (2, 1), (4, 1, 3), (3, )]
- hypothesis.extra.numpy.byte_string_dtypes(*, endianness='?', min_len=1, max_len=16)
Return a strategy for generating bytestring dtypes, of various lengths and byteorder.
While Hypothesis' string strategies can generate empty strings, string dtypes with length 0 indicate that size is still to be determined, so the minimum length for string dtypes is 1.
- hypothesis.extra.numpy.complex_number_dtypes(*, endianness='?', sizes=(64, 128))
Return a strategy for complex-number dtypes.
sizes is the total size in bits of a complex number, which consists of two floats. Complex halves (a 16-bit real part) are not supported by numpy and will not be generated by this strategy.
- hypothesis.extra.numpy.datetime64_dtypes(*, max_period='Y', min_period='ns', endianness='?')
Return a strategy for datetime64 dtypes, with various precisions from year to attosecond.
- hypothesis.extra.numpy.floating_dtypes(*, endianness='?', sizes=(16, 32, 64))
Return a strategy for floating-point dtypes.
sizes is the size in bits of floating-point number. Some machines support 96- or 128-bit floats, but these are not generated by default.
Larger floats (96 and 128 bit real parts) are not supported on all platforms and therefore disabled by default. To generate these dtypes, include these values in the sizes argument.
- hypothesis.extra.numpy.from_dtype(dtype, *, alphabet=None, min_size=0, max_size=None, min_value=None, max_value=None, allow_nan=None, allow_infinity=None, allow_subnormal=None, exclude_min=None, exclude_max=None, min_magnitude=0, max_magnitude=None)
Creates a strategy which can generate any value of the given dtype.
Compatible parameters are passed to the inferred strategy function while inapplicable ones are ignored. This allows you, for example, to customise the min and max values, control the length or contents of strings, or exclude non-finite numbers. This is particularly useful when kwargs are passed through from arrays() which allow a variety of numeric dtypes, as it seamlessly handles the width or representable bounds for you.
- hypothesis.extra.numpy.integer_array_indices(shape, *, result_shape=array_shapes(), dtype=dtype('int64'))
Return a search strategy for tuples of integer-arrays that, when used to index into an array of shape shape, given an array whose shape was drawn from result_shape.
Examples from this strategy shrink towards the tuple of index-arrays:
len(shape) * (np.zeros(drawn_result_shape, dtype), )
- shape a tuple of integers that indicates the shape of the array, whose indices are being generated.
- result_shape a strategy for generating tuples of integers, which describe the shape of the resulting index arrays. The default is array_shapes(). The shape drawn from this strategy determines the shape of the array that will be produced when the corresponding example from integer_array_indices is used as an index.
- dtype the integer data type of the generated index-arrays. Negative integer indices can be generated if a signed integer type is specified.
Recall that an array can be indexed using a tuple of integer-arrays to access its members in an arbitrary order, producing an array with an arbitrary shape. For example:
>>> from numpy import array >>> x = array([-0, -1, -2, -3, -4]) >>> ind = (array([[4, 0], [0, 1]]),) # a tuple containing a 2D integer-array >>> x[ind] # the resulting array is commensurate with the indexing array(s) array([[-4, 0], [0, -1]])Note that this strategy does not accommodate all variations of so-called 'advanced indexing', as prescribed by NumPy's nomenclature. Combinations of basic and advanced indexes are too complex to usefully define in a standard strategy; we leave application-specific strategies to the user. Advanced-boolean indexing can be defined as arrays(shape=..., dtype=bool), and is similarly left to the user.
- hypothesis.extra.numpy.integer_dtypes(*, endianness='?', sizes=(8, 16, 32, 64))
Return a strategy for signed integer dtypes.
endianness and sizes are treated as for unsigned_integer_dtypes().
- hypothesis.extra.numpy.mutually_broadcastable_shapes(*, num_shapes=not_set, signature=not_set, base_shape=(), min_dims=0, max_dims=None, min_side=1, max_side=None)
Return a strategy for a specified number of shapes N that are mutually-broadcastable with one another and with the provided base shape.
- num_shapes is the number of mutually broadcast-compatible shapes to generate.
- base_shape is the shape against which all generated shapes can broadcast. The default shape is empty, which corresponds to a scalar and thus does not constrain broadcasting at all.
- min_dims is the smallest length that the generated shape can possess.
- max_dims is the largest length that the generated shape can possess, defaulting to max(len(shape), min_dims) + 2.
- min_side is the smallest size that an unaligned dimension can possess.
- max_side is the largest size that an unaligned dimension can possess, defaulting to 2 plus the size of the largest aligned dimension.
The strategy will generate a python:typing.NamedTuple containing:
- input_shapes as a tuple of the N generated shapes.
- result_shape as the resulting shape produced by broadcasting the N shapes with the base shape.
The following are some examples drawn from this strategy.
>>> # Draw three shapes where each shape is broadcast-compatible with (2, 3) ... strat = mutually_broadcastable_shapes(num_shapes=3, base_shape=(2, 3)) >>> for _ in range(5): ... print(strat.example()) BroadcastableShapes(input_shapes=((4, 1, 3), (4, 2, 3), ()), result_shape=(4, 2, 3)) BroadcastableShapes(input_shapes=((3,), (1, 3), (2, 3)), result_shape=(2, 3)) BroadcastableShapes(input_shapes=((), (), ()), result_shape=()) BroadcastableShapes(input_shapes=((3,), (), (3,)), result_shape=(3,)) BroadcastableShapes(input_shapes=((1, 2, 3), (3,), ()), result_shape=(1, 2, 3)) **Use with Generalised Universal Function signatures** A :doc:`universal function <numpy:reference/ufuncs>` (or ufunc for short) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, type casting, and several other standard features. A :doc:`generalised ufunc <numpy:reference/c-api/generalized-ufuncs>` operates on sub-arrays rather than elements, based on the "signature" of the function. Compare e.g. :obj:`numpy.add() <numpy:numpy.add>` (ufunc) to :obj:`numpy.matmul() <numpy:numpy.matmul>` (gufunc). To generate shapes for a gufunc, you can pass the ``signature`` argument instead of ``num_shapes``. This must be a gufunc signature string; which you can write by hand or access as e.g. ``np.matmul.signature`` on generalised ufuncs. In this case, the ``side`` arguments are applied to the 'core dimensions' as well, ignoring any frozen dimensions. ``base_shape`` and the ``dims`` arguments are applied to the 'loop dimensions', and if necessary, the dimensionality of each shape is silently capped to respect the 32-dimension limit. The generated ``result_shape`` is the real result shape of applying the gufunc to arrays of the generated ``input_shapes``, even where this is different to broadcasting the loop dimensions. gufunc-compatible shapes shrink their loop dimensions as above, towards omitting optional core dimensions, and smaller-size core dimensions. .. code-block:: pycon >>> # np.matmul.signature == "(m?,n),(n,p?)->(m?,p?)" >>> for _ in range(3): ... mutually_broadcastable_shapes(signature=np.matmul.signature).example() BroadcastableShapes(input_shapes=((2,), (2,)), result_shape=()) BroadcastableShapes(input_shapes=((3, 4, 2), (1, 2)), result_shape=(3, 4)) BroadcastableShapes(input_shapes=((4, 2), (1, 2, 3)), result_shape=(4, 3))- hypothesis.extra.numpy.nested_dtypes(subtype_strategy=scalar_dtypes(), *, max_leaves=10, max_itemsize=None)
Return the most-general dtype strategy.
Elements drawn from this strategy may be simple (from the subtype_strategy), or several such values drawn from array_dtypes() with allow_subarrays=True. Subdtypes in an array dtype may be nested to any depth, subject to the max_leaves argument.
- hypothesis.extra.numpy.scalar_dtypes()
Return a strategy that can return any non-flexible scalar dtype.
- hypothesis.extra.numpy.timedelta64_dtypes(*, max_period='Y', min_period='ns', endianness='?')
Return a strategy for timedelta64 dtypes, with various precisions from year to attosecond.
- hypothesis.extra.numpy.unicode_string_dtypes(*, endianness='?', min_len=1, max_len=16)
Return a strategy for generating unicode string dtypes, of various lengths and byteorder.
While Hypothesis' string strategies can generate empty strings, string dtypes with length 0 indicate that size is still to be determined, so the minimum length for string dtypes is 1.
- hypothesis.extra.numpy.unsigned_integer_dtypes(*, endianness='?', sizes=(8, 16, 32, 64))
Return a strategy for unsigned integer dtypes.
endianness may be < for little-endian, > for big-endian, = for native byte order, or ? to allow either byte order. This argument only applies to dtypes of more than one byte.
sizes must be a collection of integer sizes in bits. The default (8, 16, 32, 64) covers the full range of sizes.
- hypothesis.extra.numpy.valid_tuple_axes(ndim, *, min_size=0, max_size=None)
Return a strategy for generating permissible tuple-values for the axis argument for a numpy sequential function (e.g. numpy:numpy.sum()), given an array of the specified dimensionality.
All tuples will have a length >= min_size and <= max_size. The default value for max_size is ndim.
Examples from this strategy shrink towards an empty tuple, which render most sequential functions as no-ops.
The following are some examples drawn from this strategy.
>>> [valid_tuple_axes(3).example() for i in range(4)] [(-3, 1), (0, 1, -1), (0, 2), (0, -2, 2)]
valid_tuple_axes can be joined with other strategies to generate any type of valid axis object, i.e. integers, tuples, and None:
any_axis_strategy = none() | integers(-ndim, ndim - 1) | valid_tuple_axes(ndim)
pandas
Hypothesis provides strategies for several of the core pandas data types: pandas.Index, pandas.Series and pandas.DataFrame.
The general approach taken by the pandas module is that there are multiple strategies for generating indexes, and all of the other strategies take the number of entries they contain from their index strategy (with sensible defaults). So e.g. a Series is specified by specifying its numpy.dtype (and/or a strategy for generating elements for it).
- class hypothesis.extra.pandas.column(name=None, elements=None, dtype=None, fill=None, unique=False)
Data object for describing a column in a DataFrame.
Arguments:
- name: the column name, or None to default to the column position. Must be hashable, but can otherwise be any value supported as a pandas column name.
- elements: the strategy for generating values in this column, or None to infer it from the dtype.
- dtype: the dtype of the column, or None to infer it from the element strategy. At least one of dtype or elements must be provided.
- fill: A default value for elements of the column. See arrays() for a full explanation.
- unique: If all values in this column should be distinct.
- hypothesis.extra.pandas.columns(names_or_number, *, dtype=None, elements=None, fill=None, unique=False)
A convenience function for producing a list of column objects of the same general shape.
The names_or_number argument is either a sequence of values, the elements of which will be used as the name for individual column objects, or a number, in which case that many unnamed columns will be created. All other arguments are passed through verbatim to create the columns.
- hypothesis.extra.pandas.data_frames(columns=None, *, rows=None, index=None)
Provides a strategy for producing a pandas.DataFrame.
Arguments:
- columns: An iterable of column objects describing the shape of the generated DataFrame.
rows: A strategy for generating a row object. Should generate either dicts mapping column names to values or a sequence mapping column position to the value in that position (note that unlike the pandas.DataFrame constructor, single values are not allowed here. Passing e.g. an integer is an error, even if there is only one column).
At least one of rows and columns must be provided. If both are provided then the generated rows will be validated against the columns and an error will be raised if they don't match.
Caveats on using rows:
- In general you should prefer using columns to rows, and only use rows if the columns interface is insufficiently flexible to describe what you need - you will get better performance and example quality that way.
- If you provide rows and not columns, then the shape and dtype of the resulting DataFrame may vary. e.g. if you have a mix of int and float in the values for one column in your row entries, the column will sometimes have an integral dtype and sometimes a float.
index: If not None, a strategy for generating indexes for the resulting DataFrame. This can generate either pandas.Index objects or any sequence of values (which will be passed to the Index constructor).
You will probably find it most convenient to use the indexes() or range_indexes() function to produce values for this argument.
Usage:
The expected usage pattern is that you use column and columns() to specify a fixed shape of the DataFrame you want as follows. For example the following gives a two column data frame:
>>> from hypothesis.extra.pandas import column, data_frames >>> data_frames([ ... column('A', dtype=int), column('B', dtype=float)]).example() A B 0 2021915903 1.793898e+232 1 1146643993 inf 2 -2096165693 1.000000e+07If you want the values in different columns to interact in some way you can use the rows argument. For example the following gives a two column DataFrame where the value in the first column is always at most the value in the second:
>>> from hypothesis.extra.pandas import column, data_frames >>> import hypothesis.strategies as st >>> data_frames( ... rows=st.tuples(st.floats(allow_nan=False), ... st.floats(allow_nan=False)).map(sorted) ... ).example() 0 1 0 -3.402823e+38 9.007199e+15 1 -1.562796e-298 5.000000e-01You can also combine the two:
>>> from hypothesis.extra.pandas import columns, data_frames >>> import hypothesis.strategies as st >>> data_frames( ... columns=columns(["lo", "hi"], dtype=float), ... rows=st.tuples(st.floats(allow_nan=False), ... st.floats(allow_nan=False)).map(sorted) ... ).example() lo hi 0 9.314723e-49 4.353037e+45 1 -9.999900e-01 1.000000e+07 2 -2.152861e+134 -1.069317e-73(Note that the column dtype must still be specified and will not be inferred from the rows. This restriction may be lifted in future).
Combining rows and columns has the following behaviour:
- The column names and dtypes will be used.
- If the column is required to be unique, this will be enforced.
- Any values missing from the generated rows will be provided using the column's fill.
- Any values in the row not present in the column specification (if dicts are passed, if there are keys with no corresponding column name, if sequences are passed if there are too many items) will result in InvalidArgument being raised.
- hypothesis.extra.pandas.indexes(*, elements=None, dtype=None, min_size=0, max_size=None, unique=True, name=none())
Provides a strategy for producing a pandas.Index.
Arguments:
- elements is a strategy which will be used to generate the individual values of the index. If None, it will be inferred from the dtype. Note: even if the elements strategy produces tuples, the generated value will not be a MultiIndex, but instead be a normal index whose elements are tuples.
- dtype is the dtype of the resulting index. If None, it will be inferred from the elements strategy. At least one of dtype or elements must be provided.
- min_size is the minimum number of elements in the index.
- max_size is the maximum number of elements in the index. If None then it will default to a suitable small size. If you want larger indexes you should pass a max_size explicitly.
- unique specifies whether all of the elements in the resulting index should be distinct.
- name is a strategy for strings or None, which will be passed to the pandas.Index constructor.
- hypothesis.extra.pandas.range_indexes(min_size=0, max_size=None, name=none())
Provides a strategy which generates an Index whose values are 0, 1, ..., n for some n.
Arguments:
- min_size is the smallest number of elements the index can have.
- max_size is the largest number of elements the index can have. If None it will default to some suitable value based on min_size.
- name is the name of the index. If st.none(), the index will have no name.
- hypothesis.extra.pandas.series(*, elements=None, dtype=None, index=None, fill=None, unique=False, name=none())
Provides a strategy for producing a pandas.Series.
Arguments:
- elements: a strategy that will be used to generate the individual values in the series. If None, we will attempt to infer a suitable default from the dtype.
- dtype: the dtype of the resulting series and may be any value that can be passed to numpy.dtype. If None, will use pandas's standard behaviour to infer it from the type of the elements values. Note that if the type of values that comes out of your elements strategy varies, then so will the resulting dtype of the series.
index: If not None, a strategy for generating indexes for the resulting Series. This can generate either pandas.Index objects or any sequence of values (which will be passed to the Index constructor).
You will probably find it most convenient to use the indexes() or range_indexes() function to produce values for this argument.
- name: is a strategy for strings or None, which will be passed to the pandas.Series constructor.
Usage:
>>> series(dtype=int).example() 0 -2001747478 1 1153062837
Supported versions
There is quite a lot of variation between pandas versions. We only commit to supporting the latest version of pandas, but older minor versions are supported on a "best effort" basis. Hypothesis is currently tested against and confirmed working with every Pandas minor version from 1.1 through to 2.2.
Releases that are not the latest patch release of their minor version are not tested or officially supported, but will probably also work unless you hit a pandas bug.
Array API
- Note:
Several array libraries have more library-specific strategies, including Xarray <https://pypi.org/project/xarray/> (via their upstream strategies) and NumPy <https://pypi.org/project/NumPy/> (via its Hypothesis extra). Of course, strategies in the Array API namespace can still be used to test Xarray or NumPy, just like any other array library.
Hypothesis offers strategies for Array API <https://data-apis.org/> adopting libraries in the hypothesis.extra.array_api package. See issue #3037 <https://github.com/HypothesisWorks/hypothesis/issues/3037> for more details. If you want to test with CuPy <https://pypi.org/project/cupy/>, Dask <https://pypi.org/project/dask/>, JAX <https://pypi.org/project/jax/>, MXNet <https://pypi.org/project/maxnet/>, PyTorch <https://pypi.org/project/torch/>, TensorFlow <https://pypi.org/project/tensorflow/>, or Xarray <https://pypi.org/project/xarray/> - or just NumPy <https://pypi.org/project/numpy/> - this is the extension for you!
- hypothesis.extra.array_api.make_strategies_namespace(xp, *, api_version=None)
Creates a strategies namespace for the given array module.
- xp is the Array API library to automatically pass to the namespaced methods.
- api_version is the version of the Array API which the returned strategies namespace should conform to. If None, the latest API version which xp supports will be inferred from xp.__array_api_version__. If a version string in the YYYY.MM format, the strategies namespace will conform to that version if supported.
A python:types.SimpleNamespace is returned which contains all the strategy methods in this module but without requiring the xp argument. Creating and using a strategies namespace for NumPy's Array API implementation would go like this:
>>> xp.__array_api_version__ # xp is your desired array library '2021.12' >>> xps = make_strategies_namespace(xp) >>> xps.api_version '2021.12' >>> x = xps.arrays(xp.int8, (2, 3)).example() >>> x Array([[-8, 6, 3], [-6, 4, 6]], dtype=int8) >>> x.__array_namespace__() is xp True
The resulting namespace contains all our familiar strategies like arrays() and from_dtype(), but based on the Array API standard semantics and returning objects from the xp module:
- xps.array_shapes(*, min_dims=1, max_dims=None, min_side=1, max_side=None)
Return a strategy for array shapes (tuples of int >= 1).
- min_dims is the smallest length that the generated shape can possess.
- max_dims is the largest length that the generated shape can possess, defaulting to min_dims + 2.
- min_side is the smallest size that a dimension can possess.
- max_side is the largest size that a dimension can possess, defaulting to min_side + 5.
- xps.arrays(dtype, shape, *, elements=None, fill=None, unique=False)
Returns a strategy for arrays <https://data-apis.org/array-api/latest/API_specification/array_object.html>.
- dtype may be a valid dtype <https://data-apis.org/array-api/latest/API_specification/data_types.html> object or name, or a strategy that generates such values.
- shape may be an integer >= 0, a tuple of such integers, or a strategy that generates such values.
- elements is a strategy for values to put in the array. If None then a suitable value will be inferred based on the dtype, which may give any legal value (including e.g. NaN for floats). If a mapping, it will be passed as **kwargs to from_dtype() when inferring based on the dtype.
- fill is a strategy that may be used to generate a single background value for the array. If None, a suitable default will be inferred based on the other arguments. If set to nothing() then filling behaviour will be disabled entirely and every element will be generated independently.
- unique specifies if the elements of the array should all be distinct from one another; if fill is also set, the only valid values for fill to return are NaN values.
Arrays of specified dtype and shape are generated for example like this:
>>> from numpy import array_api as xp >>> xps.arrays(xp, xp.int8, (2, 3)).example() Array([[-8, 6, 3], [-6, 4, 6]], dtype=int8)Specifying element boundaries by a python:dict of the kwargs to pass to from_dtype() will ensure dtype bounds will be respected.
>>> xps.arrays(xp, xp.int8, 3, elements={"min_value": 10}).example() Array([125, 13, 79], dtype=int8)>>> xps.arrays(xp, xp.float32, 3, elements=floats(0, 1, width=32)).example() Array([ 0.88974794, 0.77387938, 0.1977879 ], dtype=float32)
Array values are generated in two parts:
- A single value is drawn from the fill strategy and is used to create a filled array.
- Some subset of the coordinates of the array are populated with a value drawn from the elements strategy (or its inferred form).
You can set fill to nothing() if you want to disable this behaviour and draw a value for every element.
By default arrays will attempt to infer the correct fill behaviour: if unique is also True, no filling will occur. Otherwise, if it looks safe to reuse the values of elements across multiple coordinates (this will be the case for any inferred strategy, and for most of the builtins, but is not the case for mutable values or strategies built with flatmap, map, composite, etc.) then it will use the elements strategy as the fill, else it will default to having no fill.
Having a fill helps Hypothesis craft high quality examples, but its main importance is when the array generated is large: Hypothesis is primarily designed around testing small examples. If you have arrays with hundreds or more elements, having a fill value is essential if you want your tests to run in reasonable time.
- xps.boolean_dtypes()
Return a strategy for just the boolean dtype object.
- xps.broadcastable_shapes(shape, *, min_dims=0, max_dims=None, min_side=1, max_side=None)
Return a strategy for shapes that are broadcast-compatible with the provided shape.
Examples from this strategy shrink towards a shape with length min_dims. The size of an aligned dimension shrinks towards size 1. The size of an unaligned dimension shrink towards min_side.
- shape is a tuple of integers.
- min_dims is the smallest length that the generated shape can possess.
- max_dims is the largest length that the generated shape can possess, defaulting to max(len(shape), min_dims) + 2.
- min_side is the smallest size that an unaligned dimension can possess.
- max_side is the largest size that an unaligned dimension can possess, defaulting to 2 plus the size of the largest aligned dimension.
The following are some examples drawn from this strategy.
>>> [broadcastable_shapes(shape=(2, 3)).example() for i in range(5)] [(1, 3), (), (2, 3), (2, 1), (4, 1, 3), (3, )]
- xps.complex_dtypes(*, sizes=(64, 128))
Return a strategy for complex dtype objects.
sizes contains the complex sizes in bits, defaulting to (64, 128) which covers all valid sizes.
- xps.floating_dtypes(*, sizes=(32, 64))
Return a strategy for real-valued floating-point dtype objects.
sizes contains the floating-point sizes in bits, defaulting to (32, 64) which covers all valid sizes.
- xps.from_dtype(dtype, *, min_value=None, max_value=None, allow_nan=None, allow_infinity=None, allow_subnormal=None, exclude_min=None, exclude_max=None)
Return a strategy for any value of the given dtype.
Values generated are of the Python scalar which is promotable <https://data-apis.org/array-api/latest/API_specification/type_promotion.html> to dtype, where the values do not exceed its bounds.
- dtype may be a dtype object or the string name of a valid dtype <https://data-apis.org/array-api/latest/API_specification/data_types.html>.
Compatible **kwargs are passed to the inferred strategy function for integers and floats. This allows you to customise the min and max values, and exclude non-finite numbers. This is particularly useful when kwargs are passed through from arrays(), as it seamlessly handles the width or other representable bounds for you.
- xps.indices(shape, *, min_dims=0, max_dims=None, allow_newaxis=False, allow_ellipsis=True)
Return a strategy for valid indices <https://data-apis.org/array-api/latest/API_specification/indexing.html> of arrays with the specified shape, which may include dimensions of size zero.
It generates tuples containing some mix of integers, python:slice objects, ... (an Ellipsis), and None. When a length-one tuple would be generated, this strategy may instead return the element which will index the first axis, e.g. 5 instead of (5,).
- shape is the shape of the array that will be indexed, as a tuple of integers >= 0. This must be at least two-dimensional for a tuple to be a valid index; for one-dimensional arrays use slices() instead.
- min_dims is the minimum dimensionality of the resulting array from use of the generated index.
- max_dims is the maximum dimensionality of the resulting array, defaulting to len(shape) if not allow_newaxis else max(len(shape), min_dims) + 2.
- allow_ellipsis specifies whether None is allowed in the index.
- allow_ellipsis specifies whether ... is allowed in the index.
- xps.integer_dtypes(*, sizes=(8, 16, 32, 64))
Return a strategy for signed integer dtype objects.
sizes contains the signed integer sizes in bits, defaulting to (8, 16, 32, 64) which covers all valid sizes.
- xps.mutually_broadcastable_shapes(num_shapes, *, base_shape=(), min_dims=0, max_dims=None, min_side=1, max_side=None)
Return a strategy for a specified number of shapes N that are mutually-broadcastable with one another and with the provided base shape.
- num_shapes is the number of mutually broadcast-compatible shapes to generate.
- base_shape is the shape against which all generated shapes can broadcast. The default shape is empty, which corresponds to a scalar and thus does not constrain broadcasting at all.
- min_dims is the smallest length that the generated shape can possess.
- max_dims is the largest length that the generated shape can possess, defaulting to max(len(shape), min_dims) + 2.
- min_side is the smallest size that an unaligned dimension can possess.
- max_side is the largest size that an unaligned dimension can possess, defaulting to 2 plus the size of the largest aligned dimension.
The strategy will generate a python:typing.NamedTuple containing:
- input_shapes as a tuple of the N generated shapes.
- result_shape as the resulting shape produced by broadcasting the N shapes with the base shape.
The following are some examples drawn from this strategy.
>>> # Draw three shapes where each shape is broadcast-compatible with (2, 3) ... strat = mutually_broadcastable_shapes(num_shapes=3, base_shape=(2, 3)) >>> for _ in range(5): ... print(strat.example()) BroadcastableShapes(input_shapes=((4, 1, 3), (4, 2, 3), ()), result_shape=(4, 2, 3)) BroadcastableShapes(input_shapes=((3,), (1, 3), (2, 3)), result_shape=(2, 3)) BroadcastableShapes(input_shapes=((), (), ()), result_shape=()) BroadcastableShapes(input_shapes=((3,), (), (3,)), result_shape=(3,)) BroadcastableShapes(input_shapes=((1, 2, 3), (3,), ()), result_shape=(1, 2, 3))
- xps.numeric_dtypes()
Return a strategy for all numeric dtype objects.
- xps.real_dtypes()
Return a strategy for all real-valued dtype objects.
- xps.scalar_dtypes()
Return a strategy for all valid dtype <https://data-apis.org/array-api/latest/API_specification/data_types.html> objects.
- xps.unsigned_integer_dtypes(*, sizes=(8, 16, 32, 64))
Return a strategy for unsigned integer dtype objects.
sizes contains the unsigned integer sizes in bits, defaulting to (8, 16, 32, 64) which covers all valid sizes.
- xps.valid_tuple_axes(ndim, *, min_size=0, max_size=None)
Return a strategy for permissible tuple-values for the axis argument in Array API sequential methods e.g. sum, given the specified dimensionality.
All tuples will have a length >= min_size and <= max_size. The default value for max_size is ndim.
Examples from this strategy shrink towards an empty tuple, which render most sequential functions as no-ops.
The following are some examples drawn from this strategy.
>>> [valid_tuple_axes(3).example() for i in range(4)] [(-3, 1), (0, 1, -1), (0, 2), (0, -2, 2)]
valid_tuple_axes can be joined with other strategies to generate any type of valid axis object, i.e. integers, tuples, and None:
any_axis_strategy = none() | integers(-ndim, ndim - 1) | valid_tuple_axes(ndim)
Django
See also:
See the Django API reference <#hypothesis-django> for documentation on testing Django with Hypothesis.
- hypothesis.extra.django.from_model(model, /, **field_strategies)
Return a strategy for examples of model.
Warning:
Hypothesis creates saved models. This will run inside your testing transaction when using the test runner, but if you use the dev console this will leave debris in your database.
model must be an subclass of Model. Strategies for fields may be passed as keyword arguments, for example is_staff=st.just(False). In order to support models with fields named "model", this is a positional-only parameter.
Hypothesis can often infer a strategy based the field type and validators, and will attempt to do so for any required fields. No strategy will be inferred for an AutoField, nullable field, foreign key, or field for which a keyword argument is passed to from_model(). For example, a Shop type with a foreign key to Company could be generated with:
shop_strategy = from_model(Shop, company=from_model(Company))
Like for builds(), you can pass ... (python:Ellipsis) as a keyword argument to infer a strategy for a field which has a default value instead of using the default.
- hypothesis.extra.django.from_form(form, form_kwargs=None, **field_strategies)
Return a strategy for examples of form.
form must be an subclass of Form. Strategies for fields may be passed as keyword arguments, for example is_staff=st.just(False).
Hypothesis can often infer a strategy based the field type and validators, and will attempt to do so for any required fields. No strategy will be inferred for a disabled field or field for which a keyword argument is passed to from_form().
This function uses the fields of an unbound form instance to determine field strategies, any keyword arguments needed to instantiate the unbound form instance can be passed into from_form() as a dict with the keyword form_kwargs. E.g.:
shop_strategy = from_form(Shop, form_kwargs={"company_id": 5})Like for builds(), you can pass ... (python:Ellipsis) as a keyword argument to infer a strategy for a field which has a default value instead of using the default.
- hypothesis.extra.django.from_field(field)
Return a strategy for values that fit the given field.
This function is used by from_form() and from_model() for any fields that require a value, or for which you passed ... (python:Ellipsis) to infer a strategy from an annotation.
It's pretty similar to the core from_type() function, with a subtle but important difference: from_field takes a Field instance, rather than a Field subtype, so that it has access to instance attributes such as string length and validators.
- hypothesis.extra.django.register_field_strategy(field_type, strategy)
Add an entry to the global field-to-strategy lookup used by from_field().
field_type must be a subtype of django.db.models.Field or django.forms.Field, which must not already be registered. strategy must be a SearchStrategy.
hypothesis[lark]
- Note:
Strategies in this module require the hypothesis[lark] extra <>, via pip install hypothesis[lark].
This extra can be used to generate strings matching any context-free grammar, using the Lark parser library <https://github.com/lark-parser/lark>.
It currently only supports Lark's native EBNF syntax, but we plan to extend this to support other common syntaxes such as ANTLR and RFC 5234 <https://datatracker.ietf.org/doc/html/rfc5234.html> ABNF. Lark already supports loading grammars <https://lark-parser.readthedocs.io/en/stable/tools.html#importing-grammars-from-nearley-js> from nearley.js <https://nearley.js.org/>, so you may not have to write your own at all.
- hypothesis.extra.lark.from_lark(grammar, *, start=None, explicit=None, alphabet=characters(codec='utf-8'))
A strategy for strings accepted by the given context-free grammar.
grammar must be a Lark object, which wraps an EBNF specification. The Lark EBNF grammar reference can be found here <https://lark-parser.readthedocs.io/en/latest/grammar.html>.
from_lark will automatically generate strings matching the nonterminal start symbol in the grammar, which was supplied as an argument to the Lark class. To generate strings matching a different symbol, including terminals, you can override this by passing the start argument to from_lark. Note that Lark may remove unreachable productions when the grammar is compiled, so you should probably pass the same value for start to both.
Currently from_lark does not support grammars that need custom lexing. Any lexers will be ignored, and any undefined terminals from the use of %declare will result in generation errors. To define strategies for such terminals, pass a dictionary mapping their name to a corresponding strategy as the explicit argument.
The hypothesmith <https://pypi.org/project/hypothesmith/> project includes a strategy for Python source, based on a grammar and careful post-processing.
Example grammars, which may provide a useful starting point for your tests, can be found in the Lark repository <https://github.com/lark-parser/lark/tree/master/examples> and in this third-party collection <https://github.com/ligurio/lark-grammars>.
hypothesis[pytz]
- Note:
Strategies in this module require the hypothesis[pytz] extra <>, via pip install hypothesis[pytz].
This module provides pytz <https://pypi.org/project/pytz/> timezones.
If you are unable to use the stdlib zoneinfo module, e.g. via the hypothesis.strategies.timezones() strategy, you can use this strategy with hypothesis.strategies.datetimes() and hypothesis.strategies.times() to produce timezone-aware values.
- Warning:
Since zoneinfo was added in Python 3.9, this extra is deprecated. We intend to remove it after libraries such as Pandas and Django complete their own migrations.
- hypothesis.extra.pytz.timezones()
Any timezone in the Olsen database, as a pytz tzinfo object.
This strategy minimises to UTC, or the smallest possible fixed offset, and is designed for use with hypothesis.strategies.datetimes().
Tip:
Prefer the hypothesis.strategies.timezones() strategy, which uses the stdlib zoneinfo module and avoids the many footguns in pytz <https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html>.
hypothesis[dateutil]
- Note:
Strategies in this module require the hypothesis[dateutil] extra <>, via pip install hypothesis[dateutil].
This module provides dateutil <https://pypi.org/project/python-dateutil/> timezones.
You can use this strategy to make datetimes() and times() produce timezone-aware values.
- Tip:
Consider using the stdlib zoneinfo module, via st.timezones().
- hypothesis.extra.dateutil.timezones()
Any timezone from dateutil <https://pypi.org/project/python-dateutil/>.
This strategy minimises to UTC, or the timezone with the smallest offset from UTC as of 2000-01-01, and is designed for use with datetimes().
Note that the timezones generated by the strategy may vary depending on the configuration of your machine. See the dateutil documentation for more information.
Integrations Reference
Reference for Hypothesis features with a defined interface, but no code API.
Ghostwriter
Writing tests with Hypothesis frees you from the tedium of deciding on and writing out specific inputs to test. Now, the hypothesis.extra.ghostwriter module can write your test functions for you too!
The idea is to provide an easy way to start property-based testing, and a seamless transition to more complex test code - because ghostwritten tests are source code that you could have written for yourself.
So just pick a function you'd like tested, and feed it to one of the functions below. They follow imports, use but do not require type annotations, and generally do their best to write you a useful test. You can also use our command-line interface:
$ hypothesis write --help
Usage: hypothesis write [OPTIONS] FUNC...
`hypothesis write` writes property-based tests for you!
Type annotations are helpful but not required for our advanced
introspection and templating logic. Try running the examples below to see
how it works:
hypothesis write gzip
hypothesis write numpy.matmul
hypothesis write pandas.from_dummies
hypothesis write re.compile --except re.error
hypothesis write --equivalent ast.literal_eval eval
hypothesis write --roundtrip json.dumps json.loads
hypothesis write --style=unittest --idempotent sorted
hypothesis write --binary-op operator.add
Options:
--roundtrip start by testing write/read or encode/decode!
--equivalent very useful when optimising or refactoring code
--errors-equivalent --equivalent, but also allows consistent errors
--idempotent check that f(x) == f(f(x))
--binary-op associativity, commutativity, identity element
--style [pytest|unittest] pytest-style function, or unittest-style method?
-e, --except OBJ_NAME dotted name of exception(s) to ignore
--annotate / --no-annotate force ghostwritten tests to be type-annotated
(or not). By default, match the code to test.
-h, --help Show this message and exit.- Tip:
Using a light theme? Hypothesis respects NO_COLOR <https://no-color.org/> and DJANGO_COLORS=light.
- Note:
The ghostwriter requires black <https://pypi.org/project/black/>, but the generated code only requires Hypothesis itself.
- Note:
Legal questions? While the ghostwriter fragments and logic is under the MPL-2.0 license like the rest of Hypothesis, the output from the ghostwriter is made available under the Creative Commons Zero (CC0) <https://creativecommons.org/public-domain/cc0/> public domain dedication, so you can use it without any restrictions.
- hypothesis.extra.ghostwriter.magic(*modules_or_functions, except_=(), style='pytest', annotate=None)
Guess which ghostwriters to use, for a module or collection of functions.
As for all ghostwriters, the except_ argument should be an python:Exception or tuple of exceptions, and style may be either "pytest" to write test functions or "unittest" to write test methods and TestCase.
After finding the public functions attached to any modules, the magic ghostwriter looks for pairs of functions to pass to roundtrip(), then checks for binary_operation() and ufunc() functions, and any others are passed to fuzz().
For example, try hypothesis write gzip on the command line!
- hypothesis.extra.ghostwriter.fuzz(func, *, except_=(), style='pytest', annotate=None)
Write source code for a property-based test of func.
The resulting test checks that valid input only leads to expected exceptions. For example:
from re import compile, error from hypothesis.extra import ghostwriter ghostwriter.fuzz(compile, except_=error)
Gives:
# This test code was written by the `hypothesis.extra.ghostwriter` module # and is provided under the Creative Commons Zero public domain dedication. import re from hypothesis import given, reject, strategies as st # TODO: replace st.nothing() with an appropriate strategy @given(pattern=st.nothing(), flags=st.just(0)) def test_fuzz_compile(pattern, flags): try: re.compile(pattern=pattern, flags=flags) except re.error: reject()Note that it includes all the required imports. Because the pattern parameter doesn't have annotations or a default argument, you'll need to specify a strategy - for example text() <#hypothesis.strategies.text> or binary() <#hypothesis.strategies.binary>. After that, you have a test!
- hypothesis.extra.ghostwriter.idempotent(func, *, except_=(), style='pytest', annotate=None)
Write source code for a property-based test of func.
The resulting test checks that if you call func on it's own output, the result does not change. For example:
from typing import Sequence from hypothesis.extra import ghostwriter def timsort(seq: Sequence[int]) -> Sequence[int]: return sorted(seq) ghostwriter.idempotent(timsort)Gives:
# This test code was written by the `hypothesis.extra.ghostwriter` module # and is provided under the Creative Commons Zero public domain dedication. from hypothesis import given, strategies as st @given(seq=st.one_of(st.binary(), st.binary().map(bytearray), st.lists(st.integers()))) def test_idempotent_timsort(seq): result = timsort(seq=seq) repeat = timsort(seq=result) assert result == repeat, (result, repeat)- hypothesis.extra.ghostwriter.roundtrip(*funcs, except_=(), style='pytest', annotate=None)
Write source code for a property-based test of funcs.
The resulting test checks that if you call the first function, pass the result to the second (and so on), the final result is equal to the first input argument.
This is a very powerful property to test, especially when the config options are varied along with the object to round-trip. For example, try ghostwriting a test for python:json.dumps() - would you have thought of all that?
hypothesis write --roundtrip json.dumps json.loads
- hypothesis.extra.ghostwriter.equivalent(*funcs, allow_same_errors=False, except_=(), style='pytest', annotate=None)
Write source code for a property-based test of funcs.
The resulting test checks that calling each of the functions returns an equal value. This can be used as a classic 'oracle', such as testing a fast sorting algorithm against the python:sorted() builtin, or for differential testing where none of the compared functions are fully trusted but any difference indicates a bug (e.g. running a function on different numbers of threads, or simply multiple times).
The functions should have reasonably similar signatures, as only the common parameters will be passed the same arguments - any other parameters will be allowed to vary.
If allow_same_errors is True, then the test will pass if calling each of the functions returns an equal value, or if the first function raises an exception and each of the others raises an exception of the same type. This relaxed mode can be useful for code synthesis projects.
- hypothesis.extra.ghostwriter.binary_operation(func, *, associative=True, commutative=True, identity=Ellipsis, distributes_over=None, except_=(), style='pytest', annotate=None)
Write property tests for the binary operation func.
While binary operations <https://en.wikipedia.org/wiki/Binary_operation> are not particularly common, they have such nice properties to test that it seems a shame not to demonstrate them with a ghostwriter. For an operator f, test that:
- if associative <https://en.wikipedia.org/wiki/Associative_property>, f(a, f(b, c)) == f(f(a, b), c)
- if commutative <https://en.wikipedia.org/wiki/Commutative_property>, f(a, b) == f(b, a)
- if identity <https://en.wikipedia.org/wiki/Identity_element> is not None, f(a, identity) == a
- if distributes_over <https://en.wikipedia.org/wiki/Distributive_property> is +, f(a, b) + f(a, c) == f(a, b+c)
For example:
ghostwriter.binary_operation( operator.mul, identity=1, distributes_over=operator.add, style="unittest", )- hypothesis.extra.ghostwriter.ufunc(func, *, except_=(), style='pytest', annotate=None)
Write a property-based test for the array ufunc func.
The resulting test checks that your ufunc or gufunc has the expected broadcasting and dtype casting behaviour. You will probably want to add extra assertions, but as with the other ghostwriters this gives you a great place to start.
hypothesis write numpy.matmul
A note for test-generation researchers
Ghostwritten tests are intended as a starting point for human authorship, to demonstrate best practice, help novices past blank-page paralysis, and save time for experts. They may be ready-to-run, or include placeholders and # TODO: comments to fill in strategies for unknown types. In either case, improving tests for their own code gives users a well-scoped and immediately rewarding context in which to explore property-based testing.
By contrast, most test-generation tools aim to produce ready-to-run test suites... and implicitly assume that the current behavior is the desired behavior. However, the code might contain bugs, and we want our tests to fail if it does! Worse, tools require that the code to be tested is finished and executable, making it impossible to generate tests as part of the development process.
Fraser 2013 <https://doi.org/10.1145/2483760.2483774> found that evolving a high-coverage test suite (e.g. Randoop <https://homes.cs.washington.edu/~mernst/pubs/feedback-testgen-icse2007.pdf>, EvoSuite <https://www.evosuite.org/wp-content/papercite-data/pdf/esecfse11.pdf>, Pynguin <https://arxiv.org/abs/2007.14049>) "leads to clear improvements in commonly applied quality metrics such as code coverage [but] no measurable improvement in the number of bugs actually found by developers" and that "generating a set of test cases, even high coverage test cases, does not necessarily improve our ability to test software". Invariant detection (famously Daikon <https://plse.cs.washington.edu/daikon/pubs/>; in PBT see e.g. Alonso 2022 <https://doi.org/10.1145/3540250.3559080>, QuickSpec <http://www.cse.chalmers.se/~nicsma/papers/quickspec2.pdf>, Speculate <https://matela.com.br/speculate.pdf>) relies on code execution. Program slicing (e.g. FUDGE <https://research.google/pubs/pub48314/>, FuzzGen <https://www.usenix.org/conference/usenixsecurity20/presentation/ispoglou>, WINNIE <https://www.ndss-symposium.org/wp-content/uploads/2021-334-paper.pdf>) requires downstream consumers of the code to test.
Ghostwriter inspects the function name, argument names and types, and docstrings. It can be used on buggy or incomplete code, runs in a few seconds, and produces a single semantically-meaningful test per function or group of functions. Rather than detecting regressions, these tests check semantic properties such as encode/decode or save/load round-trips <https://zhd.dev/ghostwriter/?q=gzip.compress>, for commutative, associative, and distributive operations <https://zhd.dev/ghostwriter/?q=operator.mul>, equivalence between methods <https://zhd.dev/ghostwriter/?q=operator.add+numpy.add>, array shapes <https://zhd.dev/ghostwriter/?q=numpy.matmul>, and idempotence. Where no property is detected, we simply check for 'no error on valid input' and allow the user to supply their own invariants.
Evaluations such as the SBFT24 <https://arxiv.org/abs/2401.15189> competition <https://github.com/ThunderKey/python-tool-competition-2024> measure performance on a task which the Ghostwriter is not intended to perform. I'd love to see qualitative user studies, such as PBT in Practice <https://harrisongoldste.in/papers/icse24-pbt-in-practice.pdf> for test generation, which could check whether the Ghostwriter is onto something or tilting at windmills. If you're interested in similar questions, drop me an email <mailto:zac@zhd.dev?subject=Hypothesis%20Ghostwriter%20research>!
Observability
- Note:
The Tyche <https://github.com/tyche-pbt/tyche-extension> VSCode extension provides an in-editor UI for observability results generated by Hypothesis. If you want to view observability results, rather than programmatically consume or display them, we recommend using Tyche.
- Warning:
This feature is experimental, and could have breaking changes or even be removed without notice. Try it out, let us know what you think, but don't rely on it just yet!
Motivation
Understanding what your code is doing - for example, why your test failed - is often a frustrating exercise in adding some more instrumentation or logging (or print() calls) and running it again. The idea of observability <https://en.wikipedia.org/wiki/Observability_(software)> is to let you answer questions you didn't think of in advance. In slogan form,
Debugging should be a data analysis problem.
By default, Hypothesis only reports the minimal failing example... but sometimes you might want to know something about all the examples. Printing them to the terminal by increasing Verbosity <#hypothesis.Verbosity> might be nice, but isn't always enough. This feature gives you an analysis-ready dataframe with useful columns and one row per test case, with columns from arguments to code coverage to pass/fail status.
This is deliberately a much lighter-weight and task-specific system than e.g. OpenTelemetry <https://opentelemetry.io/>. It's also less detailed than time-travel debuggers such as rr <https://rr-project.org/> or pytrace <https://pytrace.com/>, because there's no good way to compare multiple traces from these tools and their Python support is relatively immature.
Configuration
If you set the HYPOTHESIS_EXPERIMENTAL_OBSERVABILITY environment variable, Hypothesis will log various observations to jsonlines files in the .hypothesis/observed/ directory. You can load and explore these with e.g. pd.read_json(".hypothesis/observed/*_testcases.jsonl", lines=True), or by using the sqlite-utils <https://pypi.org/project/sqlite-utils/> and datasette <https://pypi.org/project/datasette/> libraries:
sqlite-utils insert testcases.db testcases .hypothesis/observed/*_testcases.jsonl --nl --flatten datasette serve testcases.db
If you are experiencing a significant slow-down, you can try setting HYPOTHESIS_EXPERIMENTAL_OBSERVABILITY_NOCOVER instead; this will disable coverage information collection. This should not be necessary on Python 3.12 or later, where coverage collection is very fast.
Collecting more information
If you want to record more information about your test cases than the arguments and outcome - for example, was x a binary tree? what was the difference between the expected and the actual value? how many queries did it take to find a solution? - Hypothesis makes this easy.
event() <#hypothesis.event> accepts a string label, and optionally a string or int or float observation associated with it. All events are collected and summarized in Test statistics, as well as included on a per-test-case basis in our observations.
target() <#hypothesis.target> is a special case of numeric-valued events: as well as recording them in observations, Hypothesis will try to maximize the targeted value. Knowing that, you can use this to guide the search for failing inputs.
Data Format
We dump observations in json lines format <https://jsonlines.org/>, with each line describing either a test case or an information message. The tables below are derived from this machine-readable JSON schema, to provide both readable and verifiable specifications.
Note that we use python:json.dumps() and can therefore emit non-standard JSON which includes infinities and NaN. This is valid in JSON5 <https://json5.org/>, and supported by some JSON parsers <https://evanhahn.com/pythons-nonstandard-json-encoding/> including Gson in Java, JSON.parse() in Ruby, and of course in Python.
Information message
| Info, alert, and error messages correspond to a group of test cases or the overall run, and are intended for humans rather than machine analysis. | |
| properties | |
| A tag which labels this observation as general information to show the user. Hypothesis uses info messages to report statistics; alert or error messages can be provided by plugins. |
| enum | info, alert, error |
| The title of this message |
| type | string |
| The body of the message. Strings are presumed to be human-readable messages in markdown format; dictionaries may contain arbitrary information (as for test-case metadata). |
| type | string / object |
| The name or representation of the test function we're running. For Hypothesis, usually the Pytest nodeid. |
| type | string |
| unix timestamp at which we started running this test function, so that later analysis can group test cases by run. |
| type | number |
Test case
| Describes the inputs to and result of running some test function on a particular input. The test might have passed, failed, or been abandoned part way through (e.g. because we failed a .filter() <#hypothesis.strategies.SearchStrategy.filter> condition). | ||
| properties | ||
| A tag which labels this observation as data about a specific test case. | |
| const | test_case | |
| Whether the test passed, failed, or was aborted before completion (e.g. due to use of .filter() <#hypothesis.strategies.SearchStrategy.filter>). Note that if we gave_up partway, values such as arguments and features may be incomplete. | |
| enum | passed, failed, gave_up | |
| If non-empty, the reason for which the test failed or was abandoned. For Hypothesis, this is usually the exception type and location. | |
| type | string | |
| The string representation of the input. In Hypothesis, this includes the property name and arguments (like test_a(a=1)), any interactive draws from data() <#hypothesis.strategies.data>, and additionally some comments from Phase.explain <#hypothesis.Phase.explain> for failing examples. | |
| type | string | |
| A structured json-encoded representation of the input. Hypothesis provides a dictionary of argument names to json-ified values, including interactive draws from the data() <#hypothesis.strategies.data> strategy. If 'status' is 'gave_up', this may be absent or incomplete. In other libraries this can be any object. | |
| type | object | |
| How the input was generated, if known. In Hypothesis this might be an explicit example, generated during a particular phase with some backend, or by replaying the minimal failing example. | |
| type | string / null | |
| Runtime observations which might help explain what this test case did. Hypothesis includes target() <#hypothesis.target> scores, tags from event() <#hypothesis.event>, and so on. | |
| type | object | |
| Mapping of filename to list of covered line numbers, if coverage information is available, or None if not. Hypothesis deliberately omits stdlib and site-packages code. | |
| type | object / null | |
| additionalProperties | type | array |
| items | type | integer |
| minimum | 1 | |
| uniqueItems | True | |
| The time in seconds taken by non-overlapping parts of this test case. Hypothesis reports execute:test, overall:gc, and generate:{argname} for each argument. | |
| type | object | |
| additionalProperties | type | number |
| minimum | 0 | |
| Arbitrary metadata which might be of interest, but does not semantically fit in 'features'. For example, Hypothesis includes the traceback for failing tests here. | |
| type | object | |
| The name or representation of the test function we're running. | |
| type | string | |
| unix timestamp at which we started running this test function, so that later analysis can group test cases by run. | |
| type | number |
Hypothesis metadata
While the observability format is agnostic to the property-based testing library which generated it, Hypothesis includes specific values in the metadata key for test cases. You may rely on these being present if and only if the observation was generated by Hypothesis.
| properties | ||
| The traceback for failing tests, if and only if status == "failed". | |
| type | string / null | |
| The @reproduce_failure decorator string for failing tests, if and only if status == "failed". | |
| type | string / null | |
| The number of times each assume() <#hypothesis.assume> and @precondition <#hypothesis.stateful.precondition> predicate was satisfied (True) and not satisfied (False). | |
| type | object | |
| additionalProperties | type | object |
| properties | ||
| The number of times this predicate was satisfied (True). | |
| type | integer | |
| minimum | 0 | |
| The number of times this predicate was not satisfied (False). | |
| type | integer | |
| minimum | 0 | |
| additionalProperties | False | |
| Backend-specific observations from observe_test_case() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.observe_test_case> and observe_information_messages() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.observe_information_messages>. | |
| type | object | |
| The result of sys.argv. | |
| type | array | |
| items | type | string |
| The result of os.getpid(). | |
| type | integer | |
| The unix timestamp when Hypothesis was imported. | |
| type | number | |
| The Hypothesis Phase <#hypothesis.Phase> this test case was generated in. | |
| type | string | |
| The internal status of the ConjectureData for this test case. The values are as follows: Status.OVERRUN = 0, Status.INVALID = 1, Status.VALID = 2, and Status.INTERESTING = 3. | |
| type | number | |
| enum | 0, 1, 2, 3 | |
| The internal InterestingOrigin object for failing tests, if and only if status == "failed". The traceback string value is derived from this object. | |
| type | string / null |
Choices metadata
These additional metadata elements are included in metadata (as e.g. metadata["choice_nodes"] or metadata["choice_spans"]), if and only if OBSERVABILITY_CHOICES <#hypothesis.internal.observability.OBSERVABILITY_CHOICES> is set.
| properties | ||
| Warning: EXPERIMENTAL AND UNSTABLE. This attribute may change format or disappear without warning. Only present if OBSERVABILITY_CHOICES <#hypothesis.internal.observability.OBSERVABILITY_CHOICES> is True. Note: The choice sequence is a relatively low-level implementation detail of Hypothesis, and is exposed in observability for users building tools or research on top of Hypothesis. See PrimitiveProvider <#hypothesis.internal.conjecture.providers.PrimitiveProvider> for more details about the choice sequence. | |
| type | array / null | |
| items | type | object |
| properties | ||
| The type of choice made. Corresponds to a call to draw_integer() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.draw_integer>, draw_float() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.draw_float>, draw_string() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.draw_string>, draw_bytes() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.draw_bytes>, or draw_boolean() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.draw_boolean>. | |
| type | string | |
| enum | integer, float, string, bytes, boolean | |
| The value of the choice. Corresponds to the value returned by a PrimitiveProvider.draw_* method. NaN float values are returned as ["float", <float64_int_value>], to distinguish NaN floats with nonstandard bit patterns. Integers with abs(value) >= 2**63 are returned as ["integer", str(value)], for compatibility with tools with integer size limitations. Bytes are returned as ["bytes", base64.b64encode(value)]. | |
| The constraints for this choice. Corresponds to the constraints passed to a PrimitiveProvider.draw_* method. NaN float values, integers with abs(value) >= 2**63, and byte values for constraints are transformed as for the value attribute. | |
| type | object | |
| Whether this choice was forced. As an implementation detail, Hypothesis occasionally requires that some choices take on a specific value, for instance to end generation of collection elements early for performance. These values are called "forced", and have was_forced = True. | |
| type | boolean | |
| additionalProperties | False | |
| Warning: EXPERIMENTAL AND UNSTABLE. This attribute may change format or disappear without warning. Each span has the format [label, start, end, discarded], where:
Note: Spans are a relatively low-level implementation detail of Hypothesis, and are exposed in observability for users building tools or research on top of Hypothesis. See PrimitiveProvider <#hypothesis.internal.conjecture.providers.PrimitiveProvider> (and particularly span_start() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.span_start> and span_end() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.span_end>) for more details about spans. | |
| type | array | |
| items | type | array |
The Hypothesis pytest plugin
Hypothesis includes a tiny plugin to improve integration with pytest <https://pypi.org/project/pytest/>, which is activated by default (but does not affect other test runners). It aims to improve the integration between Hypothesis and Pytest by providing extra information and convenient access to config options.
- pytest --hypothesis-show-statistics can be used to display test and data generation statistics.
- pytest --hypothesis-profile=<profile name> can be used to load a settings profile (as in load_profile() <#hypothesis.settings.load_profile>).
- pytest --hypothesis-verbosity=<level name> can be used to override the current Verbosity <#hypothesis.Verbosity> setting.
- pytest --hypothesis-seed=<an int> can be used to reproduce a failure with a particular seed (as in @seed <#hypothesis.seed>).
- pytest --hypothesis-explain can be used to temporarily enable Phase.explain <#hypothesis.Phase.explain>.
Finally, all tests that are defined with Hypothesis automatically have @pytest.mark.hypothesis applied to them. See here for information on working with markers.
Note:
Pytest will load the plugin automatically if Hypothesis is installed. You don't need to do anything at all to use it.
If this causes problems, you can avoid loading the plugin with the -p no:hypothesispytest option.
Test statistics
- Note:
While test statistics are only available under pytest, you can use the observability interface to view similar information about your tests.
You can see a number of statistics about executed tests by passing the command line argument --hypothesis-show-statistics. This will include some general statistics about the test:
For example if you ran the following with --hypothesis-show-statistics:
from hypothesis import given, strategies as st
@given(st.integers())
def test_integers(i):
passYou would see:
- during generate phase (0.06 seconds):
- Typical runtimes: < 1ms, ~ 47% in data generation
- 100 passing examples, 0 failing examples, 0 invalid examples
- Stopped because settings.max_examples=100The final "Stopped because" line tells you why Hypothesis stopped generating new examples. This is typically because we hit max_examples <#hypothesis.settings.max_examples>, but occasionally because we exhausted the search space or because shrinking was taking a very long time. This can be useful for understanding the behaviour of your tests.
In some cases (such as filtered and recursive strategies) you will see events mentioned which describe some aspect of the data generation:
from hypothesis import given, strategies as st
@given(st.integers().filter(lambda x: x % 2 == 0))
def test_even_integers(i):
passYou would see something like:
test_even_integers:
- during generate phase (0.08 seconds):
- Typical runtimes: < 1ms, ~ 57% in data generation
- 100 passing examples, 0 failing examples, 12 invalid examples
- Events:
* 51.79%, Retried draw from integers().filter(lambda x: x % 2 == 0) to satisfy filter
* 10.71%, Aborted test because unable to satisfy integers().filter(lambda x: x % 2 == 0)
- Stopped because settings.max_examples=100hypothesis[cli]
- Note:
This feature requires the hypothesis[cli] extra <>, via pip install hypothesis[cli].
$ hypothesis --help Usage: hypothesis [OPTIONS] COMMAND [ARGS]... Options: --version Show the version and exit. -h, --help Show this message and exit. Commands: codemod `hypothesis codemod` refactors deprecated or inefficient code. fuzz [hypofuzz] runs tests with an adaptive coverage-guided fuzzer. write `hypothesis write` writes property-based tests for you!
This module requires the click <https://pypi.org/project/click/> package, and provides Hypothesis' command-line interface, for e.g. 'ghostwriting' tests via the terminal. It's also where HypoFuzz <https://hypofuzz.com/> adds the hypothesis fuzz command (learn more about that here <https://hypofuzz.com/docs/quickstart.html>).
hypothesis[codemods]
- Note:
This feature requires the hypothesis[codemods] extra <>, via pip install hypothesis[codemods].
This module provides codemods based on the LibCST <https://pypi.org/project/LibCST/> library, which can both detect and automatically fix issues with code that uses Hypothesis, including upgrading from deprecated features to our recommended style.
You can run the codemods via our CLI:
$ hypothesis codemod --help Usage: hypothesis codemod [OPTIONS] PATH... `hypothesis codemod` refactors deprecated or inefficient code. It adapts `python -m libcst.tool`, removing many features and config options which are rarely relevant for this purpose. If you need more control, we encourage you to use the libcst CLI directly; if not this one is easier. PATH is the file(s) or directories of files to format in place, or "-" to read from stdin and write to stdout. Options: -h, --help Show this message and exit.
Alternatively you can use python -m libcst.tool, which offers more control at the cost of additional configuration (adding 'hypothesis.extra' to the modules list in .libcst.codemod.yaml) and some issues on Windows <https://github.com/Instagram/LibCST/issues/435>.
- hypothesis.extra.codemods.refactor(code)
Update a source code string from deprecated to modern Hypothesis APIs.
This may not fix all the deprecation warnings in your code, but we're confident that it will be easier than doing it all by hand.
We recommend using the CLI, but if you want a Python function here it is.
hypothesis[dpcontracts]
- Note:
This feature requires the hypothesis[dpcontracts] extra <>, via pip install hypothesis[dpcontracts].
- Tip:
For new projects, we recommend using either deal <https://pypi.org/project/deal/> or icontract <https://pypi.org/project/icontract/> and icontract-hypothesis <https://pypi.org/project/icontract-hypothesis/> over dpcontracts <https://pypi.org/project/dpcontracts/>. They're generally more powerful tools for design-by-contract programming, and have substantially nicer Hypothesis integration too!
Hypothesis internals
- Warning:
This page documents internal Hypothesis interfaces. Some are fairly stable, while others are still experimental. In either case, they are not subject to our standard deprecation policy <#deprecation-policy>, and we might make breaking changes in minor or patch releases.
This page is intended for people building tools, libraries, or research on top of Hypothesis. If that includes you, please get in touch! We'd love to hear what you're doing, or explore more stable ways to support your use-case.
Alternative backends
See also:
See also the user-facing Alternative backends for Hypothesis <#alternative-backends> documentation.
- class hypothesis.internal.conjecture.providers.PrimitiveProvider(conjecturedata, /)
PrimitiveProvider is the implementation interface of a Hypothesis backend <#alternative-backends>.
A PrimitiveProvider is required to implement the following five draw_* methods:
- draw_integer()
- draw_boolean()
- draw_float()
- draw_string()
- draw_bytes()
Each strategy in Hypothesis generates values by drawing a series of choices from these five methods. By overriding them, a PrimitiveProvider can control the distribution of inputs generated by Hypothesis.
For example, hypothesis-crosshair <https://pypi.org/project/hypothesis-crosshair/> implements a PrimitiveProvider which uses an SMT solver to generate inputs that uncover new branches.
Once you implement a PrimitiveProvider, you can make it available for use through AVAILABLE_PROVIDERS.
- lifetime = 'test_function'
The lifetime of a PrimitiveProvider instance. Either test_function or test_case.
If test_function (the default), a single provider instance will be instantiated and used for the entirety of each test function (i.e., roughly one provider per @given <#hypothesis.given> annotation). This can be useful for tracking state over the entirety of a test function.
If test_case, a new provider instance will be instantiated and used for each input Hypothesis generates.
The conjecturedata argument to PrimitiveProvider.__init__ will be None for a lifetime of test_function, and an instance of ConjectureData for a lifetime of test_case.
Third-party providers likely want to set a lifetime of test_function.
- avoid_realization = False
Solver-based backends such as hypothesis-crosshair use symbolic values which record operations performed on them in order to discover new paths. If avoid_realization is set to True, hypothesis will avoid interacting with symbolic choices returned by the provider in any way that would force the solver to narrow the range of possible values for that symbolic.
Setting this to True disables some hypothesis features and optimizations. Only set this to True if it is necessary for your backend.
- add_observability_callback = False
will never be called by Hypothesis.
The opt-in behavior of observability is because enabling observability might increase runtime or memory usage.
- abstractmethod draw_boolean(p=0.5)
Draw a boolean choice.
- Parameters
p (float) --
The probability of returning True. Between 0 and 1 inclusive.
Except for 0 and 1, the value of p is a hint provided by Hypothesis, and may be ignored by the backend.
If 0, the provider must return False. If 1, the provider must return True.
- abstractmethod draw_integer(min_value=None, max_value=None, *, weights=None, shrink_towards=0)
Draw an integer choice.
- Parameters
- min_value (int | None) -- (Inclusive) lower bound on the integer value. If None, there is no lower bound.
- max_value (int | None) -- (Inclusive) upper bound on the integer value. If None, there is no upper bound.
- weights (dict[int, float] | None) -- Maps keys in the range [min_value, max_value] to the probability of returning that key.
- shrink_towards (int) -- The integer to shrink towards. This is not used during generation and can be ignored by backends.
- abstractmethod draw_float(*, min_value=-inf, max_value=inf, allow_nan=True, smallest_nonzero_magnitude)
Draw a float choice.
- Parameters
- min_value (float) -- (Inclusive) lower bound on the float value.
- max_value (float) -- (Inclusive) upper bound on the float value.
- allow_nan (bool) -- If False, it is invalid to return math.nan.
- smallest_nonzero_magnitude (float) -- The smallest allowed nonzero magnitude. draw_float should not return a float f if abs(f) < smallest_nonzero_magnitude.
- abstractmethod draw_string(intervals, *, min_size=0, max_size=10000000000)
Draw a string choice.
- Parameters
- intervals (IntervalSet <#hypothesis.internal.intervalsets.IntervalSet>) -- The set of codepoints to sample from.
- min_size (int) -- (Inclusive) lower bound on the string length.
- max_size (int) -- (Inclusive) upper bound on the string length.
- abstractmethod draw_bytes(min_size=0, max_size=10000000000)
Draw a bytes choice.
- Parameters
- min_size (int) -- (Inclusive) lower bound on the bytes length.
- max_size (int) -- (Inclusive) upper bound on the bytes length.
- per_test_case_context_manager()
Returns a context manager which will be entered each time Hypothesis starts generating and executing one test case, and exited when that test case finishes generating and executing, including if any exception is thrown.
In the lifecycle of a Hypothesis test, this is called before generating strategy values for each test case. This is just before any custom executor <#custom-function-execution> is called.
Even if not returning a custom context manager, PrimitiveProvider subclasses are welcome to override this method to know when Hypothesis starts and ends the execution of a single test case.
- realize(value, *, for_failure=False)
Called whenever hypothesis requires a concrete (non-symbolic) value from a potentially symbolic value. Hypothesis will not check that value is symbolic before calling realize, so you should handle the case where value is non-symbolic.
The returned value should be non-symbolic. If you cannot provide a value, raise BackendCannotProceed with a value of "discard_test_case".
If for_failure is True, the value is associated with a failing example. In this case, the backend should spend substantially more effort when attempting to realize the value, since it is important to avoid discarding failing examples. Backends may still raise BackendCannotProceed when for_failure is True, if realization is truly impossible or if realization takes significantly longer than expected (say, 5 minutes).
- replay_choices(choices)
Called when Hypothesis has discovered a choice sequence which the provider may wish to enqueue to replay under its own instrumentation when we next ask to generate a test case, rather than generating one from scratch.
This is used to e.g. warm-start hypothesis-crosshair <https://pypi.org/project/hypothesis-crosshair/> with a corpus of high-code-coverage inputs discovered by HypoFuzz <https://hypofuzz.com/>.
- observe_test_case()
Called at the end of the test case when observability <#observability> is enabled.
The return value should be a non-symbolic json-encodable dictionary, and will be included in observations as observation["metadata"]["backend"].
- observe_information_messages(*, lifetime)
Called at the end of each test case and again at end of the test function.
Return an iterable of {type: info/alert/error, title: str, content: str | dict} dictionaries to be delivered as individual information messages. Hypothesis adds the run_start timestamp and property name for you.
- on_observation(observation)
Called at the end of each test case which uses this provider, with the same observation["type"] == "test_case" observation that is passed to other callbacks added via add_observability_callback. This method is not called with observation["type"] in {"info", "alert", "error"} observations.
Important:
For on_observation() to be called by Hypothesis, add_observability_callback must be set to True.
on_observation() is explicitly opt-in, as enabling observability might increase runtime or memory usage.
Calls to this method are guaranteed to alternate with calls to per_test_case_context_manager(). For example:
# test function starts per_test_case_context_manager() on_observation() per_test_case_context_manager() on_observation() ... # test function ends
Note that on_observation() will not be called for test cases which did not use this provider during generation, for example during Phase.reuse <#hypothesis.Phase.reuse> or Phase.shrink <#hypothesis.Phase.shrink>, or because Hypothesis switched to the standard Hypothesis backend after this backend raised too many BackendCannotProceed exceptions.
- span_start(label, /)
Marks the beginning of a semantically meaningful span of choices.
Spans are a depth-first tree structure. A span is opened by a call to span_start(), and a call to span_end() closes the most recently opened span. So the following sequence of calls:
span_start(label=1) n1 = draw_integer() span_start(label=2) b1 = draw_boolean() n2 = draw_integer() span_end() f1 = draw_float() span_end()
produces the following two spans of choices:
1: [n1, b1, n2, f1] 2: [b1, n2]
Hypothesis uses spans to denote "semantically meaningful" sequences of choices. For instance, Hypothesis opens a span for the sequence of choices made while drawing from each strategy. Not every span corresponds to a strategy; the generation of e.g. each element in lists() <#hypothesis.strategies.lists> is also marked with a span, among others.
label is an opaque integer, which has no defined semantics. The only guarantee made by Hypothesis is that all spans with the same "meaning" will share the same label. So all spans from the same strategy will share the same label, as will e.g. the spans for lists() <#hypothesis.strategies.lists> elements.
Providers can track calls to span_start() and span_end() to learn something about the semantics of the test's choice sequence. For instance, a provider could track the depth of the span tree, or the number of unique labels, which says something about the complexity of the choices being generated. Or a provider could track the span tree across test cases in order to determine what strategies are being used in what contexts.
It is possible for Hypothesis to start and immediately stop a span, without calling a draw_* method in between. These spans contain zero choices.
Hypothesis will always balance the number of calls to span_start() and span_end(). A call to span_start() will always be followed by a call to span_end() before the end of the test case.
span_start() is called from ConjectureData.start_span() internally.
- span_end(discard, /)
Marks the end of a semantically meaningful span of choices.
discard is True when the draw was filtered out or otherwise marked as unlikely to contribute to the input data as seen by the user's test. Note however that side effects can make this determination unsound.
span_end() is called from ConjectureData.stop_span() internally.
- hypothesis.internal.conjecture.providers.AVAILABLE_PROVIDERS
Registered Hypothesis backends. This is a dictionary where keys are the name to be used in settings.backend <#hypothesis.settings.backend>. The value of a key can be either:
- A string corresponding to an importable absolute path of a PrimitiveProvider subclass
- A PrimitiveProvider subclass (the class itself, not an instance of the class)
Hypothesis will instantiate the corresponding PrimitiveProvider subclass when the backend is requested by a test's settings.backend <#hypothesis.settings.backend> value.
For example, the default Hypothesis backend is registered as:
from hypothesis.internal.conjecture.providers import AVAILABLE_PROVIDERS AVAILABLE_PROVIDERS["hypothesis"] = "hypothesis.internal.conjecture.providers.HypothesisProvider" # or AVAILABLE_PROVIDERS["hypothesis"] = HypothesisProvider
And can be used with:
from hypothesis import given, settings, strategies as st @given(st.integers()) @settings(backend="hypothesis") def f(n): passThough, as backend="hypothesis" is the default setting, the above would typically not have any effect.
For third-party backend authors, we strongly encourage ensuring that import hypothesis does not automatically import the expensive parts of your package, by:
- setting a string path here, instead of a provider class
- ensuring the registered hypothesis plugin path references a path which just sets AVAILABLE_PROVIDERS and does not import your package
- hypothesis.internal.conjecture.provider_conformance.run_conformance_test(Provider, *, context_manager_exceptions=(), settings=None, _realize_objects=st.from_type(object) | st.from_type(type).flatmap(st.from_type))
Test that the given Provider class conforms to the PrimitiveProvider interface.
For instance, this tests that Provider does not return out of bounds choices from any of the draw_* methods, or violate other invariants which Hypothesis depends on.
This function is intended to be called at test-time, not at runtime. It is provided by Hypothesis to make it easy for third-party backend authors to test their provider. Backend authors wishing to test their provider should include a test similar to the following in their test suite:
from hypothesis.internal.conjecture.provider_conformance import run_conformance_test def test_conformance(): run_conformance_test(MyProvider)If your provider can raise control flow exceptions inside one of the five draw_* methods that are handled by your provider's per_test_case_context_manager, pass a list of these exceptions types to context_manager_exceptions. Otherwise, run_conformance_test will treat those exceptions as fatal errors.
- class hypothesis.errors.BackendCannotProceed(scope='other', /)
Raised by alternative backends when a PrimitiveProvider cannot proceed. This is expected to occur inside one of the .draw_*() methods, or for symbolic execution perhaps in realize().
The optional scope argument can enable smarter integration:
- verified:
Do not request further test cases from this backend. We may generate more test cases with other backends; if one fails then Hypothesis will report unsound verification in the backend too.
- exhausted:
Do not request further test cases from this backend; finish testing with test cases generated with the default backend. Common if e.g. native code blocks symbolic reasoning very early.
- discard_test_case:
This particular test case could not be converted to concrete values; skip any further processing and continue with another test case from this backend.
- final class hypothesis.internal.intervalsets.IntervalSet(intervals=())
A compact and efficient representation of a set of (a, b) intervals. Can be treated like a set of integers, in that n in intervals will return True if n is contained in any of the (a, b) intervals, and False otherwise.
Observability
- hypothesis.internal.observability.add_observability_callback(f, /, *, all_threads=False)
Adds f as a callback for observability <#observability>. f should accept one argument, which is an observation. Whenever Hypothesis produces a new observation, it calls each callback with that observation.
If Hypothesis tests are being run from multiple threads, callbacks are tracked per-thread. In other words, add_observability_callback(f) only adds f as an observability callback for observations produced on that thread.
If all_threads=True is passed, f will instead be registered as a callback for all threads. This means it will be called for observations generated by all threads, not just the thread which registered f as a callback. In this case, f will be passed two arguments: the first is the observation, and the second is the integer thread id from python:threading.get_ident() where that observation was generated.
We recommend against registering f as a callback for both all_threads=True and the default all_threads=False, due to unclear semantics with remove_observability_callback.
- hypothesis.internal.observability.remove_observability_callback(f, /)
Removes f from the observability <#observability> callbacks.
If f is not in the list of observability callbacks, silently do nothing.
If running under multiple threads, f will only be removed from the callbacks for this thread.
- hypothesis.internal.observability.with_observability_callback(f, /, *, all_threads=False)
A simple context manager which calls add_observability_callback on f when it enters and remove_observability_callback on f when it exits.
- hypothesis.internal.observability.observability_enabled()
Returns whether or not Hypothesis considers observability <#observability> to be enabled. Observability is enabled if there is at least one observability callback present.
Callers might use this method to determine whether they should compute an expensive representation that is only used under observability, for instance by alternative backends <#alternative-backends>.
- hypothesis.internal.observability.TESTCASE_CALLBACKS = <hypothesis.internal.observability._TestcaseCallbacks object>
- Warning:
Deprecated in favor of add_observability_callback, remove_observability_callback, and observability_enabled.
TESTCASE_CALLBACKS remains a thin compatibility shim which forwards .append, .remove, and bool() to those three methods. It is not an attempt to be fully compatible with the previous TESTCASE_CALLBACKS = [], so iteration or other usages will not work anymore. Please update to using the new methods instead.
TESTCASE_CALLBACKS will eventually be removed.
- hypothesis.internal.observability.OBSERVABILITY_COLLECT_COVERAGE = True
If False, do not collect coverage information when observability is enabled.
This is exposed both for performance (as coverage collection can be slow on Python 3.11 and earlier) and size (if you do not use coverage information, you may not want to store it in-memory).
- hypothesis.internal.observability.OBSERVABILITY_CHOICES = False
If True, include the metadata.choice_nodes and metadata.spans keys in test case observations.
False by default. metadata.choice_nodes and metadata.spans can be a substantial amount of data, and so must be opted-in to, even when observability is enabled.
Warning:
EXPERIMENTAL AND UNSTABLE. We are actively working towards a better interface for this as of June 2025, and this attribute may disappear or be renamed without notice.
Engine constants
We pick reasonable values for these constants, but if you must, you can monkeypatch them. (Hypothesis is not responsible for any performance degradation that may result).
- hypothesis.internal.conjecture.engine.MAX_SHRINKS = 500
The maximum number of times the shrinker will reduce the complexity of a failing input before giving up. This avoids falling down a trap of exponential (or worse) complexity, where the shrinker appears to be making progress but will take a substantially long time to finish completely.
- hypothesis.internal.conjecture.engine.MAX_SHRINKING_SECONDS = 300
The maximum total time in seconds that the shrinker will try to shrink a failure for before giving up. This is across all shrinks for the same failure, so even if the shrinker successfully reduces the complexity of a single failure several times, it will stop when it hits MAX_SHRINKING_SECONDS of total time taken.
- hypothesis.internal.conjecture.engine.BUFFER_SIZE = 8192
The maximum amount of entropy a single test case can use before giving up while making random choices during input generation.
The "unit" of one BUFFER_SIZE does not have any defined semantics, and you should not rely on it, except that a linear increase BUFFER_SIZE will linearly increase the amount of entropy a test case can use during generation.
Stateful tests
- Note:
See also How not to Die Hard with Hypothesis <https://hypothesis.works/articles/how-not-to-die-hard-with-hypothesis/> and An Introduction to Rule-Based Stateful Testing <https://hypothesis.works/articles/rule-based-stateful-testing/>.
With @given <#hypothesis.given>, your tests are still something that you mostly write yourself, with Hypothesis providing some data. With Hypothesis's stateful testing, Hypothesis instead tries to generate not just data but entire tests. You specify a number of primitive actions that can be combined together, and then Hypothesis will try to find sequences of those actions that result in a failure.
You may not need stateful tests
The basic idea of stateful testing is to make Hypothesis choose actions as well as values for your test, and state machines are a great declarative way to do just that.
For simpler cases though, you might not need them at all - a standard test with @given <#hypothesis.given> might be enough, since you can use data() <#hypothesis.strategies.data> in branches or loops. In fact, that's how the state machine explorer works internally. For more complex workloads though, where a higher level API comes into it's own, keep reading!
Rule-based state machines
A state machine is very similar to a normal @given <#hypothesis.given> based test in that it takes values drawn from strategies and passes them to a user defined test function, which may use assertions to check the system's behavior. The key difference is that where @given <#hypothesis.given> based tests must be independent, rules can be chained together - a single test run may involve multiple rule invocations, which may interact in various ways.
Rules can take normal strategies as arguments, but normal strategies, with the exception of runner() <#hypothesis.strategies.runner> and data() <#hypothesis.strategies.data>, cannot take into account the current state of the machine. This is where bundles come in.
A rule can, in place of a normal strategy, take a Bundle <#hypothesis.stateful.Bundle>. A hypothesis.stateful.Bundle <#hypothesis.stateful.Bundle> is a named collection of generated values that can be reused by other operations in the test. They are populated with the results of rules, and may be used as arguments to rules, allowing data to flow from one rule to another, and rules to work on the results of previous computations or actions.
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 consumes() <#hypothesis.stateful.consumes> as in an_argument=consumes(a_bundle).
Note:
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 runner() <#hypothesis.strategies.runner> and .flatmap() <#hypothesis.strategies.SearchStrategy.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 data() <#hypothesis.strategies.data> to draw data from the instance (or anywhere else) based on logic in the rule.
The following rule based state machine example is a simplified version of a test for Hypothesis's example database implementation. An example database maps keys to sets of values, and in this test we compare one implementation of it to a simplified in memory model of its behaviour, which just stores the same values in a Python dict. The test then runs operations against both the real database and the in-memory representation of it and looks for discrepancies in their behaviour.
import shutil
import tempfile
from collections import defaultdict
import hypothesis.strategies as st
from hypothesis.database import DirectoryBasedExampleDatabase
from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule
class DatabaseComparison(RuleBasedStateMachine):
def __init__(self):
super().__init__()
self.tempd = tempfile.mkdtemp()
self.database = DirectoryBasedExampleDatabase(self.tempd)
self.model = defaultdict(set)
keys = Bundle("keys")
values = Bundle("values")
@rule(target=keys, k=st.binary())
def add_key(self, k):
return k
@rule(target=values, v=st.binary())
def add_value(self, v):
return v
@rule(k=keys, v=values)
def save(self, k, v):
self.model[k].add(v)
self.database.save(k, v)
@rule(k=keys, v=values)
def delete(self, k, v):
self.model[k].discard(v)
self.database.delete(k, v)
@rule(k=keys)
def values_agree(self, k):
assert set(self.database.fetch(k)) == self.model[k]
def teardown(self):
shutil.rmtree(self.tempd)
TestDBComparison = DatabaseComparison.TestCaseIn this we declare two bundles - one for keys, and one for values. We have two trivial rules which just populate them with data (k and v), and three non-trivial rules: save saves a value under a key and delete removes a value from a key, in both cases also updating the model of what should be in the database. values_agree then checks that the contents of the database agrees with the model for a particular key.
Note:
While this could have been simplified by not using bundles, generating keys and values directly in the save and delete rules, using bundles encourages Hypothesis to choose the same keys and values for multiple operations. The bundle operations establish a "universe" of keys and values that are used in the rules.
We can now integrate this into our test suite by getting a unittest TestCase from it:
TestTrees = DatabaseComparison.TestCase
# Or just run with pytest's unittest support
if __name__ == "__main__":
unittest.main()This test currently passes, but if we comment out the line where we call self.model[k].discard(v), we would see the following output when run under pytest:
AssertionError: assert set() == {b''}
------------ Hypothesis ------------
state = DatabaseComparison()
var1 = state.add_key(k=b'')
var2 = state.add_value(v=var1)
state.save(k=var1, v=var2)
state.delete(k=var1, v=var2)
state.values_agree(k=var1)
state.teardown()Note how it's printed out a very short program that will demonstrate the problem. The output from a rule based state machine should generally be pretty close to Python code - if you have custom repr implementations that don't return valid Python then it might not be, but most of the time you should just be able to copy and paste the code into a test to reproduce it.
You can control the detailed behaviour with a settings object on the TestCase (this is a normal hypothesis settings object using the defaults at the time the TestCase class was first referenced). For example if you wanted to run fewer examples with larger programs you could change the settings to:
DatabaseComparison.TestCase.settings = settings(
max_examples=50, stateful_step_count=100
)Which doubles the number of steps each program runs and halves the number of test cases that will be run.
Rules
As said earlier, rules are the most common feature used in RuleBasedStateMachine. They are defined by applying the rule() <#hypothesis.stateful.rule> decorator on a function. Note that RuleBasedStateMachine must have at least one rule defined and that a single function cannot be used to define multiple rules (this is to avoid having multiple rules doing the same things). Due to the stateful execution method, rules generally cannot take arguments from other sources such as fixtures or pytest.mark.parametrize - consider providing them via a strategy such as sampled_from() <#hypothesis.strategies.sampled_from> instead.
Initializes
Initializes are a special case of rules, which are guaranteed to be run exactly once before any normal rule is called. Note if multiple initialize rules are defined, they will all be called but in any order, and that order will vary from run to run.
Initializes are typically useful to populate bundles:
import hypothesis.strategies as st
from hypothesis.stateful import Bundle, RuleBasedStateMachine, initialize, rule
name_strategy = st.text(min_size=1).filter(lambda x: "/" not in x)
class NumberModifier(RuleBasedStateMachine):
folders = Bundle("folders")
files = Bundle("files")
@initialize(target=folders)
def init_folders(self):
return "/"
@rule(target=folders, parent=folders, name=name_strategy)
def create_folder(self, parent, name):
return f"{parent}/{name}"
@rule(target=files, parent=folders, name=name_strategy)
def create_file(self, parent, name):
return f"{parent}/{name}"Initializes can also allow you to initialize the system under test in a way that depends on values chosen from a strategy. You could do this by putting an instance variable in the state machine that indicates whether the system under test has been initialized or not, and then using preconditions (below) to ensure that exactly one of the rules that initialize it get run before any rules that depend on it being initialized.
Preconditions
While it's possible to use assume() <#hypothesis.assume> in RuleBasedStateMachine rules, if you use it in only a few rules you can quickly run into a situation where few or none of your rules pass their assumptions. Thus, Hypothesis provides a precondition() <#hypothesis.stateful.precondition> decorator to avoid this problem. The precondition() <#hypothesis.stateful.precondition> decorator is used on rule-decorated functions, and must be given a function that returns True or False based on the RuleBasedStateMachine instance.
from hypothesis.stateful import RuleBasedStateMachine, precondition, rule
class NumberModifier(RuleBasedStateMachine):
num = 0
@rule()
def add_one(self):
self.num += 1
@precondition(lambda self: self.num != 0)
@rule()
def divide_with_one(self):
self.num = 1 / self.numBy using precondition() <#hypothesis.stateful.precondition> here instead of assume() <#hypothesis.assume>, Hypothesis can filter the inapplicable rules before running them. This makes it much more likely that a useful sequence of steps will be generated.
Note that currently preconditions can't access bundles; if you need to use preconditions, you should store relevant data on the instance instead.
Invariants
Often there are invariants that you want to ensure are met after every step in a process. It would be possible to add these as rules that are run, but they would be run zero or multiple times between other rules. Hypothesis provides a decorator that marks a function to be run after every step.
from hypothesis.stateful import RuleBasedStateMachine, invariant, rule
class NumberModifier(RuleBasedStateMachine):
num = 0
@rule()
def add_two(self):
self.num += 2
if self.num > 50:
self.num += 1
@invariant()
def is_even(self):
assert self.num % 2 == 0
NumberTest = NumberModifier.TestCaseInvariants can also have precondition() <#hypothesis.stateful.precondition>s applied to them, in which case they will only be run if the precondition function returns true.
Note that currently invariants can't access bundles; if you need to use invariants, you should store relevant data on the instance instead.
More fine grained control
If you want to bypass the TestCase infrastructure you can invoke these manually. The stateful module exposes the function run_state_machine_as_test, which takes an arbitrary function returning a RuleBasedStateMachine and an optional settings parameter and does the same as the class based runTest provided.
First-party extensions
Hypothesis has minimal dependencies, to maximise compatibility and make installing Hypothesis as easy as possible.
Our integrations with specific packages are therefore provided by extra modules that need their individual dependencies installed in order to work. You can install these dependencies using the setuptools extra feature as e.g. pip install hypothesis[django]. This will check installation of compatible versions.
You can also just install hypothesis into a project using them, ignore the version constraints, and hope for the best.
In general "Which version is Hypothesis compatible with?" is a hard question to answer and even harder to regularly test. Hypothesis is always tested against the latest compatible version and each package will note the expected compatibility range. If you run into a bug with any of these please specify the dependency version.
The following lists the available extras in Hypothesis and their documentation.
- hypothesis[cli] <#hypothesis-cli>
- hypothesis[codemods] <#codemods>
- hypothesis[django] <#hypothesis-django>
- hypothesis[numpy] <#hypothesis-numpy>
- hypothesis[lark] <#hypothesis-lark>
- hypothesis[pytz] <#hypothesis-pytz>
- hypothesis[dateutil] <#hypothesis-dateutil>
- hypothesis[dpcontracts] <#hypothesis-dpcontracts>
Changelog
This is a record of all past Hypothesis releases and what went into them, in reverse chronological order. All previous releases are still available on PyPI <https://pypi.org/project/hypothesis/>.
Hypothesis 6.x
6.151.9 - 2026-02-16
Remove some old unused code.
6.151.8 - 2026-02-16
This patch fixes a crash when sys.modules contains unhashable values, such as SimpleNamespace objects (issue #4660 <https://github.com/HypothesisWorks/hypothesis/issues/4660>).
6.151.7 - 2026-02-16
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.151.6 - 2026-02-11
This patch fixes several duplicate word typos in comments and documentation.
6.151.5 - 2026-02-03
This patch teaches our pytest plugin to :ref:` find interesting constants <v6.131.1>` when pytest is collecting tests, to avoid arbitrarily attributing the latency to whichever test function happened to be executed first (issue #4627 <https://github.com/HypothesisWorks/hypothesis/issues/4627>).
6.151.4 - 2026-01-29
This patch adjusts how we compute the stopping threshold introduced in version 6.151.3 <changelog.html#v6-151-3>, while still maintaining 99% confidence that <1% of test cases pass.
6.151.3 - 2026-01-28
This patch makes Hypothesis more tolerant of slow-to-satisfy assume() calls. Previously, Hypothesis would give up after max_examples * 10 attempts; now it uses a statistical test to stop only when 99% confident that <1% of examples would pass (issue #4623 <https://github.com/HypothesisWorks/hypothesis/issues/4623>).
Thanks to @ajdavis for this improvement!
6.151.2 - 2026-01-26
Format our code with the latest version of black <https://pypi.org/project/black/>.
6.151.1 - 2026-01-26
Improve internal categorization of test cases when an alternative backend <#alternative-backends> raises BackendCannotProceed <#hypothesis.errors.BackendCannotProceed>.
6.151.0 - 2026-01-25
Add 2025.12 to the list of recognized Array API versions in hypothesis.extra.array_api.
6.150.3 - 2026-01-23
Hypothesis now generates powers of 2 more often when using integers() <#hypothesis.strategies.integers>.
6.150.2 - 2026-01-13
Update some internal type hints.
6.150.1 - 2026-01-12
This patch fixes a bug where recursive() <#hypothesis.strategies.recursive> would fail in cases where the extend= function does not reference it's argument - which was assumed by the recent min_leaves= feature, because the strategy can't actually recurse otherwise. (issue #4638 <https://github.com/HypothesisWorks/hypothesis/issues/4638>)
Now, the historical behavior is working-but-deprecated, or an error if you explicitly pass min_leaves=.
6.150.0 - 2026-01-06
This release adds a min_leaves argument to recursive() <#hypothesis.strategies.recursive>, which ensures that generated recursive structures have at least the specified number of leaf nodes (issue #4205 <https://github.com/HypothesisWorks/hypothesis/issues/4205>).
6.149.1 - 2026-01-05
Add type hints to an internal class.
6.149.0 - 2026-01-05
This release extends the explain-phase # or any other generated value comments to sub-arguments within builds() <#hypothesis.strategies.builds>, tuples() <#hypothesis.strategies.tuples>, and fixed_dictionaries() <#hypothesis.strategies.fixed_dictionaries>.
Previously, these comments only appeared on top-level test arguments. Now, when the explain phase determines that a sub-argument can vary freely without affecting the test failure, you'll see comments like:
Falsifying example: test_foo(
obj=MyClass(
x=0, # or any other generated value
y=True,
),
data=(
'', # or any other generated value
42,
),
)This makes it easier to understand which parts of complex inputs actually matter for reproducing a failure.
6.148.13 - 2026-01-05
Clean up an internal helper.
6.148.12 - 2026-01-04
This patch fixes from_type() <#hypothesis.strategies.from_type> to properly handle parameterized type aliases created with Python 3.12+'s PEP 695 <https://peps.python.org/pep-0695/> type statement. For example, st.from_type(A[int]) where type A[T] = list[T] now correctly resolves to lists(integers()) instead of raising a TypeError (issue #4628 <https://github.com/HypothesisWorks/hypothesis/issues/4628>).
6.148.11 - 2026-01-03
Hypothesis now prints a Verbosity.verbose <#hypothesis.Verbosity.verbose> log when we switch away from an alternative backend <#alternative-backends>.
6.148.10 - 2026-01-03
Fixes Ghostwriter <#ghostwriter> output for numpy <https://pypi.org/project/numpy/> >= 2.4.0. Also adds support from_type() <#hypothesis.strategies.from_type> for numpy <https://pypi.org/project/numpy/> 2.5.0 nightly (which has not yet been released).
6.148.9 - 2026-01-01
.example() <#hypothesis.strategies.SearchStrategy.example> no longer emits NonInteractiveExampleWarning <#hypothesis.errors.NonInteractiveExampleWarning> when running a python file directly. This means that e.g. python my_sandbox.py during exploratory work with .example() <#hypothesis.strategies.SearchStrategy.example> will no longer raise warnings.
6.148.8 - 2025-12-23
Add __dict__ and __proto__ to the list of constant strings Hypothesis sometimes generates.
6.148.7 - 2025-12-05
When multiple explicit @example <#hypothesis.example> decorators fail with the same error, Hypothesis now shows only the simplest failing example (by shortlex order) with a note about how many other examples also failed (issue #4520 <https://github.com/HypothesisWorks/hypothesis/issues/4520>).
To see all failing examples, use Verbosity.verbose <#hypothesis.Verbosity.verbose> or higher.
6.148.6 - 2025-12-04
Fix a bug where we persisted symbolics from solver-based alternative backends <#alternative-backends> in event() <#hypothesis.event>.
6.148.5 - 2025-12-01
This patch improves the error message for FlakyStrategyDefinition <#hypothesis.errors.FlakyStrategyDefinition> when the precondition for a rule is flaky (issue #4206 <https://github.com/HypothesisWorks/hypothesis/issues/4206>).
6.148.4 - 2025-12-01
This patch improves the type annotations for basic_indices() <#hypothesis.extra.numpy.basic_indices>. The return type now accurately reflects the allow_ellipsis and allow_newaxis parameters, excluding EllipsisType or None from the union when those index types are disabled (issue #4607 <https://github.com/HypothesisWorks/hypothesis/issues/4607>).
Additionally, assume() <#hypothesis.assume> now has overloaded type annotations: assume(True) returns Literal[True], while assume(False) and assume(None) return NoReturn.
6.148.3 - 2025-11-27
Clean up some internal code.
6.148.2 - 2025-11-18
Document fuzz_one_input() <#hypothesis.core.HypothesisHandle.fuzz_one_input>.
6.148.1 - 2025-11-16
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.148.0 - 2025-11-15
Calling register_profile() <#hypothesis.settings.register_profile> from within a test decorated with @settings <#hypothesis.settings> is now deprecated, to avoid confusion about which settings are used as the baseline for the new profile.
6.147.0 - 2025-11-06
This release drops support for nose <https://pypi.org/project/nose/>, which ceased development 9 years ago and does not support Python 3.10 or newer.
Hypothesis still supports nose2 <https://pypi.org/project/nose2/>. While we do not test nose2 in our CI, we will fix any bugs that get reported.
6.146.0 - 2025-11-05
@settings <#hypothesis.settings> now accepts equivalent string representations for settings.verbosity <#hypothesis.settings.verbosity>, settings.phases <#hypothesis.settings.phases>, and settings.suppress_health_check <#hypothesis.settings.suppress_health_check>. For example:
# these two are now equivalent... settings(verbosity=Verbosity.verbose) settings(verbosity="verbose") # ...as are these two... settings(phases=[Phase.explicit]) settings(phases=["explicit"]) # ...and these two. settings(suppress_health_check=[HealthCheck.filter_too_much]) settings(suppress_health_check=["filter_too_much"])
This release also changes the canonical value of Verbosity <#hypothesis.Verbosity>, Phase <#hypothesis.Phase>, and HealthCheck <#hypothesis.HealthCheck> members to a string instead of an integer. For example, Phase.reuse.value == "explicit" as of this release, where previously Phase.reuse.value == 1.
Instantiating Verbosity <#hypothesis.Verbosity>, Phase <#hypothesis.Phase>, or HealthCheck <#hypothesis.HealthCheck> with an integer, such as Verbosity(0), is now deprecated.
6.145.1 - 2025-11-03
Refactor some internal logic around strategy definitions.
6.145.0 - 2025-11-03
Hypothesis previously required attrs <https://pypi.org/project/attrs/> as a dependency. This release removes that dependency, so that the only required dependency of Hypothesis is sortedcontainers <https://pypi.org/project/sortedcontainers/>.
All attrs-specific features of Hypothesis, such as using from_type() <#hypothesis.strategies.from_type> with attrs classes, will continue to behave as before.
6.144.1 - 2025-11-03
Tweak how Hypothesis hides internal tracebacks to fix an error under rare conditions (issue #3822 <https://github.com/HypothesisWorks/hypothesis/issues/3822>).
6.144.0 - 2025-11-02
This release adds support for Fraction objects as min_value and max_value bounds in decimals() <#hypothesis.strategies.decimals>, if they can be exactly represented as decimals in the target precision (issue #4466 <https://github.com/HypothesisWorks/hypothesis/issues/4466>).
Bounding decimals() <#hypothesis.strategies.decimals> with other values that cannot be exactly represented is now deprecated; previously the bounds could be off by one.
6.143.1 - 2025-11-02
from_type() <#hypothesis.strategies.from_type> now correctly handles annotated-types <https://pypi.org/project/annotated-types/> annotations on typing.TypedDict fields which are also marked as being ReadOnly, Required, or NotRequired (issue #4474 <https://github.com/HypothesisWorks/hypothesis/issues/4474>).
6.143.0 - 2025-11-01
The extras for NumPy <#hypothesis-numpy> and pandas <#hypothesis-pandas> now support automatically inferring a strategy for dtype="O". Previously, Hypothesis required an explicit elements strategy to be passed, for example nps.arrays("O", shape=(1,), elements=st.just(object())). Now, Hypothesis automatically infers elements=st.from_type(object).
Thanks to Shaun Read for identifying and fixing this!
6.142.5 - 2025-10-31
This patch fixes binary_operation() <#hypothesis.extra.ghostwriter.binary_operation> to include imports for hypothesis.extra.numpy <#module-hypothesis.extra.numpy> strategies such as arrays() <#hypothesis.extra.numpy.arrays>, scalar_dtypes() <#hypothesis.extra.numpy.scalar_dtypes>, and array_shapes() <#hypothesis.extra.numpy.array_shapes> when ghostwriting tests for functions with numpy array parameters (issue #4576 <https://github.com/HypothesisWorks/hypothesis/issues/4576>).
6.142.4 - 2025-10-25
Improve the accuracy of test timing reports, by tracking the start time of each test case closer to when the test is executed.
6.142.3 - 2025-10-22
Fix a recursion error when observability <#observability> is enabled and a test generates an object with a recursive reference, like a = []; a.append(a).
6.142.2 - 2025-10-20
Remove a case where Hypothesis would interact with the global python:random.Random instance if Hypothesis internals were used directly.
6.142.1 - 2025-10-16
Simplify some internal typing logic after dropping Python 3.9.
6.142.0 - 2025-10-16
This release drops support for Python 3.9, which reached end of life in October 2025 <https://devguide.python.org/versions/>.
6.141.1 - 2025-10-15
Fixes an error when using the Ghostwriter <#ghostwriter> with annotations that include python:typing.ForwardRef on Python 3.14 (issue #4565 <https://github.com/HypothesisWorks/hypothesis/issues/4565>).
6.141.0 - 2025-10-15
The from_field() <#hypothesis.extra.django.from_field> and from_form() <#hypothesis.extra.django.from_form> strategies from our Django extra <#hypothesis-django> now support FileField.
Thanks to Arjoonn Sharma for this fix!
6.140.4 - 2025-10-14
Clean up internal @overload type annotations.
6.140.3 - 2025-10-04
Fixes our bundled run_conformance_test() <#hypothesis.internal.conjecture.provider_conformance.run_conformance_test> not respecting avoid_realization <#hypothesis.internal.conjecture.providers.PrimitiveProvider.avoid_realization>.
6.140.2 - 2025-09-23
The automatic switch to the CI settings profile <#hypothesis.settings> now works under tox <https://pypi.org/project/tox/> (for tox >= 4.30.0).
6.140.1 - 2025-09-22
This patch re-enables the warning for incompatible shared() <#hypothesis.strategies.shared> strategies that was first enabled in v6.133.0 <changelog.html#v6-133-0> but disabled in v6.135.15 <changelog.html#v6-135-15>.
6.140.0 - 2025-09-22
characters() <#hypothesis.strategies.characters> now validates that the elements of the exclude_characters and include_characters arguments are single characters, which was always assumed internally. For example, exclude_characters=["a", "b"] is valid while exclude_characters=["ab"] will now raise an error up-front.
6.139.3 - 2025-09-22
Add phase to the hypothesis-specific metadata <#observability-hypothesis-metadata> in observability <#observability>.
6.139.2 - 2025-09-18
Internal refactoring for new lint rules.
6.139.1 - 2025-09-16
Fixed another typo in error message around function-scoped fixtures.
6.139.0 - 2025-09-16
Add get_current_profile_name() <#hypothesis.settings.get_current_profile_name>, which returns the name of the current settings profile.
6.138.17 - 2025-09-15
Fixed typo in error message around function-scoped fixtures.
6.138.16 - 2025-09-13
Improved error message for DeadlineExceeded <#hypothesis.errors.DeadlineExceeded>.
6.138.15 - 2025-09-08
Refactor some stateful testing internals for easier use by third-party libraries.
6.138.14 - 2025-09-02
Patch files written by hypothesis now use a deterministic ordering when multiple @example <#hypothesis.example> decorators are present.
6.138.13 - 2025-09-01
Fix a typo affecting pretty-printing of lambdas with complex default arguments.
6.138.12 - 2025-09-01
Improve automatic detection of the CI profile <#builtin-profiles> on various vendor-specific CI systems.
6.138.11 - 2025-09-01
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.138.10 - 2025-08-31
Internal refactor to simplify SearchStrategy <#hypothesis.strategies.SearchStrategy>.
6.138.9 - 2025-08-31
This patch further improves stringification of lambdas, by never returning a lambda source unless it is confirmed to compile to the same code object. This stricter check makes it possible to widen the search for a matching source block, so that it can often be found even if the file has been edited.
6.138.8 - 2025-08-29
Fixes a race condition under threading when using deferred() <#hypothesis.strategies.deferred>.
6.138.7 - 2025-08-28
Improves upon the cache eviction problem workaround of v6.135.12 <changelog.html#v6-135-12>.
6.138.6 - 2025-08-27
Documentation tweaks.
6.138.5 - 2025-08-27
Fixes a race condition under threading for strategies which trigger our filter-rewriting rules, like st.integers().filter(lambda x: abs(x) > 100).
6.138.4 - 2025-08-27
One of our shrinking passes for reducing failing inputs targets failures which require two numbers to add to the same value. This pass previously only worked for positive numbers. This patch fixes that, so it also works for negative numbers.
6.138.3 - 2025-08-24
This patch slightly improves the cache-hit rate for dictionaries() <#hypothesis.strategies.dictionaries> and certain unique lists() <#hypothesis.strategies.lists>.
6.138.2 - 2025-08-16
The type annotations for register_type_strategy() <#hypothesis.strategies.register_type_strategy> now indicate that it accepts registering types created with python:typing.TypeAliasType (aka type MyType = int).
6.138.1 - 2025-08-15
Internal refactoring and cleanup. As a result, hypothesis[cli] and hypothesis[ghostwriter] now require black>=20.8b0 instead of the previous black>=19.10b0.
6.138.0 - 2025-08-13
On Python 3.14, python:memoryview is newly generic. This release adds the ability for from_type() <#hypothesis.strategies.from_type> to resolve generic python:memoryview types on 3.14, like st.from_type(memoryview[CustomBufferClass]) . CustomBufferClass must implement __buffer__, as expected by python:memoryview.
6.137.3 - 2025-08-11
This patch makes the stringification of lambdas, and as a result certain automatic filter rewriting operations, more robust. This fixes issue #4498 <https://github.com/HypothesisWorks/hypothesis/issues/4498>, where a lambda was mistakenly identified as the identity operator due to inspect.getsource() only returning the first line of the lambda definition.
As a result, the repr of strategies filtered or mapped by lambda functions may change slightly.
6.137.2 - 2025-08-11
Add support for Python 3.14, which is currently in release candidate 1 <https://devguide.python.org/versions/>.
6.137.1 - 2025-08-05
Fixes a bug with solver-based alternative backends <#alternative-backends> (like crosshair <https://github.com/pschanely/CrossHair>) where symbolic values passed to event() <#hypothesis.event> would not be realized to concrete values at the end of the test case.
6.137.0 - 2025-08-05
Add the add_observability_callback <#hypothesis.internal.observability.add_observability_callback>, remove_observability_callback <#hypothesis.internal.observability.remove_observability_callback>, with_observability_callback <#hypothesis.internal.observability.with_observability_callback>, and observability_enabled <#hypothesis.internal.observability.observability_enabled> methods to the observability <#observability> interface. The previous TESTCASE_CALLBACKS <#hypothesis.internal.observability.TESTCASE_CALLBACKS> is deprecated.
This release also adds better threading support to observability callbacks. An observability callback will now only be called for observations generated by the same thread.
6.136.9 - 2025-08-04
Fix a threading race condition in one_of() <#hypothesis.strategies.one_of> initialization.
6.136.8 - 2025-08-04
Improve the error messages and documentation for HealthCheck <#hypothesis.HealthCheck>. Among others, the messaging is now more clear that health checks are proactive warnings, not correctness errors.
6.136.7 - 2025-08-01
Improve detection of sys.monitoring to avoid errors on GraalPy.
6.136.6 - 2025-07-28
When a test is executed concurrently from multiple threads, HealthCheck.too_slow <#hypothesis.HealthCheck.too_slow> is now disabled, since the Python runtime may decide to switch away from a thread for arbitrarily long and Hypothesis cannot track execution time per-thread.
6.136.5 - 2025-07-28
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.136.4 - 2025-07-25
HealthCheck.differing_executors <#hypothesis.HealthCheck.differing_executors> is no longer raised if a test is executed by different executors from different threads. HealthCheck.differing_executors <#hypothesis.HealthCheck.differing_executors> will still be raised if a test is executed by different executors in the same thread.
6.136.3 - 2025-07-23
When a test is executed concurrently from multiple threads, DeadlineExceeded <#hypothesis.errors.DeadlineExceeded> is now disabled, since the Python runtime may decide to switch away from a thread for longer than settings.deadline <#hypothesis.settings.deadline>, and Hypothesis cannot track execution time per-thread. See issue #4478 <https://github.com/HypothesisWorks/hypothesis/issues/4478>.
6.136.2 - 2025-07-21
@precondition <#hypothesis.stateful.precondition> now errors if used without @rule <#hypothesis.stateful.rule> or @invariant <#hypothesis.stateful.invariant>. Doing so has no effect and is indicative of a user error (issue #4413 <https://github.com/HypothesisWorks/hypothesis/issues/4413>).
6.136.1 - 2025-07-20
Fix on_observation() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.on_observation> being called with observations it wasn't responsible for generating if the test failed.
6.136.0 - 2025-07-19
When a failure found by an alternative backend <#alternative-backends> does not reproduce under the Hypothesis backend, we now raise FlakyBackendFailure <#hypothesis.errors.FlakyBackendFailure> instead of an internal FlakyReplay exception.
6.135.33 - 2025-07-18
Speculative fix for a thread-safety issue in calculating strategy labels.
6.135.32 - 2025-07-15
Improve the thread-safety of strategy validation.
Before this release, Hypothesis did not require that super().__init__() be called in SearchStrategy subclasses. Subclassing SearchStrategy is not supported or part of the public API, but if you are subclassing it anyway, you will need to make sure to call super().__init__() after this release.
6.135.31 - 2025-07-15
Fix a remaining thread-safety issue with the deprecation warning for use of the global random instance (see v6.135.24).
6.135.30 - 2025-07-14
Fix a remaining thread-safety issue with the recursion limit warning Hypothesis issues when an outside caller sets sys.setrecursionlimit (see v6.135.23).
6.135.29 - 2025-07-12
Optimize performance of sampled_from() <#hypothesis.strategies.sampled_from> and internal selection of stateful testing <#stateful> rules.
6.135.28 - 2025-07-12
Optimize the memory and speed of an internal datastructure for compactly storing integers.
6.135.27 - 2025-07-12
Improve thread-safety for stateful @initialize <#hypothesis.stateful.initialize> rules.
6.135.26 - 2025-07-05
Fix a "dictionary changed size during iteration" error that could occur under with register_random() <#hypothesis.register_random> under multiple threads.
6.135.25 - 2025-07-05
Improve thread safety of our sys.monitoring usage (by the Phase.shrink <#hypothesis.Phase.shrink> and Phase.explain <#hypothesis.Phase.explain> phases), as well as the internal computation of strategy labels.
6.135.24 - 2025-07-03
Makes the deprecation warning for using the global random instance thread-safe, as part of our work towards thread safety (issue #4451 <https://github.com/HypothesisWorks/hypothesis/issues/4451>).
6.135.23 - 2025-07-02
In order to de-flake RecursionError failures, Hypothesis sets a deterministic limit on sys.setrecursionlimit. This patch makes the setting of this limit aware of uses by Hypothesis from multiple threads, so it does not produce spurious warnings in multithreaded environments.
6.135.22 - 2025-07-02
Improves the thread safety of caching strategy definitions, as well as usage of strategy transformations like .map() <#hypothesis.strategies.SearchStrategy.map> and .filter() <#hypothesis.strategies.SearchStrategy.filter>.
6.135.21 - 2025-07-02
Fix the thread safety of @rule <#hypothesis.stateful.rule> definitions in RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>.
6.135.20 - 2025-06-30
Fixes reproduction_decorator being missing under hypothesis-specific metadata <#observability-hypothesis-metadata> in many observability <#observability> observations, when it should have been present.
6.135.19 - 2025-06-30
Improve threading compatibility of an internal helper for managing deterministic rng seeding.
6.135.18 - 2025-06-30
Remove an internal assertion which could trigger if (1) a lambda was present in the source code of a test, (2) and the source code file was edited on disk between the start of the python process and when Hypothesis runs the property.
6.135.17 - 2025-06-30
Refactor some internals related to the shrinker for better compatibility with free-threading (issue #4451 <https://github.com/HypothesisWorks/hypothesis/issues/4451>).
6.135.16 - 2025-06-26
Fixes an error when the _pytest module is present in sys.modules, but not the _pytest.outcomes or _pytest.fixtures modules. This can happen with code that imports just _pytest, without importing pytest.
6.135.15 - 2025-06-25
Temporarily disable the warning when shared() <#hypothesis.strategies.shared> strategies with the same key draw from different base strategies, due to false alarms. Once we fix the false alarms in a future release, the warning will be re-enabled.
6.135.14 - 2025-06-20
Speed up usages of sampled_from() <#hypothesis.strategies.sampled_from> by deferring evaluation of its repr, and truncating its repr for large collections (over 512 elements). This is especially noticeable when using sampled_from() <#hypothesis.strategies.sampled_from> with large collections. The repr of sampled_from() <#hypothesis.strategies.sampled_from> strategies involving sequence classes with custom reprs may change as a result of this release.
6.135.13 - 2025-06-20
Fixes a substantial performance regression in stateful tests from computing string representations, present since version 6.131.20.
6.135.12 - 2025-06-19
Fix a rare race condition in internal cache eviction logic.
6.135.11 - 2025-06-17
This patch fixes an error when importing our django extra <#hypothesis-django> (via hypothesis.extra.django) if django.contrib.auth was not in INSTALLED_APPS (issue #3716 <https://github.com/HypothesisWorks/hypothesis/issues/3716>).
Thanks to Chris Wesseling for this fix!
6.135.10 - 2025-06-15
Fix a rare race condition in fetch() <#hypothesis.database.ExampleDatabase.fetch>, where we might have read from a non-existent directory.
6.135.9 - 2025-06-13
Refactor some internal code related to patches to make it easier to test.
6.135.8 - 2025-06-13
Add type hints to internal code for patching.
6.135.7 - 2025-06-12
Fixes a race condition in add_listener() <#hypothesis.database.ExampleDatabase.add_listener> for DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> after version 6.135.1 where the listener might have tried to read a file that doesn't exist.
6.135.6 - 2025-06-11
This patch corrects the f-string formatting of a few array-related error messages.
6.135.5 - 2025-06-10
Improve the error message when applying @given <#hypothesis.given> to a pytest <https://pypi.org/project/pytest/> fixture with pytest 8.4.0.
6.135.4 - 2025-06-09
Further improve the performance of the constants-collection feature introduced in version 6.131.1, by ignoring large files and files with many constants.
6.135.3 - 2025-06-08
This release adds the experimental and unstable OBSERVABILITY_CHOICES <#hypothesis.internal.observability.OBSERVABILITY_CHOICES> option for observability <#observability>. If set, the choice sequence is included in metadata.choice_nodes, and choice sequence spans are included in metadata.choice_spans.
These are relatively low-level implementation detail of Hypothesis, and are exposed in observability for users building tools or research on top of Hypothesis. See PrimitiveProvider <#hypothesis.internal.conjecture.providers.PrimitiveProvider> for more details about the choice sequence and choice spans.
We are actively working towards a better interface for this. Feel free to use OBSERVABILITY_CHOICES <#hypothesis.internal.observability.OBSERVABILITY_CHOICES> to experiment, but don't rely on it yet!
6.135.2 - 2025-06-08
This patch restores compatibility when using the legacy Python 3.9 LL(1) parser <https://docs.python.org/3/whatsnew/3.9.html#new-parser> yet again, because the fix in version 6.131.33 was too brittle.
Thanks to Marco Ricci for this fix!
6.135.1 - 2025-06-05
DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> now removes empty directories after delete() <#hypothesis.database.ExampleDatabase.delete> is called.
6.135.0 - 2025-06-03
This release adds run_conformance_test() <#hypothesis.internal.conjecture.provider_conformance.run_conformance_test>, for use in testing implementations of alternative backends <#alternative-backends>.
6.134.0 - 2025-06-03
This patch adds hypothesis.extra.django.SimpleTestCase <#hypothesis.extra.django.SimpleTestCase> (issue #4117 <https://github.com/HypothesisWorks/hypothesis/issues/4117>)
Thanks to Chris Wesseling for this contribution!
6.133.2 - 2025-06-03
Internal changes to support hypofuzz <https://hypofuzz.com>.
6.133.1 - 2025-06-03
The to_json hook used internally when writing observability <#observability> reports is now supported on nested dataclasses (in addition to outermost dataclasses).
6.133.0 - 2025-06-02
Warn when shared() <#hypothesis.strategies.shared> strategies with the same key draw from different base strategies. This could lead to subtle failures or lower-than-expected example coverage.
6.132.0 - 2025-05-31
Add on_observation() <#hypothesis.internal.conjecture.providers.PrimitiveProvider.on_observation> to the internal alternative backends <#alternative-backends-internals> interface.
6.131.33 - 2025-05-31
This patch restores compatibility when using the legacy Python 3.9 LL(1) parser <https://docs.python.org/3/whatsnew/3.9.html#new-parser>, which was accidentally broken since version 6.130.13.
Thanks to Marco Ricci for this fix!
6.131.32 - 2025-05-30
fuzz_one_input <#fuzz-one-input> now writes observability reports <#observability> if observability is enabled, bringing it in line with the behavior of other standard ways to invoke a Hypothesis test.
6.131.31 - 2025-05-30
Improve documentation of @example <#hypothesis.example>.
6.131.30 - 2025-05-27
This patch resolves a Pandas FutureWarning (issue #4400 <https://github.com/HypothesisWorks/hypothesis/issues/4400>) caused by indexing with an integer key.
6.131.29 - 2025-05-27
The observations passed to TESTCASE_CALLBACKS <#hypothesis.internal.observability.TESTCASE_CALLBACKS> are now dataclasses, rather than dictionaries. The content written to .hypothesis/observed under HYPOTHESIS_EXPERIMENTAL_OBSERVABILITY remains the same.
6.131.28 - 2025-05-25
Add documentation to some internal APIs.
6.131.27 - 2025-05-24
Add PrimitiveProvider.replay_choices to the alternative backends <#alternative-backends> interface, to support warm-starting e.g. hypothesis-crosshair <https://pypi.org/project/hypothesis-crosshair/> from hypofuzz <https://pypi.org/project/hypofuzz/>.
6.131.26 - 2025-05-24
Improve ExampleDatabase <#hypothesis.database.ExampleDatabase> documentation.
6.131.25 - 2025-05-23
Add some internal type hints.
6.131.24 - 2025-05-23
Improve @settings <#hypothesis.settings> documentation.
6.131.23 - 2025-05-23
This patch adds GITLAB_CI to the environment variables checked when enabling the default CI settings <#hypothesis.settings> profile.
Thanks to Genevieve Mendoza for this contribution!
6.131.22 - 2025-05-22
Include note() <#hypothesis.note> and Phase.explain <#hypothesis.Phase.explain> output in the "representation" field of observability reports <#observability> for failing examples, to more closely match the output produced by Hypothesis.
6.131.21 - 2025-05-21
BackgroundWriteDatabase <#hypothesis.database.BackgroundWriteDatabase> instances now defer creating and starting a thread until first use.
6.131.20 - 2025-05-20
Improve the string representation of characters() <#hypothesis.strategies.characters> in some cases.
6.131.19 - 2025-05-19
Cap the length of bytestrings collected as part of the constants-collection feature introduced in version 6.131.1, as long bytestrings are unlikely to be useful.
6.131.18 - 2025-05-17
All ExampleDatabase <#hypothesis.database.ExampleDatabase> implementations in Hypothesis now implement __eq__.
6.131.17 - 2025-05-14
Further improve the performance of the new features introduced in version 6.131.1, by improving unions of large sets.
6.131.16 - 2025-05-13
Improve performance of an internal method to count possible choices.
6.131.15 - 2025-05-07
Improves output when assigning values to multiple target bundles in a stateful rule (issue #4361 <https://github.com/HypothesisWorks/hypothesis/issues/4361>).
6.131.14 - 2025-05-07
Internal optimization to reduce redundant choice sequence spans.
6.131.13 - 2025-05-07
Add a for_failure: bool = False parameter to provider.realize in alternative backends <#alternative-backends>, so that symbolic-based backends can increase their timeouts when realizing failures, which are more important than regular examples.
6.131.12 - 2025-05-06
Improve type hints for the single-argument form of one_of() <#hypothesis.strategies.one_of>. st.one_of(strategies) now matches the type of st.one_of(*strategies). For instance, st.one_of([st.integers(), st.none()]) now has the correct type of SearchStrategy[int | None] instead of SearchStrategy[Any].
6.131.11 - 2025-05-06
Fix incorrectly reporting alternative backends <#alternative-backends> as unsound in some cases.
6.131.10 - 2025-05-06
Remove more false-positive locations from Phase.explain <#hypothesis.Phase.explain> output, and add a new metadata.reproduction_decorator field in observability reports <#observability> for failing examples.
6.131.9 - 2025-04-25
Fix a BytesWarning after version 6.131.1 if the source code used the same value in both a normal and binary string.
6.131.8 - 2025-04-23
DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> will now fall back to (potentially non-atomic) copies rather than renames, if the temporary directory used for atomic write-and-rename is on a different filesystem to the configured database location (issue #4335 <https://github.com/HypothesisWorks/hypothesis/issues/4335>).
6.131.7 - 2025-04-22
Further improve the performance of the new features introduced in version 6.131.1.
6.131.6 - 2025-04-19
This patch makes the new features introduced in version 6.131.1 much faster, and fixes an internal RecursionError when working with deeply-nested code.
6.131.5 - 2025-04-18
Fix a rare case where database entries were kept after they were no longer needed when using Phase.target <#hypothesis.Phase.target>.
6.131.4 - 2025-04-18
Internal refactoring of the @settings <#hypothesis.settings> object, with no user-visible change.
6.131.3 - 2025-04-18
Fixes a rare internal error where new code from version 6.131.1 could fail if sys.modules is simultaneously modified, e.g. as a side effect of imports executed from another thread. Our Thread-Safety Policy <#thread-safety-policy> does not promise that this is supported, but we're happy to take reasonable fixes.
Thanks to Tony Li for reporting and fixing this issue.
6.131.2 - 2025-04-17
The pub-sub change listening interface of the Hypothesis database <#database> now correctly fires events for DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> if the directory was created after the listener was added.
Also disables on emscripten the constants-extraction feature introduced in 6.131.1 - 2025-04-17, where it caused substantial slowdown.
6.131.1 - 2025-04-17
Hypothesis now looks for constant values in the source code of your program, and sometimes uses them while generating examples. This lets Hypothesis generate interesting inputs that are specific to your program.
6.131.0 - 2025-04-10
Add is_hypothesis_test() <#hypothesis.is_hypothesis_test>, for third-party libraries which want to determine whether a test has been defined with Hypothesis.
6.130.13 - 2025-04-09
Refactor some internals.
6.130.12 - 2025-04-09
Lays some groundwork for future work on collecting interesting literals from the code being tested, for increased bug-finding power (issue #3127 <https://github.com/HypothesisWorks/hypothesis/issues/3127>). There is no user-visible change (yet!)
6.130.11 - 2025-04-08
Fix the caching behavior of sampled_from() <#hypothesis.strategies.sampled_from>, which in rare cases led to failing an internal assertion (issue #4339 <https://github.com/HypothesisWorks/hypothesis/issues/4339>).
6.130.10 - 2025-04-07
This patch deprecates creating a database using the abstract ExampleDatabase() class. Use one of the following instead:
- Replace ExampleDatabase(":memory:") with InMemoryExampleDatabase <#hypothesis.database.InMemoryExampleDatabase>.
- Replace ExampleDatabase("/path/to/dir") with DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase>.
- Replace ExampleDatabase() with either InMemoryExampleDatabase <#hypothesis.database.InMemoryExampleDatabase> or DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase>, depending on your needs. Previously, Hypothesis interpreted ExampleDatabase() as a DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> in the default .hypothesis directory, with a fallback to InMemoryExampleDatabase <#hypothesis.database.InMemoryExampleDatabase> if that location was not available.
6.130.9 - 2025-04-06
When reporting the always-failing, never-passing lines from the Phase.explain <#hypothesis.Phase.explain> phase, we now sort the reported lines so that local code shows up first, then third-party library code, then standard library code.
6.130.8 - 2025-04-02
Improves the documentation of settings.verbosity <#hypothesis.settings.verbosity> objects.
6.130.7 - 2025-04-02
Rename internal variables for clarity.
6.130.6 - 2025-03-31
Update the documentation link in HealthCheck <#hypothesis.HealthCheck> error messages to their new location in the documentation.
6.130.5 - 2025-03-28
Improve our internal type hints.
6.130.4 - 2025-03-25
Improve an additional interaction between the hypothesis-crosshair <https://pypi.org/project/hypothesis-crosshair/> backend <#alternative-backends> and our observability tools <#observability>.
6.130.3 - 2025-03-24
This patch improves the interaction between the hypothesis-crosshair <https://pypi.org/project/hypothesis-crosshair/> backend <#alternative-backends> and our observability tools <#observability>.
6.130.2 - 2025-03-22
Fix an issue with realizing symbolic values provided by alternative backends <#alternative-backends> when Hypothesis encounters an internal error in its engine.
6.130.1 - 2025-03-22
Improve the documentation for some strategies, including @composite <#hypothesis.strategies.composite> and data() <#hypothesis.strategies.data>.
6.130.0 - 2025-03-21
Nesting @given <#hypothesis.given> inside of @given <#hypothesis.given> is now a HealthCheck <#hypothesis.HealthCheck> failure. Nesting @given <#hypothesis.given> results in quadratic generation and shrinking behavior, and can usually be more cleanly expressed by replacing the inner function with a data() <#hypothesis.strategies.data> parameter on the outer given. For more details, see nested_given <#hypothesis.HealthCheck.nested_given>. (issue #4167 <https://github.com/HypothesisWorks/hypothesis/issues/4167>)
6.129.5 - 2025-03-21
Fixes an internal error when certain alternative backends <#alternative-backends> find a failure on their very first generated example.
6.129.4 - 2025-03-18
nothing() <#hypothesis.strategies.nothing> is now typed as SearchStrategy[Never], because no value can ever be drawn from it. This may help type checkers statically determine that some code is not reachable.
6.129.3 - 2025-03-16
This patch improves the string representation of fixed_dictionaries() <#hypothesis.strategies.fixed_dictionaries>.
6.129.2 - 2025-03-14
Improve how the shrinker checks for unnecessary work, leading to 10% less time spent shrinking on average, with no reduction in quality.
6.129.1 - 2025-03-13
randoms() <#hypothesis.strategies.randoms> no longer produces 1.0, matching the exclusive upper bound of random.Random.random (issue #4297 <https://github.com/HypothesisWorks/hypothesis/issues/4297>).
6.129.0 - 2025-03-11
This release adds a "hypothesis-urandom" backend <#alternative-backends>, which draws randomness from /dev/urandom instead of Python's PRNG. This is useful for users of Antithesis <https://antithesis.com/> who also have Hypothesis tests, allowing Antithesis mutation of /dev/urandom to drive Hypothesis generation. We expect it to be strictly slower than the default backend for everyone else.
It can be enabled with @settings(backend="hypothesis-urandom").
6.128.3 - 2025-03-11
For strategies which draw make recursive draws, including recursive() <#hypothesis.strategies.recursive> and deferred() <#hypothesis.strategies.deferred>, we now generate examples with duplicated subtrees more often. This tends to uncover interesting behavior in tests.
For instance, we might now generate a tree like this more often (though the details depend on the strategy):
┌─────┐
┌──────┤ a ├──────┐
│ └─────┘ │
┌──┴──┐ ┌──┴──┐
│ b │ │ a │
└──┬──┘ └──┬──┘
┌────┴────┐ ┌────┴────┐
┌──┴──┐ ┌──┴──┐ ┌──┴──┐ ┌──┴──┐
│ c │ │ d │ │ b │ │ ... │
└─────┘ └─────┘ └──┬──┘ └─────┘
┌────┴────┐
┌──┴──┐ ┌──┴──┐
│ c │ │ d │
└─────┘ └─────┘6.128.2 - 2025-03-10
Improves input validation for several strategies in our pandas extra <#hypothesis-pandas>, so that they raise a helpful InvalidArgument rather than OverflowError.
Discovered by our recent string generation upgrade.
6.128.1 - 2025-03-09
Rename a few internal classes for clarity.
6.128.0 - 2025-03-09
text() <#hypothesis.strategies.text> now occasionally generates from a preselected list of strings which are likely to find bugs. These include ligatures, right-to-left and top-to-bottom text, emojis, emoji modifiers, strings like "Infinity", "None", and "FALSE", and other interesting things. This is especially useful when testing the full unicode range, where the search space is too large for uniform sampling to be very effective.
Of course, examples generated this way shrink just like they normally would. It was always possible for Hypothesis to generate these strings; it is just more likely after this change. From the outside, it is as if Hypothesis generated the example completely randomly.
Many thanks to the Big List of Naughty Strings <https://github.com/minimaxir/big-list-of-naughty-strings>, Text Rendering Hates You <https://faultlore.com/blah/text-hates-you/>, and Text Editing Hates You Too <https://lord.io/text-editing-hates-you-too/> for forming the basis of this list.
6.127.9 - 2025-03-06
We now provide a better string representation for one_of() <#hypothesis.strategies.one_of> strategies, by flattening consecutive | combinations. For instance:
>>> st.integers() | st.text() | st.booleans() # previously: one_of(one_of(integers(), text()), booleans()) one_of(integers(), text(), booleans())
Explicit calls to one_of() <#hypothesis.strategies.one_of> remain unflattened, in order to make tracking down complicated one_of() <#hypothesis.strategies.one_of> constructions easier:
>>> st.one_of(st.integers(), st.one_of(st.text(), st.booleans())) one_of(integers(), one_of(text(), booleans()))
We print one_of in reprs (rather than integers() | text() | ...) for consistency with reprs containing .filter or .map calls, which uses the full one_of to avoid ambiguity.
6.127.8 - 2025-03-06
This patch improves shrinking behavior for values from text() <#hypothesis.strategies.text> and binary() <#hypothesis.strategies.binary> which contain duplicate elements, like "zzzabc". It also improves shrinking for bugs which require the same character to be drawn from two different text() <#hypothesis.strategies.text> strategies to trigger.
6.127.7 - 2025-03-05
Fix a type-hinting regression from version 6.125.1, where we would no longer guarantee the type of the argument to .filter predicates (issue #4269 <https://github.com/HypothesisWorks/hypothesis/issues/4269>).
# x was previously Unknown, but is now correctly guaranteed to be int st.integers().filter(lambda x: x > 0)
6.127.6 - 2025-03-04
This patch tweaks the performance of Phase.target <#hypothesis.Phase.target>, avoiding aborting some test cases when it would be better to finish generating them.
6.127.5 - 2025-03-03
This patch fixes a bug where from_type() <#hypothesis.strategies.from_type> would error on certain types involving Protocol (issue #4194 <https://github.com/HypothesisWorks/hypothesis/issues/4194>).
6.127.4 - 2025-03-02
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.127.3 - 2025-02-28
Improve shrinking of non-standard NaN float values (issue #4277 <https://github.com/HypothesisWorks/hypothesis/issues/4277>).
6.127.2 - 2025-02-24
Adjust type hints for the pub-sub database implementation in version 6.126.0, and remove a remnant debug print in its implementation.
6.127.1 - 2025-02-23
Improve the clarity of printing counterexamples in stateful testing <#stateful>, by avoiding confusing Bundle <#hypothesis.stateful.Bundle> references with equivalent values drawn from a regular strategy.
For example, we now print:
a_0 = state.add_to_bundle(a=0) state.unrelated(value=0)
instead of
a_0 = state.add_to_bundle(a=0) state.unrelated(value=a_0)
if the unrelated rule draws from a regular strategy such as integers() <#hypothesis.strategies.integers> instead of the a bundle.
6.127.0 - 2025-02-23
This releases adds support for type aliases created with the type statement (new in python 3.12) to from_type() <#hypothesis.strategies.from_type> and register_type_strategy() <#hypothesis.strategies.register_type_strategy>.
6.126.0 - 2025-02-18
The Hypothesis database <#database> now supports a pub-sub interface to efficiently listen for changes in the database, via .add_listener and .remove_listener. While all databases that ship with Hypothesis support this interface, implementing it is not required for custom database subclasses. Hypothesis will warn when trying to listen on a database without support.
This feature is currently only used downstream in hypofuzz <https://github.com/zac-hd/hypofuzz>.
6.125.3 - 2025-02-11
Improves sharing of some internal cache behavior.
6.125.2 - 2025-02-06
Optimize performance (improves speed by ~5%) and clarify the wording in an error message.
6.125.1 - 2025-02-03
Fixes a bug since around version 6.124.4 where we might have generated -0.0 for st.floats(min_value=0.0), which is unsound.
6.125.0 - 2025-02-03
Add 2024.12 to the list of recognized Array API versions in hypothesis.extra.array_api.
6.124.9 - 2025-02-01
Registration of experimental Alternative backends for Hypothesis <#alternative-backends> is now done via hypothesis.internal.conjecture.providers.AVAILABLE_PROVIDERS instead of hypothesis.internal.conjecture.data.AVAILABLE_PROVIDERS.
6.124.8 - 2025-02-01
Refactor some internals for better type hinting.
6.124.7 - 2025-01-25
Internal renamings.
6.124.6 - 2025-01-25
More work on internal type hints.
6.124.5 - 2025-01-25
Internal refactoring to make some stateful internals easier to access.
6.124.4 - 2025-01-25
Refactoring of our internal input generation. This shouldn't lead to any changes in the distribution of test inputs. If you notice any, please open an issue!
6.124.3 - 2025-01-24
Some Hypothesis internals now use the number of choices as a yardstick of input size, rather than the entropy consumed by those choices. We don't expect this to cause significant behavioral changes.
6.124.2 - 2025-01-21
Improves our internal caching logic for test cases.
6.124.1 - 2025-01-18
fuzz_one_input <#fuzz-one-input> is now implemented using an alternative backend <#alternative-backends>. This brings the interpretation of the fuzzer-provided bytestring closer to the fuzzer mutations, allowing the mutations to work more reliably. We hope to use this backend functionality to improve fuzzing integration (e.g. atheris issue #20 <https://github.com/google/atheris/issues/20>) in the future!
6.124.0 - 2025-01-16
The Hypothesis example database <#database> now uses a new internal format to store examples. This new format is not compatible with the previous format, so stored entries will not carry over.
The database is best thought of as a cache that may be invalidated at times. Instead of relying on it for correctness, we recommend using @example <#hypothesis.example> to specify explicit examples. When using databases across environments (such as connecting a GitHubArtifactDatabase <#hypothesis.database.GitHubArtifactDatabase> database in CI to your local environment), we recommend using the same version of Hypothesis for each where possible, for maximum reproducibility.
6.123.17 - 2025-01-13
This patch improves certain corner cases for reporting of flaky errors (issue #4183 <https://github.com/HypothesisWorks/hypothesis/issues/4183> and issue #4228 <https://github.com/HypothesisWorks/hypothesis/issues/4228>).
6.123.16 - 2025-01-13
Improves an edge case in one of our integer and float shrinking passes.
6.123.15 - 2025-01-11
Improves one of our shrinking passes for integers which require a constant relative difference to trigger the bug.
6.123.14 - 2025-01-11
Avoid realizing symbolic values from Alternative backends for Hypothesis <#alternative-backends> when Verbosity <#hypothesis.Verbosity> is verbose or higher.
6.123.13 - 2025-01-09
More internal code refactoring.
6.123.12 - 2025-01-09
DirectoryBasedExampleDatabase <#hypothesis.database.DirectoryBasedExampleDatabase> now creates files representing database entries atomically, avoiding a very brief intermediary state where a file could be created but not yet written to.
6.123.11 - 2025-01-09
Internal code refactoring.
6.123.10 - 2025-01-09
Fixes a bug caused by alternative backends <#alternative-backends> raising hypothesis.errors.BackendCannotProceed in certain cases.
6.123.9 - 2025-01-08
Add internal type hints to our pretty printer.
6.123.8 - 2025-01-08
The shrinker contains a pass aimed at integers which are required to sum to a value. This patch extends that pass to floats as well.
6.123.7 - 2025-01-07
Internal type hint additions and refactorings.
6.123.6 - 2025-01-07
@reproduce_failure() <#hypothesis.reproduce_failure> now uses a newer internal interface to represent failures. As a reminder, this representation is not intended to be stable across versions or with respect to changes in the test.
6.123.5 - 2025-01-07
Internal code refactoring for the typed choice sequence (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>). May have some neutral effect on shrinking.
6.123.4 - 2025-01-06
This patch improves shrinking involving long strings or byte sequences whose value is not relevant to the failure.
6.123.3 - 2025-01-06
This release further improves shrinking of strategies using one_of() <#hypothesis.strategies.one_of>, allowing the shrinker to more reliably move between branches of the strategy.
6.123.2 - 2024-12-27
The shrinker now uses the typed choice sequence (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>) when ordering failing examples. As a result, Hypothesis may now report a different minimal failing example for some tests. We expect most cases to remain unchanged.
6.123.1 - 2024-12-24
Our pytest plugin now emits a warning if you set Pytest's norecursedirs config option in such a way that the .hypothesis directory would be searched for tests. This reliably indicates that you've made a mistake which slows down test collection, usually assuming that your configuration extends the set of ignored patterns when it actually replaces them. (issue #4200 <https://github.com/HypothesisWorks/hypothesis/issues/4200>)
6.123.0 - 2024-12-23
from_type() <#hypothesis.strategies.from_type> can now handle constructors with required positional-only arguments if they have type annotations. Previously, we only passed arguments by keyword.
6.122.7 - 2024-12-23
This patch lays some groundwork for migrating our internal representation to the typed choice sequence (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>)
6.122.6 - 2024-12-21
This patch cleans up some internal code around clamping floats.
6.122.5 - 2024-12-20
This release improves shrinking in some cases, especially for strategies using one_of() <#hypothesis.strategies.one_of>. This will typically improve shrinking speed and may in some cases improve the end result.
6.122.4 - 2024-12-19
This patch improves generation performance for the provisional domains() <#hypothesis.provisional.domains> strategy, including its derivative strategies urls() <#hypothesis.provisional.urls> and emails() <#hypothesis.strategies.emails>.
6.122.3 - 2024-12-08
This patch improves our error and warning messages.
- Add a warning for st.text("ascii") - you probably meant st.text(st.characters(codec="ascii")). Similarly for "utf-8".
- Recommend remedies in the error message of Unsatisfiable.
- When @given errors because it was given an extra keyword argument, and the keyword matches a setting name like max_examples, recommend @settings(max_examples=...) instead.
6.122.2 - 2024-12-08
This patch updates some outdated external links in our documentation.
6.122.1 - 2024-12-01
Fix from_type() <#hypothesis.strategies.from_type> on collections.abc.Callable returning None.
6.122.0 - 2024-11-29
This release adds .span_start() and .span_end() methods to our internal PrimitiveProvider interface, for use by Alternative backends for Hypothesis <#alternative-backends>.
6.121.2 - 2024-11-29
This patch updates our autoformatting tools, improving our code style without any API changes.
6.121.1 - 2024-11-29
This release brings back the old representation of hypothesis.stateful.Bundle <#hypothesis.stateful.Bundle>, reverting most changes of PR #4124 <https://github.com/HypothesisWorks/hypothesis/pull/4124>.
6.121.0 - 2024-11-28
This release adds BackgroundWriteDatabase <#hypothesis.database.BackgroundWriteDatabase>, a new database backend which defers writes on the wrapped database to a background thread. This allows for low-overhead writes in performance-critical environments like fuzz_one_input <#fuzz-one-input>.
6.120.0 - 2024-11-27
- This release changes our input distribution for low max_examples. Previously, we capped the size of inputs when generating at least the first 10 inputs, with the reasoning that early inputs to a property should be small. However, this meant properties with max_examples=10 would consistent entirely of small inputs. This patch removes the hard lower bound so that inputs to these properties are more representative of the input space.
- When a user requests an interactive input via strategy.example, we generate and cache a batch of 100 inputs, returning the first one. This can be expensive for large strategies or when only a few examples are needed. This release improves the speed of strategy.example by lowering the batch size to 10.
6.119.4 - 2024-11-22
This patch fixes a bug since 6.99.13 - 2024-03-24 where only interactively-generated values (via data.draw) would be reported in the arguments field of our observability output <#observability>. Now, all values are reported.
6.119.3 - 2024-11-17
Hypothesis collects coverage information during the Phase.shrink <#hypothesis.Phase.shrink> and Phase.explain <#hypothesis.Phase.explain> phases in order to show a more informative error message. On 3.12+, this uses sys.monitoring. This patch improves the performance of coverage collection on 3.12+ by disabling events we don't need.
6.119.2 - 2024-11-17
This patch refactors some internals to prepare for future work using our IR (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>).
6.119.1 - 2024-11-15
This patch migrates some more internals (around generating novel inputs) to the IR layer (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>).
6.119.0 - 2024-11-15
This release improves Hypothesis' handling of ExceptionGroup - it's now able to detect marker detections if they're inside a group and attempts to resolve them. Note that this handling is still a work in progress and might not handle edge cases optimally. Please open issues if you encounter any problems or unexpected behavior with it.
6.118.9 - 2024-11-15
Internal refactorings in preparation for upcoming changes.
6.118.8 - 2024-11-12
Internal renamings.
6.118.7 - 2024-11-10
This patch removes some # type: ignore comments following a mypy <https://pypi.org/project/mypy/> update.
6.118.6 - 2024-11-10
When Hypothesis replays examples from its test database that it knows were previously fully shrunk it will no longer try to shrink them again.
This should significantly speed up development workflows for slow tests, as the shrinking could contribute a significant delay when rerunning the tests.
In some rare cases this may cause minor reductions in example quality. This was considered an acceptable tradeoff for the improved test runtime.
6.118.5 - 2024-11-10
This patch avoids computing some string representations we won't need, giving a small speedup (part of issue #4139 <https://github.com/HypothesisWorks/hypothesis/issues/4139>).
6.118.4 - 2024-11-10
This patch migrates the optimisation algorithm for targeted property-based testing <#targeted> to our IR layer (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>). This should result in moderately different (and hopefully improved) exploration behavior in tests which use hypothesis.target() <#hypothesis.target>.
6.118.3 - 2024-11-10
This patch adds more type hints to internal Hypothesis code.
6.118.2 - 2024-11-09
This patch migrates the Phase.explain <#hypothesis.Phase.explain> phase to our IR layer (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>). This should improve both its speed and precision.
6.118.1 - 2024-11-09
This patch updates some internals around how we determine an input is too large to finish generating.
6.118.0 - 2024-11-08
The urls() <#hypothesis.provisional.urls> strategy no longer generates URLs where the port number is 0.
This change is motivated by the idea that the generated URLs should, at least in theory, be possible to fetch. The port number 0 is special; if a server binds to port 0, the kernel will allocate an unused, and non-zero, port instead. That means that it's not possible for a server to actually be listening on port 0. This motivation is briefly described in the documentation for urls() <#hypothesis.provisional.urls>.
Thanks to @gmacon for fixing issue #4157 <https://github.com/HypothesisWorks/hypothesis/issues/4157>!
6.117.0 - 2024-11-07
This changes the behaviour of settings profiles so that if you reregister the currently loaded profile it will automatically reload it. Previously you would have had to load it again.
In particular this means that if you register a "ci" profile, it will automatically be used when Hypothesis detects you are running on CI.
6.116.0 - 2024-11-01
Hypothesis now detects if it is running on a CI server and provides better default settings for running on CI in this case.
6.115.6 - 2024-10-30
This patch changes the priority order of pretty printing logic so that a user provided pretty printing method will always be used in preference to e.g. printing it like a dataclass.
6.115.5 - 2024-10-23
This patch restores diversity to the outputs of from_type(type) <#hypothesis.strategies.from_type> (issue #4144 <https://github.com/HypothesisWorks/hypothesis/issues/4144>).
6.115.4 - 2024-10-23
This release improves pretty printing of nested classes to include the outer class name in their printed representation.
6.115.3 - 2024-10-16
This patch fixes a regression from version 6.115.2 where generating values from integers() <#hypothesis.strategies.integers> with certain values for min_value and max_value would error.
6.115.2 - 2024-10-14
This release improves integer shrinking by folding the endpoint upweighting for integers() <#hypothesis.strategies.integers> into the weights parameter of our IR (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>).
If you maintain an alternative backend as part of our (for now explicitly unstable) Alternative backends for Hypothesis <#alternative-backends>, this release changes the type of the weights parameter to draw_integer and may be a breaking change for you.
6.115.1 - 2024-10-14
This patch improves the performance of from_type() <#hypothesis.strategies.from_type> with pydantic.types.condate <https://docs.pydantic.dev/latest/api/types/#pydantic.types.condate> (issue #4000 <https://github.com/HypothesisWorks/hypothesis/issues/4000>).
6.115.0 - 2024-10-12
This improves the formatting of dataclasses and attrs classes when printing falsifying examples.
6.114.1 - 2024-10-10
This patch upgrades remaining type annotations to Python 3.9 syntax.
6.114.0 - 2024-10-09
This release drops support for Python 3.8, which reached end of life on 2024-10-07 <https://devguide.python.org/versions/>.
6.113.0 - 2024-10-09
This release adds hypothesis.errors.BackendCannotProceed, an unstable API for use by Alternative backends for Hypothesis <#alternative-backends>.
6.112.5 - 2024-10-08
This release fixes a regression where hypothesis.stateful.Bundle <#hypothesis.stateful.Bundle> did not work properly with .flatmap() <#hypothesis.strategies.SearchStrategy.flatmap> functionality (issue #4128 <https://github.com/HypothesisWorks/hypothesis/issues/4128>).
6.112.4 - 2024-10-06
This patch tweaks the paths in @example(...) patches, so that both git apply and patch will work by default.
6.112.3 - 2024-10-05
This release refactors internals of hypothesis.stateful.Bundle <#hypothesis.stateful.Bundle> to have a more consistent representation internally.
6.112.2 - 2024-09-29
This patch fixes an internal error when the __context__ attribute of a raised exception leads to a cycle (issue #4115 <https://github.com/HypothesisWorks/hypothesis/issues/4115>).
6.112.1 - 2024-09-13
This patch removes a now-incorrect internal assertion about numpy's typing after recent numpy changes (currently only in numpy's nightly release).
6.112.0 - 2024-09-05
This release adds support for variable-width bytes in our IR layer (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>), which should mean improved performance anywhere you use binary() <#hypothesis.strategies.binary>. If you maintain an alternative backend as part of our (for now explicitly unstable) Alternative backends for Hypothesis <#alternative-backends>, this release changes the draw_* interface and may be a breaking change for you.
6.111.2 - 2024-08-24
This patch contains some internal code cleanup. There is no user-visible change.
6.111.1 - 2024-08-15
This patch improves shrinking in cases involving 'slips' from one strategy to another. Highly composite strategies are the most likely to benefit from this change.
This patch also reduces the range of python:datetime.datetime generated by from_model() <#hypothesis.extra.django.from_model> in order to avoid <https://code.djangoproject.com/ticket/35683>.
6.111.0 - 2024-08-11
Alternative backends for Hypothesis <#alternative-backends> can now implement .observe_test_case() and observe_information_message() methods, to record backend-specific metadata and messages in our observability output <#observability> (issue #3845 <https://github.com/HypothesisWorks/hypothesis/issues/3845> and hypothesis-crosshair#22 <https://github.com/pschanely/hypothesis-crosshair/issues/22>).
6.110.2 - 2024-08-11
Support __default__ field of TypeVar and support the same from typing-extensions <https://pypi.org/project/typing-extensions/> in from_type() <#hypothesis.strategies.from_type>.
6.110.1 - 2024-08-08
Add better error message for TypeIs types in from_type() <#hypothesis.strategies.from_type>.
6.110.0 - 2024-08-07
Support LiteralString in from_type() <#hypothesis.strategies.from_type>.
6.109.1 - 2024-08-07
This patch makes progress towards adding type hints to our internal conjecture engine (issue #3074 <https://github.com/HypothesisWorks/hypothesis/issues/3074>).
6.109.0 - 2024-08-07
This release allows using Annotated and ReadOnly types for TypedDict value types with from_type() <#hypothesis.strategies.from_type>.
6.108.10 - 2024-08-06
This patch fixes compatibility with attrs==24.1.0 <https://pypi.org/project/attrs/> on the nightly build of CPython, 3.14.0 pre-alpha (issue #4067 <https://github.com/HypothesisWorks/hypothesis/issues/4067>).
6.108.9 - 2024-08-05
This patch removes an assertion which was in fact possible in rare circumstances involving a small number of very large draws.
6.108.8 - 2024-08-04
This patch improves our example generation performance by adjusting our internal cache implementation.
6.108.7 - 2024-08-04
This patch improves our pretty-printer for unusual numbers.
- Signalling NaNs are now represented by using the struct module to show the exact value by converting from a hexadecimal integer
- CPython limits integer-to-string conversions <https://docs.python.org/3/library/stdtypes.html#integer-string-conversion-length-limitation> to mitigate DDOS attacks. We now use hexadecimal for very large integers, and include underscore separators for integers with ten or more digits.
6.108.6 - 2024-08-04
This patch improves generation speed in some cases by avoiding pretty-printing overhead for non-failing examples.
6.108.5 - 2024-07-28
This patch fixes a rare internal error when using integers() <#hypothesis.strategies.integers> with a high number of examples and certain {min, max}_value parameters (pull request #4059 <https://github.com/HypothesisWorks/hypothesis/pull/4059>).
6.108.4 - 2024-07-22
This patch addresses the issue of hypothesis potentially accessing mocked time.perf_counter during test execution (issue #4051 <https://github.com/HypothesisWorks/hypothesis/issues/4051>).
6.108.3 - 2024-07-22
Minor internal-only cleanups to some error-handling and reporting code.
6.108.2 - 2024-07-15
This patch disables hypothesis.target() <#hypothesis.target> on alternative backends where it would not work.
6.108.1 - 2024-07-14
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.108.0 - 2024-07-13
This patch changes most Flaky errors to use an ExceptionGroup, which makes the representation of these errors easier to understand.
6.107.0 - 2024-07-13
The alphabet= argument to from_regex() <#hypothesis.strategies.from_regex> now accepts unions of characters() <#hypothesis.strategies.characters> and sampled_from() <#hypothesis.strategies.sampled_from> strategies, in addition to accepting each individually.
This patch also fixes a bug where text(...).filter(re.compile(...).match) could generate non-matching instances if the regex pattern contained | (issue #4008 <https://github.com/HypothesisWorks/hypothesis/issues/4008>).
6.106.1 - 2024-07-12
This patch improves our pretty-printer (issue #4037 <https://github.com/HypothesisWorks/hypothesis/issues/4037>).
It also fixes the codemod for HealthCheck.all() from version 6.72, which was instead trying to fix Healthcheck.all() - note the lower-case c! Since our tests had the same typo, it all looked good... until issue #4030 <https://github.com/HypothesisWorks/hypothesis/issues/4030>.
6.106.0 - 2024-07-12
This release improves support for unions of numpy <https://pypi.org/project/numpy/> dtypes such as np.float64 | np.complex128 in from_type() <#hypothesis.strategies.from_type> and arrays() <#hypothesis.extra.numpy.arrays> (issue #4041 <https://github.com/HypothesisWorks/hypothesis/issues/4041>).
6.105.2 - 2024-07-12
This patch improves the reporting of certain flaky errors.
6.105.1 - 2024-07-07
This patch iterates on our experimental support for alternative backends (Alternative backends for Hypothesis <#alternative-backends>). See pull request #4029 <https://github.com/HypothesisWorks/hypothesis/pull/4029> for details.
6.105.0 - 2024-07-04
This release improves support for Django 5.0, and drops support for end-of-life Django versions (< 4.2).
Thanks to Joshua Munn for this contribution.
6.104.4 - 2024-07-04
Clean up internal cache implementation.
6.104.3 - 2024-07-04
This patch updates our autoformatting tools, improving our code style without any API changes.
6.104.2 - 2024-06-29
This patch fixes an issue when realizing symbolics with our experimental backend <#hypothesis.settings.backend> setting.
6.104.1 - 2024-06-25
Improves internal test coverage.
6.104.0 - 2024-06-24
This release adds strategies for Django's ModelChoiceField and ModelMultipleChoiceField (issue #4010 <https://github.com/HypothesisWorks/hypothesis/issues/4010>).
Thanks to Joshua Munn for this contribution.
6.103.5 - 2024-06-24
Fixes and reinstates full coverage of internal tests, which was accidentally disabled in pull request #3935 <https://github.com/HypothesisWorks/hypothesis/pull/3935> (issue #4003 <https://github.com/HypothesisWorks/hypothesis/issues/4003>).
6.103.4 - 2024-06-24
This release prevents a race condition inside internal cache implementation.
6.103.3 - 2024-06-24
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.103.2 - 2024-06-14
This patch improves our deduplication tracking across all strategies (pull request #4007 <https://github.com/HypothesisWorks/hypothesis/pull/4007>). Hypothesis is now less likely to generate the same input twice.
6.103.1 - 2024-06-05
Account for time spent in garbage collection during tests, to avoid flaky DeadlineExceeded errors as seen in issue #3975 <https://github.com/HypothesisWorks/hypothesis/issues/3975>.
Also fixes overcounting of stateful run times, a minor observability bug dating to version 6.98.9 (pull request #3890 <https://github.com/HypothesisWorks/hypothesis/pull/3890>).
6.103.0 - 2024-05-29
This release migrates the shrinker to our new internal representation, called the IR layer (pull request #3962 <https://github.com/HypothesisWorks/hypothesis/pull/3962>). This improves the shrinker's performance in the majority of cases. For example, on the Hypothesis test suite, shrinking is a median of 1.38x faster.
It is possible this release regresses performance while shrinking certain strategies. If you encounter strategies which reliably shrink more slowly than they used to (or shrink slowly at all), please open an issue!
You can read more about the IR layer at issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>.
6.102.6 - 2024-05-23
This patch fixes one of our shrinking passes getting into a rare O(n) case instead of O(log(n)).
6.102.5 - 2024-05-22
This patch fixes some introspection errors new in Python 3.11.9 and 3.13.0b1, for the Ghostwriter and from_type() <#hypothesis.strategies.from_type>.
6.102.4 - 2024-05-15
Internal developer documentation, no user-visible changes.
6.102.3 - 2024-05-15
This patch improves our shrinking of unique collections, such as dictionaries() <#hypothesis.strategies.dictionaries>, sets() <#hypothesis.strategies.sets>, and lists() <#hypothesis.strategies.lists> with unique=True.
6.102.2 - 2024-05-15
This patch fixes a rare internal error when generating very large elements from strategies (issue #3874 <https://github.com/HypothesisWorks/hypothesis/issues/3874>).
6.102.1 - 2024-05-13
This patch fixes an overly strict internal type assertion.
6.102.0 - 2024-05-13
This release improves our support for the annotated-types <https://pypi.org/project/annotated-types/> iterable GroupedMetadata protocol. In order to treat the elements "as if they had been unpacked", if one such element is a SearchStrategy <#hypothesis.strategies.SearchStrategy> we now resolve to that strategy. Previously, we treated this as an unknown filter predicate.
We expect this to be useful for libraries implementing custom metadata - instead of requiring downstream integration, they can implement the protocol and yield a lazily-created strategy. Doing so only if Hypothesis is in sys.modules gives powerful integration with no runtime overhead or extra dependencies.
6.101.0 - 2024-05-13
The from_model() <#hypothesis.extra.django.from_model> function currently tries to create a strategy for AutoField fields if they don't have auto_created set to True. The docs say it's supposed to skip all AutoField fields, so this patch updates the code to do what the docs say (issue #3978 <https://github.com/HypothesisWorks/hypothesis/issues/3978>).
6.100.8 - 2024-05-13
This patch adds some internal type annotations (issue #3074 <https://github.com/HypothesisWorks/hypothesis/issues/3074>). Thanks to Andrew Sansom for his contribution!
6.100.7 - 2024-05-12
This patch fixes a rare internal error when using integers() <#hypothesis.strategies.integers> with a high max_examples setting (issue #3974 <https://github.com/HypothesisWorks/hypothesis/issues/3974>).
6.100.6 - 2024-05-10
This patch improves our internal caching logic. We don't expect it to result in any performance improvements (yet!).
6.100.5 - 2024-05-06
This patch turns off a check in register_random() <#hypothesis.register_random> for possibly unreferenced RNG instances on the free-threaded build of CPython 3.13 because this check has a much higher false positive rate in the free-threaded build (issue #3965 <https://github.com/HypothesisWorks/hypothesis/issues/3965>).
Thanks to Nathan Goldbaum for this patch.
6.100.4 - 2024-05-05
This patch turns off a warning for functions decorated with typing.overload() and then composite() <#hypothesis.strategies.composite>, although only in that order (issue #3970 <https://github.com/HypothesisWorks/hypothesis/issues/3970>).
6.100.3 - 2024-05-04
This patch fixes a significant slowdown when using the precondition() <#hypothesis.stateful.precondition> decorator in some cases, due to expensive repr formatting internally (issue #3963 <https://github.com/HypothesisWorks/hypothesis/issues/3963>).
6.100.2 - 2024-04-28
Explicitly cast numpy.finfo.smallest_normal to builtin float in preparation for the numpy==2.0 <https://pypi.org/project/numpy/> release (issue #3950 <https://github.com/HypothesisWorks/hypothesis/issues/3950>)
6.100.1 - 2024-04-08
This patch improve a rare error message for flaky tests (issue #3940 <https://github.com/HypothesisWorks/hypothesis/issues/3940>).
6.100.0 - 2024-03-31
The from_dtype() <#hypothesis.extra.numpy.from_dtype> function no longer generates NaT ("not-a-time") values for the datetime64 or timedelta64 dtypes if passed allow_nan=False (issue #3943 <https://github.com/HypothesisWorks/hypothesis/issues/3943>).
6.99.13 - 2024-03-24
This patch includes the backend <#hypothesis.settings.backend> setting in the how_generated field of our observability output <#observability>.
6.99.12 - 2024-03-23
If you were running Python 3.13 (currently in alpha) with pytest-xdist <https://pypi.org/project/pytest-xdist/> and then attempted to pretty-print a lambda functions which was created using the eval() builtin, it would have raised an AssertionError. Now you'll get "lambda ...: <unknown>", as expected.
6.99.11 - 2024-03-20
This release improves an internal invariant.
6.99.10 - 2024-03-20
This patch fixes Hypothesis sometimes raising a Flaky error when generating collections of unique floats containing nan. See issue #3926 <https://github.com/HypothesisWorks/hypothesis/issues/3926> for more details.
6.99.9 - 2024-03-19
This patch continues our work on refactoring the shrinker (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>).
6.99.8 - 2024-03-18
This patch continues our work on refactoring shrinker internals (issue #3921 <https://github.com/HypothesisWorks/hypothesis/issues/3921>).
6.99.7 - 2024-03-18
This release resolves PermissionError that come from creating databases on inaccessible paths.
6.99.6 - 2024-03-14
This patch starts work on refactoring our shrinker internals. There is no user-visible change.
6.99.5 - 2024-03-12
This patch fixes a longstanding performance problem in stateful testing (issue #3618 <https://github.com/HypothesisWorks/hypothesis/issues/3618>), where state machines which generated a substantial amount of input for each step would hit the maximum amount of entropy and then fail with an Unsatisfiable error.
We now stop taking additional steps when we're approaching the entropy limit, which neatly resolves the problem without touching unaffected tests.
6.99.4 - 2024-03-11
Fix regression caused by using PEP 696 <https://peps.python.org/pep-0696/> default in TypeVar with Python 3.13.0a3.
6.99.3 - 2024-03-11
This patch further improves the type annotations in hypothesis.extra.numpy <#module-hypothesis.extra.numpy>.
6.99.2 - 2024-03-10
Simplify the type annotation of column() <#hypothesis.extra.pandas.column> and columns() <#hypothesis.extra.pandas.columns> by using PEP 696 <https://peps.python.org/pep-0696/> to avoid overloading.
6.99.1 - 2024-03-10
This patch implements type annotations for column() <#hypothesis.extra.pandas.column>.
6.99.0 - 2024-03-09
This release adds the experimental and unstable backend <#hypothesis.settings.backend> setting. See Alternative backends for Hypothesis <#alternative-backends> for details.
6.98.18 - 2024-03-09
This patch fixes issue #3900 <https://github.com/HypothesisWorks/hypothesis/issues/3900>, a performance regression for arrays() <#hypothesis.extra.numpy.arrays> due to the interaction of 6.98.12 - 2024-02-25 and 6.97.1 - 2024-01-27.
6.98.17 - 2024-03-04
This patch improves the type annotations in hypothesis.extra.numpy <#module-hypothesis.extra.numpy>, which makes inferred types more precise for both mypy <https://pypi.org/project/mypy/> and pyright <https://pypi.org/project/pyright/>, and fixes some strict-mode errors on the latter.
Thanks to Jonathan Plasse for reporting and fixing this in pull request #3889 <https://github.com/HypothesisWorks/hypothesis/pull/3889>!
6.98.16 - 2024-03-04
This patch paves the way for future shrinker improvements. There is no user-visible change.
6.98.15 - 2024-02-29
This release adds support for the Array API's 2023.12 release <https://data-apis.org/array-api/2023.12/> via the api_version argument in make_strategies_namespace() <#hypothesis.extra.array_api.make_strategies_namespace>. The API additions and modifications in the 2023.12 spec do not necessitate any changes in the Hypothesis strategies, hence there is no distinction between a 2022.12 and 2023.12 strategies namespace.
6.98.14 - 2024-02-29
This patch adjusts the printing of bundle values to correspond with their names when using stateful testing.
6.98.13 - 2024-02-27
This patch implements filter-rewriting for text() <#hypothesis.strategies.text> and binary() <#hypothesis.strategies.binary> with the search(), match(), or fullmatch() method of a re.compile()d regex.
6.98.12 - 2024-02-25
This patch implements filter-rewriting for most length filters on some additional collection types (issue #3795 <https://github.com/HypothesisWorks/hypothesis/issues/3795>), and fixes several latent bugs where unsatisfiable or partially-infeasible rewrites could trigger internal errors.
6.98.11 - 2024-02-24
This patch makes stateful testing somewhat less likely to get stuck when there are only a few possible rules.
6.98.10 - 2024-02-22
This patch adds a note <https://peps.python.org/pep-0678/> to errors which occur while drawing from a strategy, to make it easier to tell why your test failed in such cases.
6.98.9 - 2024-02-20
This patch ensures that observability <#observability> outputs include an informative repr for RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine> stateful tests, along with more detailed timing information.
6.98.8 - 2024-02-18
This patch improves the Ghostwriter <#ghostwriter> for binary operators.
6.98.7 - 2024-02-18
This patch improves import-detection in the Ghostwriter <#ghostwriter> (issue #3884 <https://github.com/HypothesisWorks/hypothesis/issues/3884>), particularly for from_type() <#hypothesis.strategies.from_type> and strategies from hypothesis.extra.*.
6.98.6 - 2024-02-15
This patch clarifies the documentation on stateful testing (issue #3511 <https://github.com/HypothesisWorks/hypothesis/issues/3511>).
6.98.5 - 2024-02-14
This patch improves argument-to-json conversion for observability <#observability> output. Checking for a .to_json() method on the object before a few other options like dataclass support allows better user control of the process (issue #3880 <https://github.com/HypothesisWorks/hypothesis/issues/3880>).
6.98.4 - 2024-02-12
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.98.3 - 2024-02-08
This patch fixes an error when generating observability <#observability> reports involving large (n > 1e308) integers.
6.98.2 - 2024-02-05
This patch refactors some internals. There is no user-visible change.
6.98.1 - 2024-02-05
This release improves our distribution of generated values for all strategies, by doing a better job of tracking which values we have generated before and avoiding generating them again.
For example, st.lists(st.integers()) previously generated ~5 each of [] [0] in 100 examples. In this release, each of [] and [0] are generated ~1-2 times each.
6.98.0 - 2024-02-05
This release deprecates use of the global random number generator while drawing from a strategy, because this makes test cases less diverse and prevents us from reporting minimal counterexamples (issue #3810 <https://github.com/HypothesisWorks/hypothesis/issues/3810>).
If you see this new warning, you can get a quick fix by using randoms() <#hypothesis.strategies.randoms>; or use more idiomatic strategies sampled_from() <#hypothesis.strategies.sampled_from>, floats() <#hypothesis.strategies.floats>, integers() <#hypothesis.strategies.integers>, and so on.
Note that the same problem applies to e.g. numpy.random, but for performance reasons we only check the stdlib random module - ignoring even other sources passed to register_random() <#hypothesis.register_random>.
6.97.6 - 2024-02-04
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.97.5 - 2024-02-03
This patch adds some observability information <#observability> about how many times predicates in assume() <#hypothesis.assume> or precondition() <#hypothesis.stateful.precondition> were satisfied, so that downstream tools can warn you if some were never satisfied by any test case.
6.97.4 - 2024-01-31
This patch improves formatting and adds some cross-references to our docs.
6.97.3 - 2024-01-30
Internal test refactoring.
6.97.2 - 2024-01-30
This patch slightly changes how we replay examples from the database <#database>: if the behavior of the saved example has changed, we now keep running the test case instead of aborting at the size of the saved example. While we know it's not the same example, we might as well continue running the test!
Because we now finish running a few more examples for affected tests, this might be a slight slowdown - but correspondingly more likely to find a bug.
We've also applied similar tricks to the Phase.target <#hypothesis.Phase.target> phase, where they are a pure performance improvement for affected tests.
6.97.1 - 2024-01-27
Improves the performance of the arrays() <#hypothesis.extra.numpy.arrays> strategy when generating unique values.
6.97.0 - 2024-01-25
Changes the distribution of sampled_from() <#hypothesis.strategies.sampled_from> when sampling from a Flag. Previously, no-flags-set values would never be generated, and all-flags-set values would be unlikely for large enums. With this change, the distribution is more uniform in the number of flags set.
6.96.4 - 2024-01-23
This patch slightly refactors some internals. There is no user-visible change.
6.96.3 - 2024-01-22
This patch fixes a spurious warning about slow imports when HYPOTHESIS_EXPERIMENTAL_OBSERVABILITY was set.
6.96.2 - 2024-01-21
This patch refactors some more internals, continuing our work on supporting alternative backends (issue #3086 <https://github.com/HypothesisWorks/hypothesis/issues/3086>). There is no user-visible change.
6.96.1 - 2024-01-18
Fix a spurious warning seen when running pytest's test suite, caused by never realizing we got out of initialization due to imbalanced hook calls.
6.96.0 - 2024-01-17
Warns when constructing a repr that is overly long. This can happen by accident if stringifying arbitrary strategies, and is expensive in time and memory. The associated deferring of these long strings in sampled_from() <#hypothesis.strategies.sampled_from> should also lead to improved performance.
6.95.0 - 2024-01-17
This release adds the ability to pass any object to note() <#hypothesis.note>, instead of just strings. The pretty-printed representation of the object will be used.
See also issue #3843 <https://github.com/HypothesisWorks/hypothesis/issues/3843>.
6.94.0 - 2024-01-16
This release avoids creating a .hypothesis directory when using register_type_strategy() <#hypothesis.strategies.register_type_strategy> (issue #3836 <https://github.com/HypothesisWorks/hypothesis/issues/3836>), and adds warnings for plugins which do so by other means or have other unintended side-effects.
6.93.2 - 2024-01-15
This patch improves observability <#observability> reports by moving timing information from metadata to a new timing key, and supporting conversion of additional argument types to json rather than string reprs via a .to_json() method (including e.g. Pandas dataframes).
Additionally, the too_slow <#hypothesis.HealthCheck.too_slow> health check will now report which strategies were slow, e.g. for strategies a, b, c, ...:
count | fraction | slowest draws (seconds) a | 3 | 65% | -- -- -- 0.357, 2.000 b | 8 | 16% | 0.100, 0.100, 0.100, 0.111, 0.123 c | 3 | 8% | -- -- 0.030, 0.050, 0.200 (skipped 2 rows of fast draws)
6.93.1 - 2024-01-15
This patch refactors some internals, continuing our work on supporting alternative backends (issue #3086 <https://github.com/HypothesisWorks/hypothesis/issues/3086>). There is no user-visible change.
6.93.0 - 2024-01-13
The from_lark() <#hypothesis.extra.lark.from_lark> strategy now accepts an alphabet= argument, which is passed through to from_regex() <#hypothesis.strategies.from_regex>, so that you can e.g. constrain the generated strings to a particular codec.
In support of this feature, from_regex() <#hypothesis.strategies.from_regex> will avoid generating optional parts which do not fit the alphabet. For example, from_regex(r"abc|def", alphabet="abcd") was previously an error, and will now generate only 'abc'. Cases where there are no valid strings remain an error.
6.92.9 - 2024-01-12
This patch refactors some internals, continuing our work on supporting alternative backends (issue #3086 <https://github.com/HypothesisWorks/hypothesis/issues/3086>). There is no user-visible change.
6.92.8 - 2024-01-11
This patch adds a test statistics <#statistics> event when a generated example is rejected via assume <#hypothesis.assume>.
This may also help with distinguishing gave_up examples in observability <#observability> (issue #3827 <https://github.com/HypothesisWorks/hypothesis/issues/3827>).
6.92.7 - 2024-01-10
This introduces the rewriting of length filters on some collection strategies (issue #3791 <https://github.com/HypothesisWorks/hypothesis/issues/3791>).
Thanks to Reagan Lee for implementing this feature!
6.92.6 - 2024-01-08
If a test uses sampled_from() <#hypothesis.strategies.sampled_from> on a sequence of strategies, and raises a TypeError, we now add a note <https://peps.python.org/pep-0678/> asking whether you meant to use one_of() <#hypothesis.strategies.one_of>.
Thanks to Vince Reuter for suggesting and implementing this hint!
6.92.5 - 2024-01-08
This patch registers explicit strategies for a handful of builtin types, motivated by improved introspection in PyPy 7.3.14 triggering existing internal warnings. Thanks to Carl Friedrich Bolz-Tereick for helping us work out what changed!
6.92.4 - 2024-01-08
This patch fixes an error when writing observability <#observability> reports without a pre-existing .hypothesis directory.
6.92.3 - 2024-01-08
This patch adds a new environment variable HYPOTHESIS_EXPERIMENTAL_OBSERVABILITY_NOCOVER, which turns on observability <#observability> data collection without collecting code coverage data, which may be faster on Python 3.11 and earlier.
Thanks to Harrison Goldstein for reporting and fixing issue #3821 <https://github.com/HypothesisWorks/hypothesis/issues/3821>.
6.92.2 - 2023-12-27
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.92.1 - 2023-12-16
This patch fixes a bug introduced in version 6.92.0, where using the data() <#hypothesis.strategies.data> strategy would fail to draw a dataclass() with a defaultdict field. This was due to a bug in the standard library which was fixed in 3.12 <https://github.com/python/cpython/pull/32056>, so we've vendored the fix.
6.92.0 - 2023-12-10
This release adds an experimental observability <https://en.wikipedia.org/wiki/Observability_(software)> mode. You can read the docs about it here <#observability>.
6.91.2 - 2023-12-10
This patch refactors some more internals, continuing our work on supporting alternative backends (issue #3086 <https://github.com/HypothesisWorks/hypothesis/issues/3086>). There is no user-visible change.
6.91.1 - 2023-12-08
This patch fixes an issue where builds() <#hypothesis.strategies.builds> could not be used with attrs <https://pypi.org/project/attrs/> objects that defined private attributes (i.e. attributes with a leading underscore). See also issue #3791 <https://github.com/HypothesisWorks/hypothesis/issues/3791>.
This patch also adds support more generally for using builds() <#hypothesis.strategies.builds> with attrs' alias parameter, which was previously unsupported.
This patch increases the minimum required version of attrs to 22.2.0.
6.91.0 - 2023-11-27
This release adds an optional payload argument to hypothesis.event() <#hypothesis.event>, so that you can clearly express the difference between the label and the value of an observation. Test statistics <#statistics> will still summarize it as a string, but future observability options can preserve the distinction.
6.90.1 - 2023-11-27
This patch supports assigning settings = settings(...) as a class attribute on a subclass of a .TestCase attribute of a RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>. Previously, this did nothing at all.
# works as of this release
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
settings = settings(max_examples=10000)
# the old way still works, but it's more verbose.
MyStateMachine.TestCase.settings = settings(max_examples=10000)
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
passThanks to Joey Tran for reporting these settings-related edge cases in stateful testing.
6.90.0 - 2023-11-20
This release makes it an error to assign settings = settings(...) as a class attribute on a RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>. This has never had any effect, and it should be used as a decorator instead:
class BadMachine(RuleBasedStateMachine):
"""This doesn't do anything, and is now an error!"""
settings = settings(derandomize=True)
@settings(derandomize=True)
class GoodMachine(RuleBasedStateMachine):
"""This is the right way to do it :-)"""6.89.1 - 2023-11-19
This patch refactors some internals. There is no user-visible change, but we hope to improve performance and unlock support for alternative backends such as symbolic execution with crosshair <https://pypi.org/project/crosshair-tool/> in future (issue #3086 <https://github.com/HypothesisWorks/hypothesis/issues/3086>).
Thanks to Liam DeVoe for this fantastic contribution!
6.89.0 - 2023-11-16
This release teaches from_type() <#hypothesis.strategies.from_type> to handle constraints implied by the annotated-types <https://pypi.org/project/annotated-types/> package - as used by e.g. pydantic <https://pypi.org/project/pydantic/>. This is usually efficient, but falls back to filtering in a few remaining cases.
Thanks to Viicos for pull request #3780 <https://github.com/HypothesisWorks/hypothesis/pull/3780>!
6.88.4 - 2023-11-13
This patch adds a warning when @st.composite <#hypothesis.strategies.composite> wraps a function annotated as returning a SearchStrategy <#hypothesis.strategies.SearchStrategy>, since this is usually an error (issue #3786 <https://github.com/HypothesisWorks/hypothesis/issues/3786>). The function should return a value, and the decorator will convert it to a function which returns a strategy.
6.88.3 - 2023-11-05
This patch refactors from_type(typing.Tuple), allowing register_type_strategy() <#hypothesis.strategies.register_type_strategy> to take effect for tuples instead of being silently ignored (issue #3750 <https://github.com/HypothesisWorks/hypothesis/issues/3750>).
Thanks to Nick Collins for reporting and extensive work on this issue.
6.88.2 - 2023-11-05
This patch improves the speed of the explain phase on python 3.12+, by using the new sys.monitoring module to collect coverage, instead of sys.settrace.
Thanks to Liam DeVoe for pull request #3776 <https://github.com/HypothesisWorks/hypothesis/pull/3776>!
6.88.1 - 2023-10-16
This patch improves register_type_strategy() <#hypothesis.strategies.register_type_strategy> when used with tuple subclasses, by preventing them from being interpreted as generic and provided to strategies like st.from_type(Sequence[int]) (issue #3767 <https://github.com/HypothesisWorks/hypothesis/issues/3767>).
6.88.0 - 2023-10-15
This release allows strategy-generating functions registered with register_type_strategy() <#hypothesis.strategies.register_type_strategy> to conditionally not return a strategy, by returning NotImplemented (issue #3767 <https://github.com/HypothesisWorks/hypothesis/issues/3767>).
6.87.4 - 2023-10-12
When randoms() <#hypothesis.strategies.randoms> was called with use_true_randoms=False, calling r.sample([], 0) would result in an error, when it should have returned an empty sequence to agree with the normal behaviour of random.sample(). This fixes that discrepancy (issue #3765 <https://github.com/HypothesisWorks/hypothesis/issues/3765>).
6.87.3 - 2023-10-06
This patch ensures that the hypothesis codemod <#codemods> CLI will print a warning instead of stopping with an internal error if one of your files contains invalid syntax (issue #3759 <https://github.com/HypothesisWorks/hypothesis/issues/3759>).
6.87.2 - 2023-10-06
This patch makes some small changes to our NumPy integration to ensure forward compatibility. Thanks to Mateusz Sokół for pull request #3761 <https://github.com/HypothesisWorks/hypothesis/pull/3761>.
6.87.1 - 2023-10-01
Fixes issue #3755 <https://github.com/HypothesisWorks/hypothesis/issues/3755>, where an internal condition turns out to be reachable after all.
6.87.0 - 2023-09-25
This release deprecates use of assume() <#hypothesis.assume> and reject() outside of property-based tests, because these functions work by raising a special exception (issue #3743 <https://github.com/HypothesisWorks/hypothesis/issues/3743>). It also fixes some type annotations (issue #3753 <https://github.com/HypothesisWorks/hypothesis/issues/3753>).
6.86.2 - 2023-09-18
Hotfix for issue #3747 <https://github.com/HypothesisWorks/hypothesis/issues/3747>, a bug in explain mode which is so rare that we missed it in six months of dogfooding. Thanks to mygrad <https://pypi.org/project/mygrad/> for discovering and promptly reporting this!
6.86.1 - 2023-09-17
This patch improves the documentation of @example(...).xfail() <#hypothesis.example.xfail> by adding a note about PEP 614 <https://peps.python.org/pep-0614/>, similar to @example(...).via() <#hypothesis.example.via>, and adds a warning when a strategy generates a test case which seems identical to one provided by an xfailed example.
6.86.0 - 2023-09-17
This release enables the Phase.explain <#hypothesis.Phase.explain> phase by default. We hope it helps you to understand why your failing tests have failed!
6.85.1 - 2023-09-16
This patch switches some of our type annotations to use typing.Literal when only a few specific values are allowed, such as UUID or IP address versions.
6.85.0 - 2023-09-16
This release deprecates the old whitelist/blacklist arguments to characters() <#hypothesis.strategies.characters>, in favor of include/exclude arguments which more clearly describe their effects on the set of characters which can be generated.
You can use Hypothesis' codemods <#codemods> to automatically upgrade to the new argument names. In a future version, the old names will start to raise a DeprecationWarning.
6.84.3 - 2023-09-10
This patch automatically disables the differing_executors <#hypothesis.HealthCheck.differing_executors> health check for methods which are also pytest parametrized tests, because those were mostly false alarms (issue #3733 <https://github.com/HypothesisWorks/hypothesis/issues/3733>).
6.84.2 - 2023-09-06
Building on recent releases, characters() <#hypothesis.strategies.characters> now accepts _any_ codec=, not just "utf-8" and "ascii".
This includes standard codecs from the codecs module and their aliases, platform specific and user-registered codecs if they are available, and python-specific text encodings <https://docs.python.org/3/library/codecs.html#python-specific-encodings> (but not text transforms or binary transforms).
6.84.1 - 2023-09-05
This patch by Reagan Lee makes st.text(...).filter(str.isidentifier) return an efficient custom strategy (issue #3480 <https://github.com/HypothesisWorks/hypothesis/issues/3480>).
6.84.0 - 2023-09-04
The from_regex() <#hypothesis.strategies.from_regex> strategy now takes an optional alphabet=characters(codec="utf-8") argument for unicode strings, like text() <#hypothesis.strategies.text>.
This offers more and more-consistent control over the generated strings, removing previously-hard-coded limitations. With fullmatch=False and alphabet=characters(), surrogate characters are now possible in leading and trailing text as well as the body of the match. Negated character classes such as [^A-Z] or \S had a hard-coded exclusion of control characters and surrogate characters; now they permit anything in alphabet= consistent with the class, and control characters are permitted by default.
6.83.2 - 2023-09-04
Add a health check that detects if the same test is executed several times by different executors <#custom-function-execution>. This can lead to difficult-to-debug problems such as issue #3446 <https://github.com/HypothesisWorks/hypothesis/issues/3446>.
6.83.1 - 2023-09-03
Pretty-printing of failing examples can now use functions registered with IPython.lib.pretty.for_type() or for_type_by_name(), as well as restoring compatibility with _repr_pretty_ callback methods which were accidentally broken in version 6.61.2 (issue #3721 <https://github.com/HypothesisWorks/hypothesis/issues/3721>).
6.83.0 - 2023-09-01
Adds a new codec= option in characters() <#hypothesis.strategies.characters>, making it convenient to produce only characters which can be encoded as ascii or utf-8 bytestrings.
Support for other codecs will be added in a future release.
6.82.7 - 2023-08-28
This patch updates our autoformatting tools, improving our code style without any API changes.
6.82.6 - 2023-08-20
This patch enables and fixes many more of ruff <https://pypi.org/project/ruff/>'s lint rules.
6.82.5 - 2023-08-18
Fixes the error message for missing [cli] extra.
6.82.4 - 2023-08-12
This patch ensures that we always close the download connection in GitHubArtifactDatabase <#hypothesis.database.GitHubArtifactDatabase>.
6.82.3 - 2023-08-08
We can now pretty-print combinations of zero enum.Flag values, like SomeFlag(0), which has never worked before.
6.82.2 - 2023-08-06
This patch fixes pretty-printing of combinations of enum.Flag values, which was previously an error (issue #3709 <https://github.com/HypothesisWorks/hypothesis/issues/3709>).
6.82.1 - 2023-08-05
Improve shrinking of floats in narrow regions that don't cross an integer boundary. Closes issue #3357 <https://github.com/HypothesisWorks/hypothesis/issues/3357>.
6.82.0 - 2023-07-20
from_regex() <#hypothesis.strategies.from_regex> now supports the atomic grouping ((?>...)) and possessive quantifier (*+, ++, ?+, {m,n}+) syntax added in Python 3.11 <https://docs.python.org/3/whatsnew/3.11.html#re>.
Thanks to Cheuk Ting Ho for implementing this!
6.81.2 - 2023-07-15
If the HYPOTHESIS_NO_PLUGINS <#envvar-HYPOTHESIS_NO_PLUGINS> environment variable is set, we'll avoid loading plugins <#entry-points> such as the old Pydantic integration <https://docs.pydantic.dev/latest/integrations/hypothesis/> or HypoFuzz' CLI options <https://hypofuzz.com/docs/quickstart.html#running-hypothesis-fuzz>.
This is probably only useful for our own self-tests, but documented in case it might help narrow down any particularly weird bugs in complex environments.
6.81.1 - 2023-07-11
Fixes some lingering issues with inference of recursive types in from_type() <#hypothesis.strategies.from_type>. Closes issue #3525 <https://github.com/HypothesisWorks/hypothesis/issues/3525>.
6.81.0 - 2023-07-10
This release further improves our .patch-file support from version 6.75, skipping duplicates, tests which use data() <#hypothesis.strategies.data> (and don't support @example() <#hypothesis.example>), and various broken edge-cases.
Because libCST <https://pypi.org/project/libcst/> has released version 1.0 which uses the native parser by default, we no longer set the LIBCST_PARSER_TYPE=native environment variable. If you are using an older version, you may need to upgrade or set this envvar for yourself.
6.80.1 - 2023-07-06
This patch updates some internal code for selftests. There is no user-visible change.
6.80.0 - 2023-06-27
This release drops support for Python 3.7, which reached end of life on 2023-06-27 <https://devguide.python.org/versions/>.
6.79.4 - 2023-06-27
Fixes occasional recursion-limit-exceeded errors when validating deeply nested strategies. Closes: issue #3671 <https://github.com/HypothesisWorks/hypothesis/issues/3671>
6.79.3 - 2023-06-26
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.79.2 - 2023-06-22
Improve the type rendered in from_type() <#hypothesis.strategies.from_type>, which improves the coverage of Ghostwriter.
6.79.1 - 2023-06-19
We now test against Python 3.12 beta in CI, and this patch fixes some new deprecations.
6.79.0 - 2023-06-17
This release changes register_type_strategy() <#hypothesis.strategies.register_type_strategy> for compatibility with PEP 585 <https://peps.python.org/pep-0585/>: we now store only a single strategy or resolver function which is used for both the builtin and the typing module version of each type (issue #3635 <https://github.com/HypothesisWorks/hypothesis/issues/3635>).
If you previously relied on registering separate strategies for e.g. list vs typing.List, you may need to use explicit strategies rather than inferring them from types.
6.78.3 - 2023-06-15
This release ensures that Ghostwriter does not use the deprecated aliases for the collections.abc classes in collections.
6.78.2 - 2023-06-13
This patch improves Ghostwriter's use of qualified names for re-exported functions and classes, and avoids importing useless TypeVars.
6.78.1 - 2023-06-12
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.78.0 - 2023-06-11
New input validation for recursive() <#hypothesis.strategies.recursive> will raise an error rather than hanging indefinitely if passed invalid max_leaves= arguments.
6.77.0 - 2023-06-09
from_type() <#hypothesis.strategies.from_type> now handles numpy array types: np.typing.ArrayLike, np.typing.NDArray, and parameterized versions including np.ndarray[shape, elem_type].
6.76.0 - 2023-06-04
Warn in from_type() <#hypothesis.strategies.from_type> if the inferred strategy has no variation (always returning default instances). Also handles numpy data types by calling from_dtype() <#hypothesis.extra.numpy.from_dtype> on the corresponding dtype, thus ensuring proper variation for these types.
6.75.9 - 2023-05-31
from_type() <#hypothesis.strategies.from_type> now works in cases where we use builds() <#hypothesis.strategies.builds> to create an instance and the constructor has an argument which would lead to recursion. Previously, this would raise an error if the argument had a default value.
Thanks to Joachim B Haga for reporting and fixing this problem.
6.75.8 - 2023-05-31
In preparation for supporting JAX in hypothesis.extra.array_api <#array-api>, this release supports immutable arrays being generated via xps.arrays() <#xps.arrays>. In particular, we internally removed an instance of in-place array modification, which isn't possible for an immutable array.
6.75.7 - 2023-05-30
This release fixes some .patch-file bugs from version 6.75, and adds automatic support for writing @hypothesis.example() or @example() depending on the current style in your test file - defaulting to the latter.
Note that this feature requires libcst <https://pypi.org/project/libcst/> to be installed, and black <https://pypi.org/project/black/> is strongly recommended. You can ensure you have the dependencies with pip install "hypothesis[cli,codemods]".
6.75.6 - 2023-05-27
This patch continues the work started in pull request #3651 <https://github.com/HypothesisWorks/hypothesis/pull/3651> by adding ruff <https://pypi.org/project/ruff/> linter rules for pyflakes <https://pypi.org/project/pyflakes/>, flake8-comprehensions <https://pypi.org/project/flake8-comprehensions/>, and flake8-implicit-str-concat <https://pypi.org/project/flake8-implicit-str-concat/>.
6.75.5 - 2023-05-26
This patch updates our linter stack to use ruff <https://pypi.org/project/ruff/>, and fixes some previously-ignored lints. Thanks to Christian Clauss for his careful review and pull request #3651 <https://github.com/HypothesisWorks/hypothesis/pull/3651>!
6.75.4 - 2023-05-26
Hypothesis will now record an event for more cases where data is marked invalid, including for exceeding the internal depth limit.
6.75.3 - 2023-05-14
This patch fixes complex_numbers() <#hypothesis.strategies.complex_numbers> accidentally invalidating itself when passed magnitude arguments for 32 and 64-bit widths, i.e. 16- and 32-bit floats, due to not internally down-casting numbers (issue #3573 <https://github.com/HypothesisWorks/hypothesis/issues/3573>).
6.75.2 - 2023-05-04
Improved the documentation regarding how to use GitHubArtifactDatabase <#hypothesis.database.GitHubArtifactDatabase> and fixed a bug that occurred in repositories with no existing artifacts.
Thanks to Agustín Covarrubias for this contribution.
6.75.1 - 2023-04-30
hypothesis.errors will now raise AttributeError when attempting to access an undefined attribute, rather than returning None.
6.75.0 - 2023-04-30
Sick of adding @example() <#hypothesis.example>s by hand? Our Pytest plugin now writes .patch files to insert them for you, making this workflow <https://blog.nelhage.com/post/property-testing-like-afl/> easier than ever before.
Note that you'll need LibCST <https://pypi.org/project/libcst/> (via hypothesis[codemods] <#codemods>), and that @example().via() <#hypothesis.example.via> requires PEP 614 <https://peps.python.org/pep-0614/> (Python 3.9 or later).
6.74.1 - 2023-04-28
This patch provides better error messages for datetime- and timedelta-related invalid dtypes in our Pandas extra (issue #3518 <https://github.com/HypothesisWorks/hypothesis/issues/3518>). Thanks to Nick Muoh at the PyCon Sprints!
6.74.0 - 2023-04-26
This release adds support for nullable pandas dtypes <https://pandas.pydata.org/docs/user_guide/integer_na.html> in pandas() <#module-hypothesis.extra.pandas> (issue #3604 <https://github.com/HypothesisWorks/hypothesis/issues/3604>). Thanks to Cheuk Ting Ho for implementing this at the PyCon sprints!
6.73.1 - 2023-04-27
This patch updates our minimum Numpy version to 1.16, and restores compatibility with versions before 1.20, which were broken by a mistake in Hypothesis 6.72.4 (issue #3625 <https://github.com/HypothesisWorks/hypothesis/issues/3625>).
6.73.0 - 2023-04-25
This release upgrades the Phase.explain <#hypothesis.Phase.explain> phase (issue #3411 <https://github.com/HypothesisWorks/hypothesis/issues/3411>).
- Following the first failure, Hypothesis will (usually, depending on the enabled Phase <#hypothesis.Phase>) track which lines of code were executed by passing and failing examples, and report where they diverged - with some heuristics to drop unhelpful reports. This is an existing feature, now upgraded and newly enabled by default.
After shrinking to a minimal failing example, Hypothesis will try to find parts of the example -- e.g. separate args to @given() <#hypothesis.given> -- which can vary freely without changing the result of that minimal failing example. If the automated experiments run without finding a passing variation, we leave a comment in the final report:
test_x_divided_by_y( x=0, # or any other generated value y=0, )
Just remember that the lack of an explanation sometimes just means that Hypothesis couldn't efficiently find one, not that no explanation (or simpler failing example) exists.
6.72.4 - 2023-04-25
This patch fixes type annotations for the arrays() <#hypothesis.extra.numpy.arrays> strategy. Thanks to Francesc Elies for pull request #3602 <https://github.com/HypothesisWorks/hypothesis/pull/3602>.
6.72.3 - 2023-04-25
This patch fixes a bug with from_type() <#hypothesis.strategies.from_type> with dict[tuple[int, int], str] (issue #3527 <https://github.com/HypothesisWorks/hypothesis/issues/3527>). Thanks to Nick Muoh at the PyCon Sprints!
6.72.2 - 2023-04-24
This patch refactors our internals to facilitate an upcoming feature.
6.72.1 - 2023-04-19
This patch fixes some documentation and prepares for future features.
6.72.0 - 2023-04-16
This release deprecates Healthcheck.all(), and adds a codemod <#codemods> to automatically replace it with list(Healthcheck) (issue #3596 <https://github.com/HypothesisWorks/hypothesis/issues/3596>).
6.71.0 - 2023-04-07
This release adds GitHubArtifactDatabase <#hypothesis.database.GitHubArtifactDatabase>, a new database backend that allows developers to access the examples found by a Github Actions CI job. This is particularly useful for workflows that involve continuous fuzzing, like HypoFuzz <https://hypofuzz.com/>.
Thanks to Agustín Covarrubias for this feature!
6.70.2 - 2023-04-03
This patch clarifies the reporting of time spent generating data. A simple arithmetic mean of the percentage of time spent can be misleading; reporting the actual time spent avoids misunderstandings.
Thanks to Andrea Reina for reporting and fixing issue #3598 <https://github.com/HypothesisWorks/hypothesis/issues/3598>!
6.70.1 - 2023-03-27
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.70.0 - 2023-03-16
This release adds an optional domains= parameter to the emails() <#hypothesis.strategies.emails> strategy, and excludes the special-use .arpa <https://en.wikipedia.org/wiki/.arpa> domain from the default strategy (issue #3567 <https://github.com/HypothesisWorks/hypothesis/issues/3567>).
Thanks to Jens Tröger for reporting and fixing this bug!
6.69.0 - 2023-03-15
This release turns HealthCheck.return_value and HealthCheck.not_a_test_method into unconditional errors. Passing them to suppress_health_check= is therefore a deprecated no-op. (issue #3568 <https://github.com/HypothesisWorks/hypothesis/issues/3568>). Thanks to Reagan Lee for the patch!
Separately, GraalPy can now run and pass most of the hypothesis test suite (issue #3587 <https://github.com/HypothesisWorks/hypothesis/issues/3587>).
6.68.3 - 2023-03-15
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.68.2 - 2023-02-17
This patch fixes missing imports of the re module, when ghostwriting <#ghostwriter> tests which include compiled patterns or regex flags. Thanks to Jens Heinrich for reporting and promptly fixing this bug!
6.68.1 - 2023-02-12
This patch adds some private hooks for use in research on Schemathesis <https://github.com/schemathesis/schemathesis> (see our preprint here <https://arxiv.org/abs/2112.10328>).
6.68.0 - 2023-02-09
This release adds support for the Array API's 2022.12 release <https://data-apis.org/array-api/2022.12/> via the api_version argument in make_strategies_namespace() <#hypothesis.extra.array_api.make_strategies_namespace>. Concretely this involves complex support in its existing strategies, plus an introduced xps.complex_dtypes() <#xps.complex_dtypes> strategy.
Additionally this release now treats hypothesis.extra.array_api <#array-api> as stable, meaning breaking changes should only happen with major releases of Hypothesis.
6.67.1 - 2023-02-05
This patch updates our autoformatting tools, improving our code style without any API changes.
6.67.0 - 2023-02-05
This release allows for more precise generation of complex numbers using from_dtype() <#hypothesis.extra.numpy.from_dtype>, by supporting the width, min_magnitude, and min_magnitude arguments (issue #3468 <https://github.com/HypothesisWorks/hypothesis/issues/3468>).
Thanks to Felix Divo for this feature!
6.66.2 - 2023-02-04
This patch fixes a rare RecursionError when pretty-printing a multi-line object without type-specific printer, which was passed to a function which returned the same object by .map() or builds() <#hypothesis.strategies.builds> and thus recursed due to the new pretty reprs in Hypothesis 6.65.0 - 2023-01-24 (issue #3560 <https://github.com/HypothesisWorks/hypothesis/issues/3560>). Apologies to all those affected.
6.66.1 - 2023-02-03
This makes from_dtype() <#hypothesis.extra.numpy.from_dtype> pass through the parameter allow_subnormal for complex dtypes.
6.66.0 - 2023-02-02
This release adds a width parameter to complex_numbers() <#hypothesis.strategies.complex_numbers>, analogously to floats() <#hypothesis.strategies.floats>.
Thanks to Felix Divo for the new feature!
6.65.2 - 2023-01-27
This patch fixes invalid annotations detected for the tests generated by Ghostwriter <#ghostwriter>. It will now correctly generate Optional types with just one type argument and handle union expressions inside of type arguments correctly. Additionally, it now supports code with the from __future__ import annotations marker for Python 3.10 and newer.
6.65.1 - 2023-01-26
This release improves the pretty-printing of enums in falsifying examples, so that they print as their full identifier rather than their repr.
6.65.0 - 2023-01-24
Hypothesis now reports some failing inputs by showing the call which constructed an object, rather than the repr of the object. This can be helpful when the default repr does not include all relevant details, and will unlock further improvements in a future version.
For now, we capture calls made via builds() <#hypothesis.strategies.builds>, and via .map() <#hypothesis.strategies.SearchStrategy.map>.
6.64.0 - 2023-01-23
The Ghostwriter <#ghostwriter> will now include type annotations on tests for type-annotated code. If you want to force this to happen (or not happen), pass a boolean to the new annotate= argument to the Python functions, or the --[no-]annotate CLI flag.
Thanks to Nicolas Ganz for this new feature!
6.63.0 - 2023-01-20
range_indexes() <#hypothesis.extra.pandas.range_indexes> now accepts a name= argument, to generate named pandas.RangeIndex objects.
Thanks to Sam Watts for this new feature!
6.62.1 - 2023-01-14
This patch tweaks xps.arrays() <#xps.arrays> internals to improve PyTorch compatibility. Specifically, torch.full() does not accept integers as the shape argument (n.b. technically "size" in torch), but such behaviour is expected in internal code, so we copy the torch module and patch in a working full() function.
6.62.0 - 2023-01-08
A classic error when testing is to write a test function that can never fail, even on inputs that aren't allowed or manually provided. By analogy to the design pattern of:
@pytest.mark.parametrize("arg", [
..., # passing examples
pytest.param(..., marks=[pytest.mark.xfail]) # expected-failing input
])we now support @example(...).xfail() <#hypothesis.example.xfail>, with the same (optional) condition, reason, and raises arguments as pytest.mark.xfail().
Naturally you can also write .via(...).xfail(...), or .xfail(...).via(...), if you wish to note the provenance of expected-failing examples.
6.61.3 - 2023-01-08
This patch teaches our enhanced get_type_hints() function to 'see through' partial application, allowing inference from type hints to work in a few more cases which aren't (yet!) supported by the standard-library version.
6.61.2 - 2023-01-07
This patch improves our pretty-printing of failing examples, including some refactoring to prepare for exciting future features.
6.61.1 - 2023-01-06
This patch brings our domains() <#hypothesis.provisional.domains> and emails() <#hypothesis.strategies.emails> strategies into compliance with RFC 5890 §2.3.1 <https://datatracker.ietf.org/doc/html/rfc5890.html>: we no longer generate parts-of-domains where the third and fourth characters are -- ("R-LDH labels"), though future versions may deliberately generate xn-- punycode labels. Thanks to python-email-validator <https://pypi.org/project/python-email-validator/> for the report <https://github.com/JoshData/python-email-validator/issues/92>!
6.61.0 - 2022-12-11
This release improves our treatment of database keys, which based on (among other things) the source code of your test function. We now post-process this source to ignore decorators, comments, trailing whitespace, and blank lines - so that you can add @example() <#hypothesis.example>s or make some small no-op edits to your code without preventing replay of any known failing or covering examples.
6.60.1 - 2022-12-11
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.60.0 - 2022-12-04
This release improves Hypothesis' ability to resolve forward references in type annotations. It fixes a bug that prevented builds() <#hypothesis.strategies.builds> from being used with pydantic models that possess updated forward references <https://pydantic-docs.helpmanual.io/usage/postponed_annotations/>. See issue #3519 <https://github.com/HypothesisWorks/hypothesis/issues/3519>.
6.59.0 - 2022-12-02
The @example(...) <#hypothesis.example> decorator now has a .via() method, which future tools will use to track automatically-added covering examples (issue #3506 <https://github.com/HypothesisWorks/hypothesis/issues/3506>).
6.58.2 - 2022-11-30
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.58.1 - 2022-11-26
This patch shifts hypothesis[lark] from depending on the old lark-parser <https://pypi.org/project/lark-parser/> package to the new lark <https://pypi.org/project/lark/> package. There are no code changes in Hypothesis, it's just that Lark got a new name on PyPI for version 1.0 onwards.
6.58.0 - 2022-11-19
register_random() <#hypothesis.register_random> has used weakref since 6.27.1 - 2021-11-22, allowing the Random-compatible objects to be garbage-collected when there are no other references remaining in order to avoid memory leaks. We now raise an error or emit a warning when this seems likely to happen immediately.
The type annotation of register_random() <#hypothesis.register_random> was also widened so that structural subtypes of Random are accepted by static typecheckers.
6.57.1 - 2022-11-14
This patch updates some internal type annotations and fixes a formatting bug in the explain <#hypothesis.Phase.explain> phase reporting.
6.57.0 - 2022-11-14
Hypothesis now raises an error if you passed a strategy as the alphabet= argument to text() <#hypothesis.strategies.text>, and it generated something which was not a length-one string. This has never been supported, we're just adding explicit validation to catch cases like this StackOverflow question <https://stackoverflow.com/a/74336909/9297601>.
6.56.4 - 2022-10-28
This patch updates some docs, and depends on exceptiongroup <https://pypi.org/project/exceptiongroup/> 1.0.0 final to avoid a bug in the previous version.
6.56.3 - 2022-10-17
This patch teaches text() <#hypothesis.strategies.text> to rewrite a few more filter predicates (issue #3134 <https://github.com/HypothesisWorks/hypothesis/issues/3134>). You're unlikely to notice any change.
6.56.2 - 2022-10-10
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy, and fixes some incorrect examples in the docs for mutually_broadcastable_shapes() <#hypothesis.extra.numpy.mutually_broadcastable_shapes>.
6.56.1 - 2022-10-05
This patch improves the error message when Hypothesis detects "flush to zero" mode for floating-point: we now report which package(s) enabled this, which can make debugging much easier. See issue #3458 <https://github.com/HypothesisWorks/hypothesis/issues/3458> for details.
6.56.0 - 2022-10-02
This release defines __bool__() on SearchStrategy <#hypothesis.strategies.SearchStrategy>. It always returns True, like before, but also emits a warning to help with cases where you intended to draw a value (issue #3463 <https://github.com/HypothesisWorks/hypothesis/issues/3463>).
6.55.0 - 2022-09-29
In preparation for future versions of the Array API standard <https://data-apis.org/array-api/latest/future_API_evolution.html>, make_strategies_namespace() <#hypothesis.extra.array_api.make_strategies_namespace> now accepts an optional api_version argument, which determines the version conformed to by the returned strategies namespace. If None, the version of the passed array module xp is inferred.
This release also introduces xps.real_dtypes() <#xps.real_dtypes>. This is currently equivalent to the existing xps.numeric_dtypes() <#xps.numeric_dtypes> strategy, but exists because the latter is expected to include complex numbers in the next version of the standard.
6.54.6 - 2022-09-18
If multiple explicit examples (from @example() <#hypothesis.example>) raise a Skip exception, for consistency with generated examples we now re-raise the first instead of collecting them into an ExceptionGroup (issue #3453 <https://github.com/HypothesisWorks/hypothesis/issues/3453>).
6.54.5 - 2022-09-05
This patch updates our autoformatting tools, improving our code style without any API changes.
6.54.4 - 2022-08-20
This patch fixes some type annotations for Python 3.9 and earlier (issue #3397 <https://github.com/HypothesisWorks/hypothesis/issues/3397>), and teaches the Phase.explain <#hypothesis.Phase.explain> phase about certain locations it should not bother reporting (issue #3439 <https://github.com/HypothesisWorks/hypothesis/issues/3439>).
6.54.3 - 2022-08-12
This patch teaches the Ghostwriter an additional check for function and class locations that should make it use public APIs more often.
6.54.2 - 2022-08-10
This patch fixes our workaround for a pytest bug where the inner exceptions in an ExceptionGroup are not displayed <https://github.com/pytest-dev/pytest/issues/9159> (issue #3430 <https://github.com/HypothesisWorks/hypothesis/issues/3430>).
6.54.1 - 2022-08-02
This patch makes FailedHealthCheck and DeadlineExceeded exceptions picklable, for compatibility with Django's parallel test runner (issue #3426 <https://github.com/HypothesisWorks/hypothesis/issues/3426>).
6.54.0 - 2022-08-02
Reporting of multiple failing examples <#hypothesis.settings.report_multiple_bugs> now uses the PEP 654 <https://peps.python.org/pep-0654/> ExceptionGroup <https://docs.python.org/3.11/library/exceptions.html#ExceptionGroup> type, which is provided by the exceptiongroup <https://pypi.org/project/exceptiongroup/> backport on Python 3.10 and earlier (issue #3175 <https://github.com/HypothesisWorks/hypothesis/issues/3175>). hypothesis.errors.MultipleFailures is therefore deprecated.
Failing examples and other reports are now stored as PEP 678 <https://peps.python.org/pep-0678/> exception notes, which ensures that they will always appear together with the traceback and other information about their respective error.
6.53.0 - 2022-07-25
from_field() <#hypothesis.extra.django.from_field> now supports UsernameField from django.contrib.auth.forms.
Thanks to Afonso Silva for reporting and working on issue #3417 <https://github.com/HypothesisWorks/hypothesis/issues/3417>.
6.52.4 - 2022-07-22
This patch improves the error message when you pass filenames to the hypothesis write CLI, which takes the name of a module or function (e.g. hypothesis write gzip or hypothesis write package.some_function rather than hypothesis write script.py).
Thanks to Ed Rogers for implementing this as part of the SciPy 2022 sprints!
6.52.3 - 2022-07-19
This patch ensures that the warning for non-interactive .example() points to your code instead of Hypothesis internals (issue #3403 <https://github.com/HypothesisWorks/hypothesis/issues/3403>).
Thanks to @jameslamb for this fix.
6.52.2 - 2022-07-19
This patch makes integers() <#hypothesis.strategies.integers> more likely to generate boundary values for large two-sided intervals (issue #2942 <https://github.com/HypothesisWorks/hypothesis/issues/2942>).
6.52.1 - 2022-07-18
This patch adds filter rewriting for math.isfinite(), math.isinf(), and math.isnan() on integers() <#hypothesis.strategies.integers> or floats() <#hypothesis.strategies.floats> (issue #2701 <https://github.com/HypothesisWorks/hypothesis/issues/2701>).
Thanks to Sam Clamons at the SciPy Sprints!
6.52.0 - 2022-07-18
This release adds the allow_subnormal argument to complex_numbers() <#hypothesis.strategies.complex_numbers> by applying it to each of the real and imaginary parts separately. Closes issue #3390 <https://github.com/HypothesisWorks/hypothesis/issues/3390>.
Thanks to Evan Tey for this fix.
6.51.0 - 2022-07-17
Issue a deprecation warning if a function decorated with @composite <#hypothesis.strategies.composite> does not draw any values (issue #3384 <https://github.com/HypothesisWorks/hypothesis/issues/3384>).
Thanks to Grzegorz Zieba, Rodrigo Girão, and Thomas Ball for working on this at the EuroPython sprints!
6.50.1 - 2022-07-09
This patch improves the error messages in @example() <#hypothesis.example> argument validation following the recent release of 6.49.1.
6.50.0 - 2022-07-09
This release allows from_dtype() <#hypothesis.extra.numpy.from_dtype> to generate Unicode strings which cannot be encoded in UTF-8, but are valid in Numpy arrays (which use UTF-32).
This logic will only be used with numpy <https://pypi.org/project/numpy/> >= 1.19, because earlier versions have an issue <https://github.com/numpy/numpy/issues/15363> which led us to revert Hypothesis 5.2 last time!
6.49.1 - 2022-07-05
This patch fixes some inconsistency between argument handling for @example <#hypothesis.example> and @given <#hypothesis.given> (2706 <https://github.com/HypothesisWorks/hypothesis/issues/2706#issuecomment-1168363177>).
6.49.0 - 2022-07-04
This release uses PEP 612 <https://peps.python.org/pep-0612/> python:typing.ParamSpec (or the typing-extensions <https://pypi.org/project/typing-extensions/> backport) to express the first-argument-removing behaviour of @st.composite <#hypothesis.strategies.composite> and signature-preservation of functions() <#hypothesis.strategies.functions> to IDEs, editor plugins, and static type checkers such as mypy <https://pypi.org/project/mypy/>.
6.48.3 - 2022-07-03
hypothesis.event() <#hypothesis.event> now works for hashable objects which do not support weakrefs, such as integers and tuples.
6.48.2 - 2022-06-29
This patch tidies up some internal introspection logic, which will improve support for positional-only arguments in a future release (issue #2706 <https://github.com/HypothesisWorks/hypothesis/issues/2706>).
6.48.1 - 2022-06-27
This release automatically rewrites some simple filters, such as floats().filter(lambda x: x >= 10) to the more efficient floats(min_value=10), based on the AST of the predicate.
We continue to recommend using the efficient form directly wherever possible, but this should be useful for e.g. pandera <https://pypi.org/project/pandera/> "Checks" where you already have a simple predicate and translating manually is really annoying. See issue #2701 <https://github.com/HypothesisWorks/hypothesis/issues/2701> for details.
6.48.0 - 2022-06-27
This release raises SkipTest for tests which never executed any examples, for example because the phases <#hypothesis.settings.phases> setting excluded the explicit <#hypothesis.Phase.explicit>, reuse <#hypothesis.Phase.reuse>, and generate <#hypothesis.Phase.generate> phases. This helps to avoid cases where broken tests appear to pass, because they didn't actually execute (issue #3328 <https://github.com/HypothesisWorks/hypothesis/issues/3328>).
6.47.5 - 2022-06-25
This patch fixes type annotations that had caused the signature of @given <#hypothesis.given> to be partially-unknown to type-checkers for Python versions before 3.10.
6.47.4 - 2022-06-23
This patch fixes from_type() <#hypothesis.strategies.from_type> on Python 3.11, following python/cpython#93754 <https://github.com/python/cpython/pull/93754/>.
6.47.3 - 2022-06-15
This patch makes the too_slow <#hypothesis.HealthCheck.too_slow> health check more consistent with long deadline <#hypothesis.settings.deadline> tests (issue #3367 <https://github.com/HypothesisWorks/hypothesis/issues/3367>) and fixes an install issue under pipenv <https://pypi.org/project/pipenv/> which was introduced in Hypothesis 6.47.2 (issue #3374 <https://github.com/HypothesisWorks/hypothesis/issues/3374>).
6.47.2 - 2022-06-12
We now use the PEP 654 <https://peps.python.org/pep-0654/> ExceptionGroup <https://docs.python.org/3.11/library/exceptions.html#ExceptionGroup> type - provided by the exceptiongroup <https://pypi.org/project/exceptiongroup/> backport on older Pythons - to ensure that if multiple errors are raised in teardown, they will all propagate.
6.47.1 - 2022-06-10
Our pretty-printer no longer sorts dictionary keys, since iteration order is stable in Python 3.7+ and this can affect reproducing examples (issue #3370 <https://github.com/HypothesisWorks/hypothesis/issues/3370>). This PR was kindly supported by Ordina Pythoneers <https://www.ordina.nl/vakgebieden/python/>.
6.47.0 - 2022-06-07
The Ghostwriter <#ghostwriter> can now write tests for @classmethod or @staticmethod methods, in addition to the existing support for functions and other callables (issue #3318 <https://github.com/HypothesisWorks/hypothesis/issues/3318>). Thanks to Cheuk Ting Ho for the patch.
6.46.11 - 2022-06-02
Mention hypothesis.strategies.timezones() <#hypothesis.strategies.timezones> in the documentation of hypothesis.strategies.datetimes() <#hypothesis.strategies.datetimes> for completeness.
Thanks to George Macon for this addition.
6.46.10 - 2022-06-01
This release contains some small improvements to our documentation. Thanks to Felix Divo for his contribution!
6.46.9 - 2022-05-25
This patch by Adrian Garcia Badaracco adds type annotations to some private internals (issue #3074 <https://github.com/HypothesisWorks/hypothesis/issues/3074>).
6.46.8 - 2022-05-25
This patch by Phillip Schanely makes changes to the floats() <#hypothesis.strategies.floats> strategy when min_value or max_value is present. Hypothesis will now be capable of generating every representable value in the bounds. You may notice that hypothesis is more likely to test values near boundaries, and values that are very close to zero.
These changes also support future integrations with symbolic execution tools and fuzzers (issue #3086 <https://github.com/HypothesisWorks/hypothesis/issues/3086>).
6.46.7 - 2022-05-19
This patch updates the type annotations for tuples() <#hypothesis.strategies.tuples> and one_of() <#hypothesis.strategies.one_of> so that type-checkers require its arguments to be positional-only, and so that it no longer fails under pyright-strict mode (see issue #3348 <https://github.com/HypothesisWorks/hypothesis/issues/3348>). Additional changes are made to Hypothesis' internals improve pyright scans.
6.46.6 - 2022-05-18
This patch by Cheuk Ting Ho adds support for PEP 655 <https://peps.python.org/pep-0655/> Required and NotRequired as attributes of TypedDict in from_type() <#hypothesis.strategies.from_type> (issue #3339 <https://github.com/HypothesisWorks/hypothesis/issues/3339>).
6.46.5 - 2022-05-15
This patch fixes from_dtype() <#hypothesis.extra.numpy.from_dtype> with long-precision floating-point datatypes (typecode g; see numpy:numpy.typename()).
6.46.4 - 2022-05-15
This patch improves some error messages for custom signatures containing invalid parameter names (issue #3317 <https://github.com/HypothesisWorks/hypothesis/issues/3317>).
6.46.3 - 2022-05-11
This patch by Cheuk Ting Ho makes it an explicit error to call from_type() <#hypothesis.strategies.from_type> or register_type_strategy() <#hypothesis.strategies.register_type_strategy> with types that have no runtime instances (issue #3280 <https://github.com/HypothesisWorks/hypothesis/issues/3280>).
6.46.2 - 2022-05-03
This patch fixes silently dropping examples when the @example <#hypothesis.example> decorator is applied to itself (issue #3319 <https://github.com/HypothesisWorks/hypothesis/issues/3319>). This was always a weird pattern, but now it works. Thanks to Ray Sogata, Keeri Tramm, and Kevin Khuong for working on this patch!
6.46.1 - 2022-05-01
This patch fixes a rare bug where we could incorrectly treat empty as a type annotation, if the callable had an explicitly assigned __signature__.
6.46.0 - 2022-05-01
This release adds an allow_nil argument to uuids() <#hypothesis.strategies.uuids>, which you can use to... generate the nil UUID. Thanks to Shlok Gandhi for the patch!
6.45.4 - 2022-05-01
This patch fixes some missing imports for certain Ghostwritten <#ghostwriter> tests. Thanks to Mel Seto for fixing issue #3316 <https://github.com/HypothesisWorks/hypothesis/issues/3316>.
6.45.3 - 2022-04-30
This patch teaches the Ghostwriter <#ghostwriter> to recognize many more common argument names (issue #3311 <https://github.com/HypothesisWorks/hypothesis/issues/3311>).
6.45.2 - 2022-04-29
This patch fixes issue #3314 <https://github.com/HypothesisWorks/hypothesis/issues/3314>, where Hypothesis would raise an internal error from domains() <#hypothesis.provisional.domains> or (only on Windows) from timezones() <#hypothesis.strategies.timezones> in some rare circumstances where the installation was subtly broken.
Thanks to Munir Abdinur for this contribution.
6.45.1 - 2022-04-27
This release fixes deprecation warnings about sre_compile and sre_parse imports and importlib.resources usage when running Hypothesis on Python 3.11.
Thanks to Florian Bruhin for this contribution.
6.45.0 - 2022-04-22
This release updates xps.indices() <#xps.indices> by introducing an allow_newaxis argument, defaulting to False. If allow_newaxis=True, indices can be generated that add dimensions to arrays, which is achieved by the indexer containing None. This change is to support a specification change that expand dimensions via indexing (data-apis/array-api#408 <https://github.com/data-apis/array-api/pull/408>).
6.44.0 - 2022-04-21
This release adds a names argument to indexes() <#hypothesis.extra.pandas.indexes> and series() <#hypothesis.extra.pandas.series>, so that you can create Pandas objects with specific or varied names.
Contributed by Sam Watts.
6.43.3 - 2022-04-18
This patch updates the type annotations for @given <#hypothesis.given> so that type-checkers will warn on mixed positional and keyword arguments, as well as fixing issue #3296 <https://github.com/HypothesisWorks/hypothesis/issues/3296>.
6.43.2 - 2022-04-16
Fixed a type annotation for pyright --strict (issue #3287 <https://github.com/HypothesisWorks/hypothesis/issues/3287>).
6.43.1 - 2022-04-13
This patch makes it an explicit error to call register_type_strategy() <#hypothesis.strategies.register_type_strategy> with a Pydantic GenericModel <https://docs.pydantic.dev/latest/concepts/models/#generic-models> and a callable, because GenericModel isn't actually a generic type at runtime and so you have to register each of the "parametrized versions" (actually subclasses!) manually. See issue #2940 <https://github.com/HypothesisWorks/hypothesis/issues/2940> for more details.
6.43.0 - 2022-04-12
This release makes it an explicit error to apply @pytest.fixture to a function which has already been decorated with @given() <#hypothesis.given>. Previously, pytest would convert your test to a fixture, and then never run it.
6.42.3 - 2022-04-10
This patch fixes from_type() <#hypothesis.strategies.from_type> on a TypedDict with complex annotations, defined in a file using from __future__ import annotations. Thanks to Katelyn Gigante for identifying and fixing this bug!
6.42.2 - 2022-04-10
The Hypothesis pytest plugin was not outputting valid xunit2 nodes when --junit-xml was specified. This has been broken since Pytest 5.4, which changed the internal API for adding nodes to the junit report.
This also fixes the issue when using hypothesis with --junit-xml and pytest-xdist where the junit xml report would not be xunit2 compatible. Now, when using with pytest-xdist, the junit report will just omit the <properties> node.
For more details, see this pytest issue <https://github.com/pytest-dev/pytest/issues/1126#issuecomment-484581283>, this pytest issue <https://github.com/pytest-dev/pytest/issues/7767#issuecomment-1082436256>, and issue #1935 <https://github.com/HypothesisWorks/hypothesis/issues/1935>.
Thanks to Brandon Chinn for this bug fix!
6.42.1 - 2022-04-10
This patch fixes pretty-printing of regular expressions in Python 3.11.0a7, and updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>,.
6.42.0 - 2022-04-09
This release makes st.functions(pure=True) <#hypothesis.strategies.functions> less noisy (issue #3253 <https://github.com/HypothesisWorks/hypothesis/issues/3253>), and generally improves pretty-printing of functions.
6.41.0 - 2022-04-01
This release changes the implementation of infer <#hypothesis.infer> to be an alias for python:Ellipsis. E.g. @given(a=infer) is now equivalent to @given(a=...). Furthermore, @given(...) can now be specified so that @given <#hypothesis.given> will infer the strategies for all arguments of the decorated function based on its annotations.
6.40.3 - 2022-04-01
This patch simplifies the repr of the strategies namespace returned in make_strategies_namespace() <#hypothesis.extra.array_api.make_strategies_namespace>, e.g.
>>> from hypothesis.extra.array_api import make_strategies_namespace >>> from numpy import array_api as xp >>> xps = make_strategies_namespace(xp) >>> xps make_strategies_namespace(numpy.array_api)
6.40.2 - 2022-04-01
Fixed from_type() <#hypothesis.strategies.from_type> support for PEP 604 <https://peps.python.org/pep-0604/> union types, like int | None (issue #3255 <https://github.com/HypothesisWorks/hypothesis/issues/3255>).
6.40.1 - 2022-04-01
Fixed an internal error when given() <#hypothesis.given> was passed a lambda.
6.40.0 - 2022-03-29
The Ghostwriter <#ghostwriter> can now write tests which check that two or more functions are equivalent on valid inputs, or raise the same type of exception for invalid inputs (issue #3267 <https://github.com/HypothesisWorks/hypothesis/issues/3267>).
6.39.6 - 2022-03-27
This patch makes some quality-of-life improvements to the Ghostwriter <#ghostwriter>: we guess the text() <#hypothesis.strategies.text> strategy for arguments named text (...obvious in hindsight, eh?); and improved the error message if you accidentally left in a nothing() <#hypothesis.strategies.nothing> or broke your rich <https://pypi.org/project/rich/> install.
6.39.5 - 2022-03-26
This patch improves our error detection and message when Hypothesis is run on a Python implementation without support for -0.0, which is required for the floats() <#hypothesis.strategies.floats> strategy but can be disabled by unsafe compiler options <https://simonbyrne.github.io/notes/fastmath/> (issue #3265 <https://github.com/HypothesisWorks/hypothesis/issues/3265>).
6.39.4 - 2022-03-17
This patch tweaks some internal formatting. There is no user-visible change.
6.39.3 - 2022-03-07
If the shrink <#hypothesis.Phase.shrink> phase is disabled, we now stop the generate <#hypothesis.Phase.generate> phase as soon as an error is found regardless of the value of the report_multiple_examples setting, since that's probably what you wanted (issue #3244 <https://github.com/HypothesisWorks/hypothesis/issues/3244>).
6.39.2 - 2022-03-07
This patch clarifies rare error messages in builds() <#hypothesis.strategies.builds> (issue #3225 <https://github.com/HypothesisWorks/hypothesis/issues/3225>) and floats() <#hypothesis.strategies.floats> (issue #3207 <https://github.com/HypothesisWorks/hypothesis/issues/3207>).
6.39.1 - 2022-03-03
This patch fixes a regression where the bound inner function (your_test.hypothesis.inner_test) would be invoked with positional arguments rather than passing them by name, which broke pytest-asyncio <https://pypi.org/project/pytest-asyncio/> (issue #3245 <https://github.com/HypothesisWorks/hypothesis/issues/3245>).
6.39.0 - 2022-03-01
This release improves Hypothesis' handling of positional-only arguments, which are now allowed @st.composite <#hypothesis.strategies.composite> strategies.
On Python 3.8 and later, the first arguments to builds() <#hypothesis.strategies.builds> and from_model() <#hypothesis.extra.django.from_model> are now natively positional-only. In cases which were already errors, the TypeError from incorrect usage will therefore be raises immediately when the function is called, rather than when the strategy object is used.
6.38.0 - 2022-02-26
This release makes floats() <#hypothesis.strategies.floats> error consistently when your floating-point hardware has been configured to violate IEEE-754 for subnormal numbers <https://en.wikipedia.org/wiki/Subnormal_number>, instead of only when an internal assertion was tripped (issue #3092 <https://github.com/HypothesisWorks/hypothesis/issues/3092>).
If this happens to you, passing allow_subnormal=False will suppress the explicit error. However, we strongly recommend fixing the root cause by disabling global-effect unsafe-math compiler options instead, or at least consulting e.g. Simon Byrne's Beware of fast-math <https://simonbyrne.github.io/notes/fastmath/> explainer first.
6.37.2 - 2022-02-21
This patch fixes a bug in stateful testing, where returning a single value wrapped in multiple() <#hypothesis.stateful.multiple> would be printed such that the assigned variable was a tuple rather than the single element (issue #3236 <https://github.com/HypothesisWorks/hypothesis/issues/3236>).
6.37.1 - 2022-02-21
This patch fixes a warning under pytest <https://pypi.org/project/pytest/> 7 relating to our rich traceback display logic (issue #3223 <https://github.com/HypothesisWorks/hypothesis/issues/3223>).
6.37.0 - 2022-02-18
When distinguishing multiple errors, Hypothesis now looks at the inner exceptions of PEP 654 <https://peps.python.org/pep-0654/> ExceptionGroups.
6.36.2 - 2022-02-13
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.36.1 - 2022-01-31
This patch fixes some deprecation warnings from pytest <https://pypi.org/project/pytest/> 7.0, along with some code formatting and docs updates.
6.36.0 - 2022-01-19
This release disallows using python:typing.Final with from_type() <#hypothesis.strategies.from_type> and register_type_strategy() <#hypothesis.strategies.register_type_strategy>.
Why? Because Final can only be used during class definition. We don't generate class attributes.
It also does not make sense as a runtime type on its own.
6.35.1 - 2022-01-17
This patch fixes hypothesis write output highlighting with rich <https://pypi.org/project/rich/> version 12.0 and later.
6.35.0 - 2022-01-08
This release disallows using python:typing.ClassVar with from_type() <#hypothesis.strategies.from_type> and register_type_strategy() <#hypothesis.strategies.register_type_strategy>.
Why? Because ClassVar can only be used during class definition. We don't generate class attributes.
It also does not make sense as a runtime type on its own.
6.34.2 - 2022-01-05
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.34.1 - 2021-12-31
This patch fixes issue #3169 <https://github.com/HypothesisWorks/hypothesis/issues/3169>, an extremely rare bug which would trigger if an internal least-recently-reused cache dropped a newly added entry immediately after it was added.
6.34.0 - 2021-12-31
This release fixes issue #3133 <https://github.com/HypothesisWorks/hypothesis/issues/3133> and issue #3144 <https://github.com/HypothesisWorks/hypothesis/issues/3144>, where attempting to generate Pandas series of lists or sets would fail with confusing errors if you did not specify dtype=object.
6.33.0 - 2021-12-30
This release disallows using python:typing.TypeAlias with from_type() <#hypothesis.strategies.from_type> and register_type_strategy() <#hypothesis.strategies.register_type_strategy>.
Why? Because TypeAlias is not really a type, it is a tag for type checkers that some expression is a type alias, not something else.
It does not make sense for Hypothesis to resolve it as a strategy. References issue #2978 <https://github.com/HypothesisWorks/hypothesis/issues/2978>.
6.32.1 - 2021-12-23
This patch updates our autoformatting tools, improving our code style without any API changes.
6.32.0 - 2021-12-23
This release drops support for Python 3.6, which reached end of life upstream <https://devguide.python.org/#status-of-python-branches> on 2021-12-23.
6.31.6 - 2021-12-15
This patch adds a temporary hook for a downstream tool, which is not part of the public API.
6.31.5 - 2021-12-14
This release updates our copyright headers to use a general authorship statement and omit the year <https://www.linuxfoundation.org/blog/copyright-notices-in-open-source-software-projects/>.
6.31.4 - 2021-12-11
This patch makes the .example() method more representative of test-time data generation, albeit often at a substantial cost to readability (issue #3182 <https://github.com/HypothesisWorks/hypothesis/issues/3182>).
6.31.3 - 2021-12-10
This patch improves annotations on some of Hypothesis' internal functions, in order to deobfuscate the signatures of some strategies. In particular, strategies shared between hypothesis.extra.numpy <#hypothesis-numpy> and the hypothesis.extra.array_api extra <#array-api> will benefit from this patch.
6.31.2 - 2021-12-10
This patch fix invariants display in stateful falsifying examples (issue #3185 <https://github.com/HypothesisWorks/hypothesis/issues/3185>).
6.31.1 - 2021-12-10
This patch updates xps.indices() <#xps.indices> so no flat indices are generated, i.e. generated indices will now always explicitly cover each axes of an array if no ellipsis is present. This is to be consistent with a specification change that dropped support for flat indexing (#272 <https://github.com/data-apis/array-api/pull/272>).
6.31.0 - 2021-12-09
This release makes us compatible with Django <https://pypi.org/project/Django/> 4.0, in particular by adding support for use of zoneinfo timezones (though we respect the new USE_DEPRECATED_PYTZ setting if you need it).
6.30.1 - 2021-12-05
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.30.0 - 2021-12-03
This release adds an allow_subnormal argument to the floats() <#hypothesis.strategies.floats> strategy, which can explicitly toggle the generation of subnormal floats <https://en.wikipedia.org/wiki/Subnormal_number> (issue #3155 <https://github.com/HypothesisWorks/hypothesis/issues/3155>). Disabling such generation is useful when testing flush-to-zero builds of libraries.
nps.from_dtype() <#hypothesis.extra.numpy.from_dtype> and xps.from_dtype() <#xps.from_dtype> can also accept the allow_subnormal argument, and xps.from_dtype() <#xps.from_dtype> or xps.arrays() <#xps.arrays> will disable subnormals by default if the array module xp is detected to flush-to-zero (like is typical with CuPy).
6.29.3 - 2021-12-02
This patch fixes a bug in mutually_broadcastable_shapes() <#hypothesis.extra.numpy.mutually_broadcastable_shapes>, which restricted the patterns of singleton dimensions that could be generated for dimensions that extended beyond base_shape (issue #3170 <https://github.com/HypothesisWorks/hypothesis/issues/3170>).
6.29.2 - 2021-12-02
This patch clarifies our pretty-printing of DataFrames (issue #3114 <https://github.com/HypothesisWorks/hypothesis/issues/3114>).
6.29.1 - 2021-12-02
This patch documents timezones() <#hypothesis.strategies.timezones> Windows-only requirement <https://docs.python.org/3/library/zoneinfo.html#data-sources> for the tzdata <https://pypi.org/project/tzdata/> package, and ensures that pip install hypothesis[zoneinfo] will install the latest version.
6.29.0 - 2021-11-29
This release teaches builds() <#hypothesis.strategies.builds> to use deferred() <#hypothesis.strategies.deferred> when resolving unrecognised type hints, so that you can conveniently register strategies for recursive types with constraints on some arguments (issue #3026 <https://github.com/HypothesisWorks/hypothesis/issues/3026>):
class RecursiveClass:
def __init__(self, value: int, next_node: typing.Optional["SomeClass"]):
assert value > 0
self.value = value
self.next_node = next_node
st.register_type_strategy(
RecursiveClass, st.builds(RecursiveClass, value=st.integers(min_value=1))
)6.28.1 - 2021-11-28
This release fixes some internal calculations related to collection sizes (issue #3143 <https://github.com/HypothesisWorks/hypothesis/issues/3143>).
6.28.0 - 2021-11-28
This release modifies our pytest <https://pypi.org/project/pytest/> plugin, to avoid importing Hypothesis and therefore triggering Hypothesis' entry points <#entry-points> for test suites where Hypothesis is installed but not actually used (issue #3140 <https://github.com/HypothesisWorks/hypothesis/issues/3140>).
6.27.3 - 2021-11-28
This release fixes issue #3080 <https://github.com/HypothesisWorks/hypothesis/issues/3080>, where from_type() <#hypothesis.strategies.from_type> failed on unions containing PEP 585 <https://peps.python.org/pep-0585/> builtin generic types (like list[int]) in Python 3.9 and later.
6.27.2 - 2021-11-26
This patch makes the hypothesis codemod command <#hypothesis-cli> somewhat faster.
6.27.1 - 2021-11-22
This patch changes the backing datastructures of register_random() <#hypothesis.register_random> and a few internal caches to use weakref.WeakValueDictionary. This reduces memory usage and may improve performance when registered Random instances are only used for a subset of your tests (issue #3131 <https://github.com/HypothesisWorks/hypothesis/issues/3131>).
6.27.0 - 2021-11-22
This release teaches Hypothesis' multiple-error reporting to format tracebacks using pytest <https://pypi.org/project/pytest/> or better-exceptions <https://pypi.org/project/better-exceptions/>, if they are installed and enabled (issue #3116 <https://github.com/HypothesisWorks/hypothesis/issues/3116>).
6.26.0 - 2021-11-21
Did you know that of the 264 possible floating-point numbers, 253 of them are nan - and Python prints them all the same way?
While nans usually have all zeros in the sign bit and mantissa, this isn't always true <https://wingolog.org/archives/2011/05/18/value-representation-in-javascript-implementations>, and 'signaling' nans might trap or error <https://en.wikipedia.org/wiki/NaN#Signaling_NaN>. To help distinguish such errors in e.g. CI logs, Hypothesis now prints -nan for negative nans, and adds a comment like # Saw 3 signaling NaNs if applicable.
6.25.0 - 2021-11-19
This release adds special filtering logic to make a few special cases like s.map(lambda x: x) and lists().filter(len) more efficient (issue #2701 <https://github.com/HypothesisWorks/hypothesis/issues/2701>).
6.24.6 - 2021-11-18
This patch makes floats() <#hypothesis.strategies.floats> generate "subnormal" floating point numbers <https://en.wikipedia.org/wiki/Subnormal_number> more often, as these rare values can have strange interactions with unsafe compiler optimisations like -ffast-math <https://simonbyrne.github.io/notes/fastmath/#flushing_subnormals_to_zero> (issue #2976 <https://github.com/HypothesisWorks/hypothesis/issues/2976>).
6.24.5 - 2021-11-16
This patch fixes a rare internal error in the datetimes() <#hypothesis.strategies.datetimes> strategy, where the implementation of allow_imaginary=False crashed when checking a time during the skipped hour of a DST transition if the DST offset is negative - only true of Europe/Dublin, who we presume have their reasons - and the tzinfo object is a pytz <https://pypi.org/project/pytz/> timezone (which predates PEP 495 <https://peps.python.org/pep-0495/>).
6.24.4 - 2021-11-15
This patch gives Hypothesis it's own internal Random instance, ensuring that test suites which reset the global random state don't induce weird correlations between property-based tests (issue #2135 <https://github.com/HypothesisWorks/hypothesis/issues/2135>).
6.24.3 - 2021-11-13
This patch updates documentation of note() <#hypothesis.note> (issue #3147 <https://github.com/HypothesisWorks/hypothesis/issues/3147>).
6.24.2 - 2021-11-05
This patch updates internal testing for the Array API extra <#array-api> to be consistent with new specification changes: sum() not accepting boolean arrays (#234 <https://github.com/data-apis/array-api/pull/234>), unique() split into separate functions (#275 <https://github.com/data-apis/array-api/pull/275>), and treating NaNs as distinct (#310 <https://github.com/data-apis/array-api/pull/310>). It has no user visible impact.
6.24.1 - 2021-11-01
This patch updates our vendored list of top-level domains <https://www.iana.org/domains/root/db>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
6.24.0 - 2021-10-23
This patch updates our vendored list of top-level domains <https://data.iana.org/TLD/tlds-alpha-by-domain.txt>, which is used by the provisional domains() <#hypothesis.provisional.domains> strategy.
(did you know that gTLDs can be both added <https://newgtlds.icann.org/en/> and removed <https://www.icann.org/resources/pages/gtld-registry-agreement-termination-2015-10-09-en>?)
6.23.4 - 2021-10-20
This patch adds an error for when shapes in xps.arrays() <#xps.arrays> is not passed as either a valid shape or strategy.
6.23.3 - 2021-10-18
This patch updates our formatting with shed <https://pypi.org/project/shed/>.
6.23.2 - 2021-10-08
This patch replaces external links to NumPy API docs with sphinx.ext.intersphinx cross-references. It is purely a documentation improvement.
6.23.1 - 2021-09-29
This patch cleans up internal logic for xps.arrays() <#xps.arrays>. There is no user-visible change.
6.23.0 - 2021-09-26
This release follows pytest <https://pypi.org/project/pytest/> in considering SystemExit and GeneratorExit exceptions to be test failures, meaning that we will shink to minimal examples and check for flakiness even though they subclass BaseException directly (issue #2223 <https://github.com/HypothesisWorks/hypothesis/issues/2223>).
KeyboardInterrupt continues to interrupt everything, and will be re-raised immediately.
6.22.0 - 2021-09-24
This release adds LiveServerTestCase <#hypothesis.extra.django.LiveServerTestCase> and StaticLiveServerTestCase <#hypothesis.extra.django.StaticLiveServerTestCase> for django test. Thanks to Ivan Tham for this feature!
6.21.6 - 2021-09-19
This patch fixes some new linter warnings such as flake8-bugbear <https://pypi.org/project/flake8-bugbear/>'s B904 for explicit exception chaining, so tracebacks might be a bit nicer.
6.21.5 - 2021-09-16
This release fixes None being inferred as the float64 dtype in from_dtype() <#xps.from_dtype> and arrays() <#xps.arrays> from the Array API extra <#array-api>.
6.21.4 - 2021-09-16
This release fixes the type hint for the @given() <#hypothesis.given> decorator when decorating an async function (issue #3099 <https://github.com/HypothesisWorks/hypothesis/issues/3099>).
6.21.3 - 2021-09-15
This release improves Ghostwritten tests for builtins (issue #2977 <https://github.com/HypothesisWorks/hypothesis/issues/2977>).
6.21.2 - 2021-09-15
This release deprecates use of both min_dims > len(shape) and max_dims > len(shape) when allow_newaxis == False in basic_indices() <#hypothesis.extra.numpy.basic_indices> (issue #3091 <https://github.com/HypothesisWorks/hypothesis/issues/3091>).
6.21.1 - 2021-09-13
This release improves the behaviour of builds() <#hypothesis.strategies.builds> and from_type() <#hypothesis.strategies.from_type> in certain situations involving decorators (issue #2495 <https://github.com/HypothesisWorks/hypothesis/issues/2495> and issue #3029 <https://github.com/HypothesisWorks/hypothesis/issues/3029>).
6.21.0 - 2021-09-11
This release introduces strategies for array/tensor libraries adopting the Array API standard <https://data-apis.org/> (issue #3037 <https://github.com/HypothesisWorks/hypothesis/issues/3037>). They are available in the hypothesis.extra.array_api extra <#array-api>, and work much like the existing strategies for NumPy <#hypothesis-numpy>.
6.20.1 - 2021-09-10
This patch fixes issue #961 <https://github.com/HypothesisWorks/hypothesis/issues/961>, where calling given() inline on a bound method would fail to handle the self argument correctly.
6.20.0 - 2021-09-09
This release allows slices() <#hypothesis.strategies.slices> to generate step=None, and fixes an off-by-one error where the start index could be equal to size. This works fine for all Python sequences and Numpy arrays, but is undefined behaviour in the Array API standard <https://data-apis.org/> (see pull request #3065 <https://github.com/HypothesisWorks/hypothesis/pull/3065>).
6.19.0 - 2021-09-08
This release makes stateful testing <#stateful> more likely to tell you if you do something unexpected and unsupported:
- The return_value <#hypothesis.HealthCheck.return_value> health check now applies to rule() <#hypothesis.stateful.rule> and initialize() <#hypothesis.stateful.initialize> rules, if they don't have target bundles, as well as invariant() <#hypothesis.stateful.invariant>.
- Using a consumes() <#hypothesis.stateful.consumes> bundle as a target is deprecated, and will be an error in a future version.
If existing code triggers these new checks, check for related bugs and misunderstandings - these patterns never had any effect.
6.18.0 - 2021-09-06
This release teaches from_type() <#hypothesis.strategies.from_type> a neat trick: when resolving an python:typing.Annotated type, if one of the annotations is a strategy object we use that as the inferred strategy. For example:
PositiveInt = Annotated[int, st.integers(min_value=1)]
If there are multiple strategies, we use the last outer-most annotation. See issue #2978 <https://github.com/HypothesisWorks/hypothesis/issues/2978> and pull request #3082 <https://github.com/HypothesisWorks/hypothesis/pull/3082> for discussion.
Requires Python 3.9 or later for get_type_hints(..., include_extras=False).
6.17.4 - 2021-08-31
This patch makes unique arrays() <#hypothesis.extra.numpy.arrays> much more efficient, especially when there are only a few valid elements - such as for eight-bit integers (issue #3066 <https://github.com/HypothesisWorks/hypothesis/issues/3066>).
6.17.3 - 2021-08-30
This patch fixes the repr of array_shapes() <#hypothesis.extra.numpy.array_shapes>.
6.17.2 - 2021-08-30
This patch wraps some internal helper code in our proxies decorator to prevent mutations of method docstrings carrying over to other instances of the respective methods.
6.17.1 - 2021-08-29
This patch moves some internal helper code in preparation for issue #3065 <https://github.com/HypothesisWorks/hypothesis/issues/3065>. There is no user-visible change, unless you depended on undocumented internals.
6.17.0 - 2021-08-27
This release adds type annotations to the stateful testing <#stateful> API.
Thanks to Ruben Opdebeeck for this contribution!
6.16.0 - 2021-08-27
This release adds the DrawFn <#hypothesis.strategies.DrawFn> type as a reusable type hint for the draw argument of @composite <#hypothesis.strategies.composite> functions.
Thanks to Ruben Opdebeeck for this contribution!
6.15.0 - 2021-08-22
This release emits a more useful error message when @given() <#hypothesis.given> is applied to a coroutine function, i.e. one defined using async def (issue #3054 <https://github.com/HypothesisWorks/hypothesis/issues/3054>).
This was previously only handled by the generic return_value <#hypothesis.HealthCheck.return_value> health check, which doesn't direct you to use either a custom executor <#custom-function-execution> or a library such as pytest-trio <https://pypi.org/project/pytest-trio/> or pytest-asyncio <https://pypi.org/project/pytest-asyncio/> to handle it for you.
6.14.9 - 2021-08-20
This patch fixes a regression in Hypothesis 6.14.8, where from_type() <#hypothesis.strategies.from_type> failed to resolve types which inherit from multiple parametrised generic types, affecting the returns <https://pypi.org/project/returns/> package (issue #3060 <https://github.com/HypothesisWorks/hypothesis/issues/3060>).
6.14.8 - 2021-08-16
This patch ensures that registering a strategy for a subclass of a parametrised generic type such as class Lines(Sequence[str]): will not "leak" into unrelated strategies such as st.from_type(Sequence[int]) (issue #2951 <https://github.com/HypothesisWorks/hypothesis/issues/2951>). Unfortunately this fix requires PEP 560 <https://peps.python.org/pep-0560/>, meaning Python 3.7 or later.
6.14.7 - 2021-08-14
This patch fixes issue #3050 <https://github.com/HypothesisWorks/hypothesis/issues/3050>, where attrs <https://pypi.org/project/attrs/> classes could cause an internal error in the ghostwriter <#ghostwriter>.
6.14.6 - 2021-08-07
This patch improves the error message for issue #3016 <https://github.com/HypothesisWorks/hypothesis/issues/3016>, where PEP 585 <https://peps.python.org/pep-0585/> builtin generics with self-referential forward-reference strings cannot be resolved to a strategy by from_type() <#hypothesis.strategies.from_type>.
6.14.5 - 2021-07-27
This patch fixes hypothesis.strategies._internal.types.is_a_new_type. It was failing on Python 3.10.0b4, where NewType is a function.
6.14.4 - 2021-07-26
This patch fixes from_type() <#hypothesis.strategies.from_type> and register_type_strategy() <#hypothesis.strategies.register_type_strategy> for python:typing.NewType on Python 3.10, which changed the underlying implementation (see bpo-44353 <https://bugs.python.org/issue44353> for details).
6.14.3 - 2021-07-18
This patch updates our autoformatting tools, improving our code style without any API changes.
6.14.2 - 2021-07-12
This patch ensures that we shorten tracebacks for tests which fail due to inconsistent data generation between runs (i.e. raise Flaky).
6.14.1 - 2021-07-02
This patch updates some internal type annotations. There is no user-visible change.
6.14.0 - 2021-06-09
The Phase.explain <#hypothesis.Phase.explain> phase now requires shrinking to be enabled, and will be automatically skipped for deadline-exceeded errors.
6.13.14 - 2021-06-04
This patch improves the tuples() <#hypothesis.strategies.tuples> strategy type annotations, to preserve the element types for up to length-five tuples (issue #3005 <https://github.com/HypothesisWorks/hypothesis/issues/3005>).
As for one_of() <#hypothesis.strategies.one_of>, this is the best we can do before a planned extension <https://mail.python.org/archives/list/typing-sig@python.org/thread/LOQFV3IIWGFDB7F5BDX746EZJG4VVBI3/> to PEP 646 <https://peps.python.org/pep-0646/> is released, hopefully in Python 3.11.
6.13.13 - 2021-06-04
This patch teaches the Ghostwriter <#ghostwriter> how to find custom ufuncs from any module that defines them, and that yaml.unsafe_load() does not undo yaml.safe_load().
6.13.12 - 2021-06-03
This patch reduces the amount of internal code excluded from our test suite's code coverage checks.
There is no user-visible change.
6.13.11 - 2021-06-02
This patch removes some old internal helper code that previously existed to make Python 2 compatibility easier.
There is no user-visible change.
6.13.10 - 2021-05-30
This release adjusts some internal code to help make our test suite more reliable.
There is no user-visible change.
6.13.9 - 2021-05-30
This patch cleans up some internal code related to filtering strategies.
There is no user-visible change.
6.13.8 - 2021-05-28
This patch slightly improves the performance of some internal code for generating integers.
6.13.7 - 2021-05-27
This patch fixes a bug in from_regex() <#hypothesis.strategies.from_regex> that caused from_regex("", fullmatch=True) to unintentionally generate non-empty strings (issue #4982 <https://github.com/HypothesisWorks/hypothesis/issues/4982>).
The only strings that completely match an empty regex pattern are empty strings.
6.13.6 - 2021-05-26
This patch fixes a bug that caused integers() <#hypothesis.strategies.integers> to shrink towards negative values instead of positive values in some cases.
6.13.5 - 2021-05-24
This patch fixes rare cases where hypothesis write --binary-op could print reproducing instructions <#reproducing-inputs> from the internal search for an identity element.
6.13.4 - 2021-05-24
This patch removes some unnecessary intermediate list-comprehensions, using the latest versions of pyupgrade <https://pypi.org/project/pyupgrade/> and shed <https://pypi.org/project/shed/>.
6.13.3 - 2021-05-23
This patch adds a .hypothesis property to invalid test functions, bringing them inline with valid tests and fixing a bug where pytest-asyncio <https://pypi.org/project/pytest-asyncio/> would swallow the real error message and mistakenly raise a version incompatibility error.
6.13.2 - 2021-05-23
Some of Hypothesis's numpy/pandas strategies use a fill argument to speed up generating large arrays, by generating a single fill value and sharing that value among many array slots instead of filling every single slot individually.
When no fill argument is provided, Hypothesis tries to detect whether it is OK to automatically use the elements argument as a fill strategy, so that it can still use the faster approach.
This patch fixes a bug that would cause that optimization to trigger in some cases where it isn't 100% guaranteed to be OK.
If this makes some of your numpy/pandas tests run more slowly, try adding an explicit fill argument to the relevant strategies to ensure that Hypothesis always uses the faster approach.
6.13.1 - 2021-05-20
This patch strengthens some internal import-time consistency checks for the built-in strategies.
There is no user-visible change.
6.13.0 - 2021-05-18
This release adds URL fragment generation to the urls() <#hypothesis.provisional.urls> strategy (issue #2908 <https://github.com/HypothesisWorks/hypothesis/issues/2908>). Thanks to Pax (R. Margret) for contributing this patch at the PyCon US Mentored Sprints <https://us.pycon.org/2021/summits/mentored-sprints/>!
6.12.1 - 2021-05-17
This patch fixes issue #2964 <https://github.com/HypothesisWorks/hypothesis/issues/2964>, where .map() and .filter() methods were omitted from the repr() of just() <#hypothesis.strategies.just> and sampled_from() <#hypothesis.strategies.sampled_from> strategies, since version 5.43.7.
6.12.0 - 2021-05-06
This release automatically rewrites some simple filters, such as integers().filter(lambda x: x > 9) to the more efficient integers(min_value=10), based on the AST of the predicate.
We continue to recommend using the efficient form directly wherever possible, but this should be useful for e.g. pandera <https://pypi.org/project/pandera/> "Checks" where you already have a simple predicate and translating manually is really annoying. See issue #2701 <https://github.com/HypothesisWorks/hypothesis/issues/2701> for ideas about floats and simple text strategies.
6.11.0 - 2021-05-06
hypothesis.target() <#hypothesis.target> now returns the observation value, allowing it to be conveniently used inline in expressions such as assert target(abs(a - b)) < 0.1.
6.10.1 - 2021-04-26
This patch fixes a deprecation warning if you're using recent versions of importlib-metadata <https://pypi.org/project/importlib-metadata/> (issue #2934 <https://github.com/HypothesisWorks/hypothesis/issues/2934>), which we use to load third-party plugins <#entry-points> such as Pydantic's integration <https://docs.pydantic.dev/latest/hypothesis_plugin/>. On older versions of importlib-metadata <https://pypi.org/project/importlib-metadata/>, there is no change and you don't need to upgrade.
6.10.0 - 2021-04-17
This release teaches the Ghostwriter <#ghostwriter> to read parameter types from Sphinx, Google, or Numpy-style structured docstrings, and improves some related heuristics about how to test scientific and numerical programs.
6.9.2 - 2021-04-15
This release improves the Ghostwriter's <#ghostwriter> handling of exceptions, by reading :raises ...: entries in function docstrings and ensuring that we don't suppresss the error raised by test assertions.
6.9.1 - 2021-04-12
This patch updates our autoformatting tools, improving our code style without any API changes.
6.9.0 - 2021-04-11
This release teaches from_type() <#hypothesis.strategies.from_type> how to see through python:typing.Annotated. Thanks to Vytautas Strimaitis for reporting and fixing issue #2919 <https://github.com/HypothesisWorks/hypothesis/issues/2919>!
6.8.12 - 2021-04-11
If rich <https://pypi.org/project/rich/> is installed, the hypothesis write command will use it to syntax-highlight the Ghostwritten <#ghostwriter> code.
6.8.11 - 2021-04-11
This patch improves an error message from builds() <#hypothesis.strategies.builds> when from_type() <#hypothesis.strategies.from_type> would be more suitable (issue #2930 <https://github.com/HypothesisWorks/hypothesis/issues/2930>).
6.8.10 - 2021-04-11
This patch updates the type annotations for arrays() <#hypothesis.extra.numpy.arrays> to reflect that shape: SearchStrategy[int] is supported.
6.8.9 - 2021-04-07
This patch fixes from_type() <#hypothesis.strategies.from_type> with abstract types which have either required but non-type-annotated arguments to __init__, or where from_type() <#hypothesis.strategies.from_type> can handle some concrete subclasses but not others.
6.8.8 - 2021-04-07
This patch teaches hypothesis write to check for possible roundtrips in several more cases, such as by looking for an inverse in the module which defines the function to test.
6.8.7 - 2021-04-07
This patch adds a more helpful error message if you try to call sampled_from() <#hypothesis.strategies.sampled_from> on an Enum which has no members, but does have dataclass()-style annotations (issue #2923 <https://github.com/HypothesisWorks/hypothesis/issues/2923>).
6.8.6 - 2021-04-06
The fixed_dictionaries() <#hypothesis.strategies.fixed_dictionaries> strategy now preserves dict iteration order instead of sorting the keys. This also affects the pretty-printing of keyword arguments to @given() <#hypothesis.given> (issue #2913 <https://github.com/HypothesisWorks/hypothesis/issues/2913>).
6.8.5 - 2021-04-05
This patch teaches hypothesis write to default to ghostwriting tests with --style=pytest only if pytest <https://pypi.org/project/pytest/> is installed, or --style=unittest otherwise.
6.8.4 - 2021-04-01
This patch adds type annotations for the settings <#hypothesis.settings> decorator, to avoid an error when running mypy in strict mode.
6.8.3 - 2021-03-28
This patch improves the Ghostwriter's <#ghostwriter> handling of strategies to generate various fiddly types including frozensets, keysviews, valuesviews, regex matches and patterns, and so on.
6.8.2 - 2021-03-27
This patch fixes some internal typos. There is no user-visible change.
6.8.1 - 2021-03-14
This patch lays more groundwork for filter rewriting (issue #2701 <https://github.com/HypothesisWorks/hypothesis/issues/2701>). There is no user-visible change... yet.
6.8.0 - 2021-03-11
This release registers <#hypothesis.strategies.register_type_strategy> the remaining builtin types, and teaches from_type() <#hypothesis.strategies.from_type> to try resolving ForwardRef and Type references to built-in types.
6.7.0 - 2021-03-10
This release teaches RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine> to avoid checking invariant() <#hypothesis.stateful.invariant>s until all initialize() <#hypothesis.stateful.initialize> rules have been run. You can enable checking of specific invariants for incompletely initialized machines by using @invariant(check_during_init=True) (issue #2868 <https://github.com/HypothesisWorks/hypothesis/issues/2868>).
In previous versions, it was possible if awkward to implement this behaviour using precondition() <#hypothesis.stateful.precondition> and an auxiliary variable.
6.6.1 - 2021-03-09
This patch improves the error message when from_type() <#hypothesis.strategies.from_type> fails to resolve a forward-reference inside a python:typing.Type such as Type["int"] (issue #2565 <https://github.com/HypothesisWorks/hypothesis/issues/2565>).
6.6.0 - 2021-03-07
This release makes it an explicit error to apply invariant() <#hypothesis.stateful.invariant> to a rule() <#hypothesis.stateful.rule> or initialize() <#hypothesis.stateful.initialize> rule in stateful testing <#stateful>. Such a combination had unclear semantics, especially in combination with precondition() <#hypothesis.stateful.precondition>, and was never meant to be allowed (issue #2681 <https://github.com/HypothesisWorks/hypothesis/issues/2681>).
6.5.0 - 2021-03-07
This release adds Phase.explain <#hypothesis.Phase.explain>, in which Hypothesis attempts to explain why your test failed by pointing to suspicious lines of code (i.e. those which were always, and only, run on failing inputs). We plan to include "generalising" failing examples in this phase in a future release (issue #2192 <https://github.com/HypothesisWorks/hypothesis/issues/2192>).
6.4.3 - 2021-03-04
This patch fixes issue #2794 <https://github.com/HypothesisWorks/hypothesis/issues/2794>, where nesting deferred() <#hypothesis.strategies.deferred> strategies within recursive() <#hypothesis.strategies.recursive> strategies could trigger an internal assertion. While it was always possible to get the same results from a more sensible strategy, the convoluted form now works too.
6.4.2 - 2021-03-04
This patch fixes several problems with mypy when --no-implicit-reexport <https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-no-implicit-reexport> was activated in user projects.
Thanks to Nikita Sobolev for fixing issue #2884 <https://github.com/HypothesisWorks/hypothesis/issues/2884>!
6.4.1 - 2021-03-04
This patch fixes an exception that occurs when using type unions of the typing-extensions <https://pypi.org/project/typing-extensions/> Literal backport on Python 3.6.
Thanks to Ben Anhalt for identifying and fixing this bug.
6.4.0 - 2021-03-02
This release fixes stateful testing methods <#stateful> with multiple precondition() <#hypothesis.stateful.precondition> decorators. Previously, only the outer-most precondition was checked (issue #2681 <https://github.com/HypothesisWorks/hypothesis/issues/2681>).
6.3.4 - 2021-02-28
This patch refactors some internals of RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>. There is no change to the public API or behaviour.
6.3.3 - 2021-02-26
This patch moves some internal code, so that future work can avoid creating import cycles. There is no user-visible change.
6.3.2 - 2021-02-25
This patch enables register_type_strategy() <#hypothesis.strategies.register_type_strategy> for subclasses of python:typing.TypedDict. Previously, from_type() <#hypothesis.strategies.from_type> would ignore the registered strategy (issue #2872 <https://github.com/HypothesisWorks/hypothesis/issues/2872>).
Thanks to Ilya Lebedev for identifying and fixing this bug!
6.3.1 - 2021-02-24
This release lays the groundwork for automatic rewriting of simple filters, for example converting integers().filter(lambda x: x > 9) to integers(min_value=10).
Note that this is not supported yet, and we will continue to recommend writing the efficient form directly wherever possible - predicate rewriting is provided mainly for the benefit of downstream libraries which would otherwise have to implement it for themselves (e.g. pandera <https://pypi.org/project/pandera/> and icontract-hypothesis <https://pypi.org/project/icontract-hypothesis/>). See issue #2701 <https://github.com/HypothesisWorks/hypothesis/issues/2701> for details.
6.3.0 - 2021-02-20
The Hypothesis pytest <https://pypi.org/project/pytest/> plugin now requires pytest version 4.6 or later. If the plugin detects an earlier version of pytest, it will automatically deactivate itself.
(4.6.x is the earliest pytest branch that still accepts community bugfixes.) <https://docs.pytest.org/en/6.2.x/py27-py34-deprecation.html>
Hypothesis-based tests should continue to work in earlier versions of pytest, but enhanced integrations provided by the plugin (such as --hypothesis-show-statistics and other command-line flags) will no longer be available in obsolete pytest versions.
6.2.0 - 2021-02-12
If you use pytest-html <https://pypi.org/project/pytest-html/>, Hypothesis now includes the summary statistics for each test <#statistics> in the HTML report, whether or not the --hypothesis-show-statistics argument was passed to show them in the command-line output.
6.1.1 - 2021-01-31
This patch updates our automatic code formatting to use shed <https://pypi.org/project/shed/>, which includes autoflake <https://pypi.org/project/autoflake/>, black <https://pypi.org/project/black/>, isort <https://pypi.org/project/isort/>, and pyupgrade <https://pypi.org/project/pyupgrade/> (issue #2780 <https://github.com/HypothesisWorks/hypothesis/issues/2780>).
6.1.0 - 2021-01-29
This release teaches Hypothesis to distinguish between errors based on the __cause__ or __context__ of otherwise identical exceptions <https://docs.python.org/3/library/exceptions.html>, which is particularly useful when internal errors can be wrapped by a library-specific or semantically appropriate exception such as:
try:
do_the_thing(foo, timeout=10)
except Exception as err:
raise FooError("Failed to do the thing") from errEarlier versions of Hypothesis only see the FooError, while we can now distinguish a FooError raised because of e.g. an internal assertion from one raised because of a TimeoutExceeded exception.
6.0.4 - 2021-01-27
This release prevents a race condition inside recursive() <#hypothesis.strategies.recursive> strategies. The race condition occurs when the same recursive() <#hypothesis.strategies.recursive> strategy is shared among tests that are running in multiple threads (issue #2717 <https://github.com/HypothesisWorks/hypothesis/issues/2717>).
6.0.3 - 2021-01-23
This patch improves the type annotations for one_of() <#hypothesis.strategies.one_of>, by adding overloads to handle up to five distinct arguments as Union before falling back to Any, as well as annotating the | (__or__) operator for strategies (issue #2765 <https://github.com/HypothesisWorks/hypothesis/issues/2765>).
6.0.2 - 2021-01-14
This release makes some small improvements to how filtered strategies work. It should improve the performance of shrinking filtered strategies, and may under some (probably rare) circumstances improve the diversity of generated examples.
6.0.1 - 2021-01-13
This patch fixes an interaction where our test statistics <#statistics> handling made Pytest's --junit-xml output fail to validate against the strict xunit2 schema (issue #1975 <https://github.com/HypothesisWorks/hypothesis/issues/1975>).
6.0.0 - 2021-01-08
Welcome to the next major version of Hypothesis!
There are no new features here, as we release those in minor versions. Instead, 6.0 is a chance for us to remove deprecated features (many already converted into no-ops), and turn a variety of warnings into errors.
If you were running on the last version of Hypothesis 5.x without any Hypothesis deprecation warnings, this will be a very boring upgrade. In fact, nothing will change for you at all.
Changes
- Many functions now use PEP 3102 <https://peps.python.org/pep-3102/> keyword-only arguments where passing positional arguments was deprecated since 5.5.
- hypothesis.extra.django.from_model() <#hypothesis.extra.django.from_model> no longer accepts model as a keyword argument, where it could conflict with fields named "model".
- randoms() <#hypothesis.strategies.randoms> now defaults to use_true_random=False.
- complex_numbers() <#hypothesis.strategies.complex_numbers> no longer accepts min_magnitude=None; either use min_magnitude=0 or just omit the argument.
- hypothesis.provisional.ip4_addr_strings and ip6_addr_strings are removed in favor of ip_addresses(v=...).map(str) <#hypothesis.strategies.ip_addresses>.
- register_type_strategy() <#hypothesis.strategies.register_type_strategy> no longer accepts generic types with type arguments, which were always pretty badly broken.
- Using function-scoped pytest fixtures is now a health-check error, instead of a warning.
- Tip:
The hypothesis codemod command can automatically refactor your code, particularly to convert positional to keyword arguments where those are now required.
Hypothesis 5.x
5.49.0 - 2021-01-07
This release adds the function_scoped_fixture <#hypothesis.HealthCheck.function_scoped_fixture> health check value, which can be used to suppress the existing warning that appears when @given <#hypothesis.given> is applied to a test that uses pytest function-scoped fixtures.
(This warning exists because function-scoped fixtures only run once per function, not once per example, which is usually unexpected and can cause subtle problems.)
When this warning becomes a health check error in a future release, suppressing it via Python warning settings will no longer be possible. In the rare case that once-per-function behaviour is intended, it will still be possible to use function_scoped_fixture <#hypothesis.HealthCheck.function_scoped_fixture> to opt out of the health check error for specific tests.
5.48.0 - 2021-01-06
This release adds hypothesis.currently_in_test_context() <#hypothesis.currently_in_test_context>, which can be used to check whether the calling code is currently running inside an @given <#hypothesis.given> or stateful <#stateful> test.
This is most useful for third-party integrations and assertion helpers which may wish to use assume() <#hypothesis.assume> or target() <#hypothesis.target>, without also requiring that the helper only be used from property-based tests (issue #2581 <https://github.com/HypothesisWorks/hypothesis/issues/2581>).
5.47.0 - 2021-01-05
This release upgrades the import logic for ghostwritten tests <#ghostwriter>, handling many cases where imports would previously be missing or from unexpected locations.
5.46.0 - 2021-01-04
This release upgrades from_type() <#hypothesis.strategies.from_type>, to infer strategies for type-annotated arguments even if they have defaults when it otherwise falls back to builds() <#hypothesis.strategies.builds> (issue #2708 <https://github.com/HypothesisWorks/hypothesis/issues/2708>).
5.45.0 - 2021-01-04
This release adds the hypothesis[codemods] <#codemods> extra, which you can use to check for and automatically fix issues such as use of deprecated Hypothesis APIs (issue #2705 <https://github.com/HypothesisWorks/hypothesis/issues/2705>).
5.44.0 - 2021-01-03
This patch fixes from_type() <#hypothesis.strategies.from_type> with the typing-extensions <https://pypi.org/project/typing-extensions/> Literal backport on Python 3.6.
5.43.9 - 2021-01-02
This patch fixes issue #2722 <https://github.com/HypothesisWorks/hypothesis/issues/2722>, where certain orderings of register_type_strategy() <#hypothesis.strategies.register_type_strategy>, ForwardRef, and from_type() <#hypothesis.strategies.from_type> could trigger an internal error.
5.43.8 - 2021-01-02
This patch makes some strategies for collections with a uniqueness constraint much more efficient, including dictionaries(keys=sampled_from(...), values=..) and lists(tuples(sampled_from(...), ...), unique_by=lambda x: x[0]). (related to issue #2036 <https://github.com/HypothesisWorks/hypothesis/issues/2036>)
5.43.7 - 2021-01-02
This patch extends our faster special case for sampled_from() <#hypothesis.strategies.sampled_from> elements in unique lists() <#hypothesis.strategies.lists> to account for chains of .map(...) and .filter(...) calls (issue #2036 <https://github.com/HypothesisWorks/hypothesis/issues/2036>).
5.43.6 - 2021-01-02
This patch improves the type annotations on assume() <#hypothesis.assume> and @reproduce_failure() <#hypothesis.reproduce_failure>.
5.43.5 - 2021-01-01
This patch updates our copyright headers to include 2021. Happy new year!
5.43.4 - 2020-12-24
This change fixes a documentation error in the database <#hypothesis.settings.database> setting.
The previous documentation suggested that callers could specify a database path string, or the special string ":memory:", but this setting has never actually allowed string arguments.
Permitted values are None, and instances of ExampleDatabase <#hypothesis.database.ExampleDatabase>.
5.43.3 - 2020-12-11
This patch fixes issue #2696 <https://github.com/HypothesisWorks/hypothesis/issues/2696>, an internal error triggered when the @example <#hypothesis.example> decorator was used and the verbosity <#hypothesis.settings.verbosity> setting was quiet.
5.43.2 - 2020-12-10
This patch improves the error message from the data_frames() <#hypothesis.extra.pandas.data_frames> strategy when both the rows and columns arguments are given, but there is a missing entry in rows and the corresponding column has no fill value (issue #2678 <https://github.com/HypothesisWorks/hypothesis/issues/2678>).
5.43.1 - 2020-12-10
This patch improves the error message if builds() <#hypothesis.strategies.builds> is passed an Enum which cannot be called without arguments, to suggest using sampled_from() <#hypothesis.strategies.sampled_from> (issue #2693 <https://github.com/HypothesisWorks/hypothesis/issues/2693>).
5.43.0 - 2020-12-09
This release adds new timezones() <#hypothesis.strategies.timezones> and timezone_keys() <#hypothesis.strategies.timezone_keys> strategies (issue #2630 <https://github.com/HypothesisWorks/hypothesis/issues/2630>) based on the new python:zoneinfo module in Python 3.9.
pip install hypothesis[zoneinfo] will ensure that you have the appropriate backports installed if you need them.
5.42.3 - 2020-12-09
This patch fixes an internal error in datetimes() <#hypothesis.strategies.datetimes> with allow_imaginary=False where the timezones argument can generate tzinfo=None (issue #2662 <https://github.com/HypothesisWorks/hypothesis/issues/2662>).
5.42.2 - 2020-12-09
This patch teaches hypothesis.extra.django.from_field() <#hypothesis.extra.django.from_field> to infer more efficient strategies by inspecting (not just filtering by) field validators for numeric and string fields (issue #1116 <https://github.com/HypothesisWorks/hypothesis/issues/1116>).
5.42.1 - 2020-12-09
This patch refactors hypothesis.settings <#hypothesis.settings> to use type-annotated keyword arguments instead of **kwargs, which makes tab-completion much more useful - as well as type-checkers like mypy <https://pypi.org/project/mypy/>.
5.42.0 - 2020-12-09
This patch teaches the magic() <#hypothesis.extra.ghostwriter.magic> ghostwriter to recognise "en/de" function roundtrips other than the common encode/decode pattern, such as encrypt/decrypt or, encipher/decipher.
5.41.5 - 2020-12-05
This patch adds a performance optimisation to avoid saving redundant seeds when using the .fuzz_one_input hook <#fuzz-one-input>.
5.41.4 - 2020-11-28
This patch fixes issue #2657 <https://github.com/HypothesisWorks/hypothesis/issues/2657>, where passing unicode patterns compiled with python:re.IGNORECASE to from_regex() <#hypothesis.strategies.from_regex> could trigger an internal error when casefolding a character creates a longer string (e.g. "\u0130".lower() -> "i\u0370").
5.41.3 - 2020-11-18
This patch adds a final fallback clause to our plugin logic <#entry-points> to fail with a warning rather than error on Python < 3.8 when neither the importlib-metadata <https://pypi.org/project/importlib-metadata/> (preferred) or setuptools <https://pypi.org/project/setuptools/> (fallback) packages are available.
5.41.2 - 2020-11-08
This patch fixes urls() <#hypothesis.provisional.urls> strategy ensuring that ~ (tilde) is treated as one of the url-safe characters (issue #2658 <https://github.com/HypothesisWorks/hypothesis/issues/2658>).
5.41.1 - 2020-11-03
This patch improves our CLI help and documentation <#hypothesis-cli>.
5.41.0 - 2020-10-30
Hypothesis now shrinks examples where the error is raised while drawing from a strategy. This makes complicated custom strategies much easier to debug, at the cost of a slowdown for use-cases where you catch and ignore such errors.
5.40.0 - 2020-10-30
This release teaches from_type() <#hypothesis.strategies.from_type> how to handle ChainMap, Counter, Deque, Generator, Match, OrderedDict, Pattern, and Set (issue #2654 <https://github.com/HypothesisWorks/hypothesis/issues/2654>).
5.39.0 - 2020-10-30
from_type() <#hypothesis.strategies.from_type> now knows how to resolve PEP 585 <https://peps.python.org/pep-0585/> parameterized standard collection types, which are new in Python 3.9 (issue #2629 <https://github.com/HypothesisWorks/hypothesis/issues/2629>).
5.38.1 - 2020-10-26
This patch fixes builds() <#hypothesis.strategies.builds>, so that when passed infer <#hypothesis.infer> for an argument with a non-Optional type annotation and a default value of None to build a class which defines an explicit __signature__ attribute, either None or that type may be generated.
This is unlikely to happen unless you are using pydantic <https://pypi.org/project/pydantic/> (issue #2648 <https://github.com/HypothesisWorks/hypothesis/issues/2648>).
5.38.0 - 2020-10-24
This release improves our support for @st.composite <#hypothesis.strategies.composite> on a python:classmethod or python:staticmethod (issue #2578 <https://github.com/HypothesisWorks/hypothesis/issues/2578>).
5.37.5 - 2020-10-24
This patch fixes from_type() <#hypothesis.strategies.from_type> with Iterable[T] (issue #2645 <https://github.com/HypothesisWorks/hypothesis/issues/2645>).
5.37.4 - 2020-10-20
This patch teaches the magic() <#hypothesis.extra.ghostwriter.magic> ghostwriter to recognise that pairs of functions like rgb_to_hsv() and hsv_to_rgb() should roundtrip() <#hypothesis.extra.ghostwriter.roundtrip>.
5.37.3 - 2020-10-15
This patch improves builds() <#hypothesis.strategies.builds> and from_type() <#hypothesis.strategies.from_type> support for explicitly defined __signature__ attributes, from version 5.8.3, to support generic types from the python:typing module.
Thanks to Rónán Carrigan for identifying and fixing this problem!
5.37.2 - 2020-10-14
This patch fixes from_lark() <#hypothesis.extra.lark.from_lark> with version 0.10.1+ of the lark-parser <https://pypi.org/project/lark-parser/> package.
5.37.1 - 2020-10-07
This patch fixes some broken links in the lark <#module-hypothesis.extra.lark> extra documentation.
5.37.0 - 2020-10-03
This release adds a new RedisExampleDatabase <#hypothesis.extra.redis.RedisExampleDatabase>, along with the ReadOnlyDatabase <#hypothesis.database.ReadOnlyDatabase> and MultiplexedDatabase <#hypothesis.database.MultiplexedDatabase> helpers, to support team workflows where failing examples can be seamlessly shared between everyone on the team - and your CI servers or buildbots.
5.36.2 - 2020-10-02
This patch ensures that if the "hypothesis" entry point <#entry-points> is callable, we call it after importing it. You can still use non-callable entry points (like modules), which are only imported.
We also prefer importlib.metadata <https://docs.python.org/3/library/importlib.metadata.html> or the backport <https://pypi.org/project/importlib-metadata/> over pkg_resources <https://setuptools.pypa.io/en/latest/pkg_resources.html>, which makes import hypothesis around 200 milliseconds faster (issue #2571 <https://github.com/HypothesisWorks/hypothesis/issues/2571>).
5.36.1 - 2020-09-25
This patch adds some helpful suggestions to error messages you might see while learning to use the @example() <#hypothesis.example> decorator (issue #2611 <https://github.com/HypothesisWorks/hypothesis/issues/2611>) or the one_of() <#hypothesis.strategies.one_of> strategy.
5.36.0 - 2020-09-24
This release upgrades the from_dtype() <#hypothesis.extra.numpy.from_dtype> strategy to pass optional **kwargs to the inferred strategy, and upgrades the arrays() <#hypothesis.extra.numpy.arrays> strategy to accept an elements=kwargs dict to pass through to from_dtype() <#hypothesis.extra.numpy.from_dtype>.
arrays(floating_dtypes(), shape, elements={"min_value": -10, "max_value": 10}) is a particularly useful pattern, as it allows for any floating dtype without triggering the roundoff warning for smaller types or sacrificing variety for larger types (issue #2552 <https://github.com/HypothesisWorks/hypothesis/issues/2552>).
5.35.4 - 2020-09-21
This patch reformats our code with the latest black <https://pypi.org/project/black/> to take advantage of the support for magic trailing commas.
5.35.3 - 2020-09-15
This release significantly improves the performance of Hypothesis's internal implementation of automaton learning. However this code does not run as part of the user-accessible API so this has no user-visible impact.
5.35.2 - 2020-09-14
This patch ensures that, when the generate phases <#hypothesis.settings.phases> is disabled, we can replay up to max_examples <#hypothesis.settings.max_examples> examples from the database - which is very useful when using Hypothesis with a fuzzer <#fuzz-one-input>.
Thanks to Afrida Tabassum for fixing issue #2585 <https://github.com/HypothesisWorks/hypothesis/issues/2585>!
5.35.1 - 2020-09-14
This patch changes some internal python:struct.Struct.format strings from bytes to str, to avoid python:BytesWarning when running python -bb <https://docs.python.org/3/using/cmdline.html#cmdoption-b>.
Thanks to everyone involved in pytest-xdist issue 596 <https://github.com/pytest-dev/pytest-xdist/issues/596>, bpo-16349 <https://bugs.python.org/issue16349>, bpo-21071 <https://bugs.python.org/issue21071>, and bpo-41777 <https://bugs.python.org/issue41777> for their work on this - it was a remarkably subtle issue!
5.35.0 - 2020-09-11
The target() <#hypothesis.target> function now accepts integers as well as floats.
5.34.1 - 2020-09-11
This patch adds explicit Optional annotations to our public API, to better support users who run mypy <https://pypi.org/project/mypy/> with --strict or no_implicit_optional=True.
Thanks to Krzysztof Przybyła for bringing this to our attention and writing the patch!
5.34.0 - 2020-09-11
This release drops support for Python 3.5, which reached end of life upstream <https://devguide.python.org/#status-of-python-branches> on 2020-09-13.
5.33.2 - 2020-09-09
This patch fixes a problem with builds() <#hypothesis.strategies.builds> that was not able to generate valid data for annotated classes with constructors.
Thanks to Nikita Sobolev for fixing issue #2603 <https://github.com/HypothesisWorks/hypothesis/issues/2603>!
5.33.1 - 2020-09-07
This patch improves the error message from the hypothesis write command if black <https://pypi.org/project/black/> (required for the ghostwriter <#ghostwriter>) is not installed.
Thanks to Nikita Sobolev for fixing issue #2604 <https://github.com/HypothesisWorks/hypothesis/issues/2604>!
5.33.0 - 2020-09-06
When reporting failing examples, or tried examples in verbose mode, Hypothesis now identifies which were from @example(...) <#hypothesis.example> explicit examples.
5.32.1 - 2020-09-06
This patch contains some internal refactoring. Thanks to Felix Sheldon for fixing issue #2516 <https://github.com/HypothesisWorks/hypothesis/issues/2516>!
5.32.0 - 2020-09-04
An array drawn from arrays() <#hypothesis.extra.numpy.arrays> will own its own memory; previously most arrays returned by this strategy were views.
5.31.0 - 2020-09-04
builds() <#hypothesis.strategies.builds> will use the __signature__ attribute of the target, if it exists, to retrieve type hints. Previously python:typing.get_type_hints(), was used by default. If argument names varied between the __annotations__ and __signature__, they would not be supplied to the target.
This was particularly an issue for pydantic <https://pypi.org/project/pydantic/> models which use an alias generator <https://docs.pydantic.dev/latest/api/config/#pydantic.alias_generators>.
5.30.1 - 2020-09-04
This patch makes the ghostwriter <#ghostwriter> much more robust when passed unusual modules.
- improved support for non-resolvable type annotations
- magic() <#hypothesis.extra.ghostwriter.magic> can now write equivalent() <#hypothesis.extra.ghostwriter.equivalent> tests
- running magic() <#hypothesis.extra.ghostwriter.magic> on modules where some names in __all__ are undefined skips such names, instead of raising an error
- magic() <#hypothesis.extra.ghostwriter.magic> now knows to skip mocks
- improved handling of import-time errors found by the ghostwriter CLI
5.30.0 - 2020-08-30
register_type_strategy() <#hypothesis.strategies.register_type_strategy> now supports python:typing.TypeVar, which was previously hard-coded, and allows a variety of types to be generated for an unconstrained TypeVar instead of just text() <#hypothesis.strategies.text>.
Thanks again to Nikita Sobolev for all your work on advanced types!
5.29.4 - 2020-08-28
This release fixes some hard to trigger bugs in Hypothesis's automata learning code. This code is only run as part of the Hypothesis build process, and not for user code, so this release has no user visible impact.
5.29.3 - 2020-08-27
This patch adds type annotations to the hypothesis.database <#database> module. There is no runtime change, but your typechecker might notice.
5.29.2 - 2020-08-27
This patch tracks some additional information in Hypothesis internals, and has no user-visible impact.
5.29.1 - 2020-08-27
This release fixes a bug in some Hypothesis internal support code for learning automata. This mostly doesn't have any user visible impact, although it slightly affects the learned shrink passes so shrinking may be subtly different.
5.29.0 - 2020-08-24
This release adds support for Hypothesis integration via entry points <#entry-points>, which allows for smoother integration of third-party Hypothesis extensions and external libraries. Unless you're publishing a library with Hypothesis integration, you'll probably only ever use this indirectly!
5.28.0 - 2020-08-24
from_type() <#hypothesis.strategies.from_type> can now resolve TypeVar instances when the bound is a ForwardRef, so long as that name is in fact defined in the same module as the typevar (no TYPE_CHECKING tricks, sorry). This feature requires Python 3.7 or later.
Thanks to Zac Hatfield-Dodds and Nikita Sobolev for this feature!
5.27.0 - 2020-08-20
This patch adds two new ghostwriters <#ghostwriter> to test binary operations <https://en.wikipedia.org/wiki/Binary_operation>, like python:operator.add(), and Numpy ufuncs and gufuncs like np.matmul().
5.26.1 - 2020-08-19
This release improves the performance of some methods in Hypothesis's internal automaton library. These are currently only lightly used by user code, but this may result in slightly faster shrinking.
5.26.0 - 2020-08-17
register_type_strategy() <#hypothesis.strategies.register_type_strategy> no longer accepts parametrised user-defined generic types, because the resolution logic was quite badly broken (issue #2537 <https://github.com/HypothesisWorks/hypothesis/issues/2537>).
Instead of registering a strategy for e.g. MyCollection[int], you should register a function for MyCollection and inspect the type parameters within that function <https://stackoverflow.com/q/48572831>.
Thanks to Nikita Sobolev for the bug report, design assistance, and pull request to implement this feature!
5.25.0 - 2020-08-16
Tired of writing tests? Or new to Hypothesis and not sure where to start?
This release is for you! With our new Ghostwriter functions <#ghostwriter> and hypothesis write ... command-line interface <#hypothesis-cli>, you can stop writing tests entirely... or take the source code Hypothesis writes for you as a starting point.
This has been in the works for months, from issue #2118 <https://github.com/HypothesisWorks/hypothesis/issues/2118> to versions 5.18.3, 5.23.5, and 5.23.5 - particular thanks to the many people who reviewed pull requests or commented on demos, and to Timothy Crosley's hypothesis-auto <https://pypi.org/project/hypothesis-auto/> project for inspiration.
5.24.4 - 2020-08-14
This patch adds yet more internal functions to support a new feature we're working on, like version 5.18.3 and version 5.23.6. We promise it's worth the wait!
5.24.3 - 2020-08-13
This release fixes a small internal bug in Hypothesis's internal automaton library. Fortunately this bug was currently impossible to hit in user facing code, so this has no user visible impact.
5.24.2 - 2020-08-12
This release improves shrink quality by allowing Hypothesis to automatically learn new shrink passes for difficult to shrink tests.
The automatic learning is not currently accessible in user code (it still needs significant work on robustness and performance before it is ready for that), but this release includes learned passes that should improve shrinking quality for tests which use any of the text() <#hypothesis.strategies.text>, floats() <#hypothesis.strategies.floats>, datetimes() <#hypothesis.strategies.datetimes>, emails() <#hypothesis.strategies.emails>, and complex_numbers() <#hypothesis.strategies.complex_numbers> strategies.
5.24.1 - 2020-08-12
This patch updates some docstrings, without changing runtime behaviour.
5.24.0 - 2020-08-10
The functions() <#hypothesis.strategies.functions> strategy has a new argument pure=True, which ensures that the same return value is generated for identical calls to the generated function (issue #2538 <https://github.com/HypothesisWorks/hypothesis/issues/2538>).
Thanks to Zac Hatfield-Dodds and Nikita Sobolev for this feature!
5.23.12 - 2020-08-10
This release removes a number of Hypothesis's internal "shrink passes" - transformations it makes to a generated test case during shrinking - which appeared to be redundant with other transformations.
It is unlikely that you will see much impact from this. If you do, it will likely show up as a change in shrinking performance (probably slower, maybe faster), or possibly in worse shrunk results. If you encounter the latter, please let us know.
5.23.11 - 2020-08-04
This release fixes a bug in some internal Hypothesis support code. It has no user visible impact.
5.23.10 - 2020-08-04
This release improves the quality of shrunk test cases in some special cases. Specifically, it should get shrinking unstuck in some scenarios which require simultaneously changing two parts of the generated test case.
5.23.9 - 2020-08-03
This release improves the performance of some internal support code. It has no user visible impact, as that code is not currently run during normal Hypothesis operation.
5.23.8 - 2020-07-31
This release adds a heuristic to detect when shrinking has finished despite the fact that there are many more possible transformations to try. This will be particularly useful for tests where the minimum failing test case is very large despite there being many smaller test cases possible, where it is likely to speed up shrinking dramatically.
In some cases it is likely that this will result in worse shrunk test cases. In those cases rerunning the test will result in further shrinking.
5.23.7 - 2020-07-29
This release makes some performance improvements to shrinking. They should only be noticeable for tests that are currently particularly slow to shrink.
5.23.6 - 2020-07-29
This patch adds some more internal functions to support a new feature we're working on, like version 5.18.3. There is still no user-visible change... yet.
5.23.5 - 2020-07-29
This release makes some changes to internal support code that is not currently used in production Hypothesis. It has no user visible effect at present.
5.23.4 - 2020-07-29
This release improves shrinking quality in some special cases.
5.23.3 - 2020-07-27
This release fixes issue #2507 <https://github.com/HypothesisWorks/hypothesis/issues/2507>, where lazy evaluation meant that the values drawn from a sampled_from() <#hypothesis.strategies.sampled_from> strategy could depend on mutations of the sampled sequence that happened after the strategy was constructed.
5.23.2 - 2020-07-27
This patch fixes issue #2462 <https://github.com/HypothesisWorks/hypothesis/issues/2462>, a bug in our handling of unittest.TestCase.subTest(). Thanks to Israel Fruchter for fixing this at the EuroPython sprints!
5.23.1 - 2020-07-26
This release improves the behaviour of the characters() <#hypothesis.strategies.characters> strategy when shrinking, by changing which characters are considered smallest to prefer more "normal" ascii characters where available.
5.23.0 - 2020-07-26
The default print_blob setting is now smarter. It defaults to True in CI and False for local development.
Thanks to Hugo van Kemenade for implementing this feature at the EuroPython sprints!
5.22.0 - 2020-07-25
The slices() <#hypothesis.strategies.slices> strategy can now generate slices for empty sequences, slices with negative start and stop indices (from the end of the sequence), and step=None in place of step=1.
Thanks to Sangarshanan for implementing this feature at the EuroPython sprints!
5.21.0 - 2020-07-23
This release ensures that tests which raise RecursionError are not reported as flaky simply because we run them from different initial stack depths (issue #2494 <https://github.com/HypothesisWorks/hypothesis/issues/2494>).
5.20.4 - 2020-07-23
This release improves the performance of the sample method on objects obtained from randoms() <#hypothesis.strategies.randoms> when use_true_random=False. This should mostly only be noticeable when the sample size is a large fraction of the population size, but may also help avoid health check failures in some other cases.
5.20.3 - 2020-07-21
This release makes some internal changes for testing purposes and should have no user visible effect.
5.20.2 - 2020-07-18
This release fixes a small caching bug in Hypothesis internals that may under some circumstances have resulted in a less diverse set of test cases being generated than was intended.
Fixing this problem revealed some performance problems that could occur during targeted property based testing, so this release also fixes those. Targeted property-based testing should now be significantly faster in some cases, but this may be at the cost of reduced effectiveness.
5.20.1 - 2020-07-17
This patch updates our formatting to use isort <https://pypi.org/project/isort/> 5. There is no user-visible change.
5.20.0 - 2020-07-17
The basic_indices() <#hypothesis.extra.numpy.basic_indices> strategy can now generate bare indexers in place of length-one tuples. Thanks to Andrea for this patch!
5.19.3 - 2020-07-15
This patch removes an internal use of distutils in order to avoid this setuptools warning <https://github.com/pypa/setuptools/issues/2261> for some users.
5.19.2 - 2020-07-13
This patch contains a small internal refactoring with no user-visible impact.
Thanks to Andrea for writing this at the SciPy 2020 Sprints <https://www.scipy2020.scipy.org/sprints-schedule>!
5.19.1 - 2020-07-12
This release slightly improves shrinking behaviour. This should mainly only impact stateful tests, but may have some minor positive impact on shrinking collections (lists, sets, etc).
5.19.0 - 2020-06-30
This release improves the randoms() <#hypothesis.strategies.randoms> strategy by adding support for Random instances where Hypothesis generates the random values rather than having them be "truly" random.
5.18.3 - 2020-06-27
This patch adds some internal functions to support a new feature we're working on. There is no user-visible change... yet.
5.18.2 - 2020-06-26
This patch improves our docs for the derandomize <#hypothesis.settings.derandomize> setting.
5.18.1 - 2020-06-25
This release consists of some internal refactoring to the shrinker in preparation for future work. It has no user visible impact.
5.18.0 - 2020-06-22
This release teaches Hypothesis to shorten tracebacks for @example <#hypothesis.example>, as we already do for generated examples, so that you can focus on your code rather than ours.
If you have multiple failing explicit examples, they will now all be reported. To report only the first failure, you can use the report_multiple_bugs=False <#hypothesis.settings.report_multiple_bugs> setting as for generated examples.
5.17.0 - 2020-06-22
This patch adds strategy inference for the Literal, NewType, Type, DefaultDict, and TypedDict types from the typing-extensions <https://pypi.org/project/typing-extensions/> backport on PyPI.
5.16.3 - 2020-06-21
This patch precomputes some of the setup logic for our external fuzzer integration <#fuzz-one-input> and sets deadline=None <#hypothesis.settings.deadline> in fuzzing mode, saving around 150us on each iteration.
This is around two-thirds the runtime to fuzz an empty test with @given(st.none()), and nice to have even as a much smaller fraction of the runtime for non-trivial tests.
5.16.2 - 2020-06-19
This patch fixes an internal error when warning about the use of function-scoped fixtures for parametrised tests where the parametrised value contained a % character. Thanks to Bryant for reporting and fixing this bug!
5.16.1 - 2020-06-10
If you pass a python:list or python:tuple where a strategy was expected, the error message now mentions sampled_from() <#hypothesis.strategies.sampled_from> as an example strategy.
Thanks to the enthusiastic participants in the PyCon Mentored Sprints <https://us.pycon.org/2020/hatchery/mentoredsprints/> who suggested adding this hint.
5.16.0 - 2020-05-27
functions() <#hypothesis.strategies.functions> can now infer the appropriate returns strategy if you pass a like function with a return-type annotation. Before, omitting the returns argument would generate functions that always returned None.
5.15.1 - 2020-05-21
Fix from_type() <#hypothesis.strategies.from_type> with generic types under Python 3.9.
5.15.0 - 2020-05-19
This patch fixes an error that happens when multiple threads create new strategies.
5.14.0 - 2020-05-13
Passing min_magnitude=None to complex_numbers() <#hypothesis.strategies.complex_numbers> is now deprecated - you can explicitly pass min_magnitude=0, or omit the argument entirely.
5.13.1 - 2020-05-13
This patch fixes an internal error in from_type() <#hypothesis.strategies.from_type> for python:typing.NamedTuple in Python 3.9. Thanks to Michel Salim for reporting and fixing issue #2427 <https://github.com/HypothesisWorks/hypothesis/issues/2427>!
5.13.0 - 2020-05-12
This release upgrades the test statistics available via the --hypothesis-show-statistics <#statistics> option to include separate information on each of the phases <#hypothesis.settings.phases> (issue #1555 <https://github.com/HypothesisWorks/hypothesis/issues/1555>).
5.12.2 - 2020-05-12
This patch teaches the from_type() <#hypothesis.strategies.from_type> internals to return slightly more efficient strategies for some generic sets and mappings.
5.12.1 - 2020-05-12
This patch adds a # noqa comment for flake8 <https://pypi.org/project/flake8/> 3.8.0, which disagrees with mypy <https://pypi.org/project/mypy/> about how to write the type of ....
5.12.0 - 2020-05-10
This release limits the maximum duration of the shrinking phase to five minutes, so that Hypothesis does not appear to hang when making very slow progress shrinking a failing example (issue #2340 <https://github.com/HypothesisWorks/hypothesis/issues/2340>).
If one of your tests triggers this logic, we would really appreciate a bug report to help us improve the shrinker for difficult but realistic workloads.
5.11.0 - 2020-05-07
This release improves the interaction between assume() <#hypothesis.assume> and the @example() <#hypothesis.example> decorator, so that the following test no longer fails with UnsatisfiedAssumption (issue #2125 <https://github.com/HypothesisWorks/hypothesis/issues/2125>):
@given(value=floats(0, 1))
@example(value=0.56789) # used to make the test fail!
@pytest.mark.parametrize("threshold", [0.5, 1])
def test_foo(threshold, value):
assume(value < threshold)
...5.10.5 - 2020-05-04
If you have Django <https://pypi.org/project/Django/> installed but don't use it, this patch will make import hypothesis a few hundred milliseconds faster (e.g. 0.704s -> 0.271s).
Thanks to importtime-waterfall <https://pypi.org/project/importtime-waterfall/> for highlighting this problem and Jake Vanderplas <https://twitter.com/jakevdp/status/1130983439862181888> for the solution - it's impossible to misuse code from a module you haven't imported!
5.10.4 - 2020-04-24
This patch improves the internals of builds() <#hypothesis.strategies.builds> type inference, to handle recursive forward references in certain dataclasses. This is useful for e.g. hypothesmith <https://pypi.org/project/hypothesmith/>'s forthcoming LibCST <https://pypi.org/project/libcst/> mode.
5.10.3 - 2020-04-22
This release reverses the order in which some operations are tried during shrinking. This should generally be a slight performance improvement, but most tests are unlikely to notice much difference.
5.10.2 - 2020-04-22
This patch fixes issue #2406 <https://github.com/HypothesisWorks/hypothesis/issues/2406>, where use of pandas:pandas.Timestamp objects as bounds for the datetimes() <#hypothesis.strategies.datetimes> strategy caused an internal error. This bug was introduced in version 5.8.1.
5.10.1 - 2020-04-19
This release is a small internal refactoring to how shrinking interacts with targeted property-based testing <#targeted> that should have no user visible impact.
5.10.0 - 2020-04-18
This release improves our support for datetimes and times around DST transitions.
times() <#hypothesis.strategies.times> and datetimes() <#hypothesis.strategies.datetimes> are now sometimes generated with fold=1, indicating that they represent the second occurrence of a given wall-time when clocks are set backwards. This may be set even when there is no transition, in which case the fold value should be ignored.
For consistency, timezones provided by the pytz <https://pypi.org/project/pytz/> package can now generate imaginary times (such as the hour skipped over when clocks 'spring forward' to daylight saving time, or during some historical timezone transitions). All other timezones have always supported generation of imaginary times.
If you prefer the previous behaviour, datetimes() <#hypothesis.strategies.datetimes> now takes an argument allow_imaginary which defaults to True but can be set to False for any timezones strategy.
5.9.1 - 2020-04-16
This patch fixes the rendering of binary() <#hypothesis.strategies.binary> docstring by using the proper backticks syntax.
5.9.0 - 2020-04-15
Failing tests which use target() <#hypothesis.target> now report the highest score observed for each target alongside the failing example(s), even without explicitly showing test statistics <#statistics>.
This improves the debugging workflow for tests of accuracy, which assert that the total imprecision is within some error budget - for example, abs(a - b) < 0.5. Previously, shrinking to a minimal failing example could often make errors seem smaller or more subtle than they really are (see the threshold problem <https://hypothesis.works/articles/threshold-problem/>, and issue #2180 <https://github.com/HypothesisWorks/hypothesis/issues/2180>).
5.8.6 - 2020-04-15
This patch improves the docstring of binary() <#hypothesis.strategies.binary>, the python:repr() of sampled_from() <#hypothesis.strategies.sampled_from> on an python:enum.Enum subclass, and a warning in our pytest plugin. There is no change in runtime behaviour.
5.8.5 - 2020-04-15
This release (potentially very significantly) improves the performance of failing tests in some rare cases, mostly only relevant when using targeted property-based testing <#targeted>, by stopping further optimisation of unrelated test cases once a failing example is found.
5.8.4 - 2020-04-14
This release fixes issue #2395 <https://github.com/HypothesisWorks/hypothesis/issues/2395>, where under some circumstances targeted property-based testing could cause Hypothesis to get caught in an infinite loop.
5.8.3 - 2020-04-12
This patch teaches builds() <#hypothesis.strategies.builds> and from_type() <#hypothesis.strategies.from_type> to use the __signature__ attribute of classes where it has been set, improving our support for pydantic <https://pypi.org/project/pydantic/> models (in pydantic >= 1.5 <https://github.com/pydantic/pydantic/pull/1034>).
5.8.2 - 2020-04-12
This release improves the performance of the part of the core engine that deliberately generates duplicate values.
5.8.1 - 2020-04-12
This patch improves dates() <#hypothesis.strategies.dates> shrinking, to simplify year, month, and day like datetimes() <#hypothesis.strategies.datetimes> rather than minimizing the number of days since 2000-01-01.
5.8.0 - 2020-03-24
This release adds a .hypothesis.fuzz_one_input <#fuzz-one-input> attribute to @given <#hypothesis.given> tests, for easy integration with external fuzzers such as python-afl <https://github.com/jwilk/python-afl> (supporting issue #171 <https://github.com/HypothesisWorks/hypothesis/issues/171>).
5.7.2 - 2020-03-24
This patch fixes issue #2341 <https://github.com/HypothesisWorks/hypothesis/issues/2341>, ensuring that the printed output from a stateful test cannot use variable names before they are defined.
5.7.1 - 2020-03-23
This patch fixes issue #2375 <https://github.com/HypothesisWorks/hypothesis/issues/2375>, preventing incorrect failure when a function scoped fixture is overridden with a higher scoped fixture.
5.7.0 - 2020-03-19
This release allows the array_dtypes() <#hypothesis.extra.numpy.array_dtypes> strategy to generate Numpy dtypes which have field titles in addition to field names <https://numpy.org/doc/stable/user/basics.rec.html#field-titles>. We expect this to expose latent bugs where code expects that set(dtype.names) == set(dtype.fields), though the latter may include titles.
5.6.1 - 2020-03-18
This makes model a positional-only argument to from_model() <#hypothesis.extra.django.from_model>, to support models with a field literally named "model" (issue #2369 <https://github.com/HypothesisWorks/hypothesis/issues/2369>).
5.6.0 - 2020-02-29
This release adds an explicit warning for tests that are both decorated with @given(...) <#hypothesis.given> and request a function-scoped pytest fixture, because such fixtures are only executed once for all Hypothesis test cases and that often causes trouble (issue #377 <https://github.com/HypothesisWorks/hypothesis/issues/377>).
It's very difficult to fix this on the pytest <https://pypi.org/project/pytest/> side, so since 2015 our advice has been "just don't use function-scoped fixtures with Hypothesis". Now we detect and warn about the issue at runtime!
5.5.5 - 2020-02-29
This release cleans up the internal machinery for Stateful tests <#stateful>, after we dropped the legacy APIs in Hypothesis 5.0 (issue #2218 <https://github.com/HypothesisWorks/hypothesis/issues/2218>). There is no user-visible change.
5.5.4 - 2020-02-16
This patch fixes issue #2351 <https://github.com/HypothesisWorks/hypothesis/issues/2351>, arrays() <#hypothesis.extra.numpy.arrays> would raise a confusing error if we inferred a strategy for datetime64 or timedelta64 values with varying time units.
We now infer an internally-consistent strategy for such arrays, and have a more helpful error message if an inconsistent strategy is explicitly specified.
5.5.3 - 2020-02-14
This patch improves the signature of builds() <#hypothesis.strategies.builds> by specifying target as a positional-only argument on Python 3.8 (see PEP 570 <https://peps.python.org/pep-0570/>). The semantics of builds() <#hypothesis.strategies.builds> have not changed at all - this just clarifies the documentation.
5.5.2 - 2020-02-13
This release makes Hypothesis faster at generating test cases that contain duplicated values in their inputs.
5.5.1 - 2020-02-07
This patch has some tiny internal code clean-ups, with no user-visible change.
5.5.0 - 2020-02-07
Our style guide <https://github.com/HypothesisWorks/hypothesis/blob/master/guides/api-style.rst> suggests that optional parameters should usually be keyword-only arguments (see PEP 3102 <https://peps.python.org/pep-3102/>) to prevent confusion based on positional arguments - for example, hypothesis.strategies.floats() <#hypothesis.strategies.floats> takes up to four boolean flags and many of the Numpy strategies have both dims and side bounds.
This release converts most optional parameters in our API to use keyword-only arguments - and adds a compatibility shim so you get warnings rather than errors everywhere (issue #2130 <https://github.com/HypothesisWorks/hypothesis/issues/2130>).
5.4.2 - 2020-02-06
This patch fixes compatibility with Python 3.5.2 (issue #2334 <https://github.com/HypothesisWorks/hypothesis/issues/2334>). Note that we only test the latest patch of each minor version <>, though as in this case we usually accept pull requests for older patch versions.
5.4.1 - 2020-02-01
This patch improves the repr of from_type() <#hypothesis.strategies.from_type>, so that in most cases it will display the strategy it resolves to rather than from_type(...). The latter form will continue to be used where resolution is not immediately successful, e.g. invalid arguments or recursive type definitions involving forward references.
5.4.0 - 2020-01-30
This release removes support for Python 3.5.0 and 3.5.1, where the python:typing module was quite immature (e.g. missing overload() and Type).
Note that Python 3.5 will reach its end-of-life in September 2020, and new releases of Hypothesis may drop support somewhat earlier.
Note:
pip install hypothesis should continue to give you the latest compatible version. If you have somehow ended up with an incompatible version, you need to update your packaging stack to pip >= 9.0 and setuptools >= 24.2 - see here for details <https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires>. Then pip uninstall hypothesis && pip install hypothesis will get you back to a compatible version.
5.3.1 - 2020-01-26
This patch does some minor internal cleanup; there is no user-visible change.
5.3.0 - 2020-01-21
The standard library ipaddress module is new in Python 3, and this release adds the new ip_addresses() <#hypothesis.strategies.ip_addresses> strategy to generate IPv4Addresses and/or IPv6Addresses (depending on the v and network arguments).
If you use them in type annotations, from_type() <#hypothesis.strategies.from_type> now has strategies registered for ipaddress address, network, and interface types.
The provisional strategies for IP address strings are therefore deprecated.
5.2.1 - 2020-01-21
This patch reverts version 5.2, due to a strange issue <https://github.com/numpy/numpy/issues/15363> where indexing an array of strings can raise an error instead of returning an item which contains certain surrogate characters.
5.2.0 - 2020-01-19
This release allows from_dtype() <#hypothesis.extra.numpy.from_dtype> to generate Unicode strings which cannot be encoded in UTF-8, but are valid in Numpy arrays (which use UTF-32).
5.1.6 - 2020-01-19
This patch fixes issue #2320 <https://github.com/HypothesisWorks/hypothesis/issues/2320>, where from_type(Set[Hashable]) could raise an internal error because Decimal("snan") is of a hashable type, but raises an error when hashed. We now ensure that set elements and dict keys in generic types can actually be hashed.
5.1.5 - 2020-01-12
This patch fixes an internal error when running in an ipython <https://pypi.org/project/ipython/> repl or jupyter <https://pypi.org/project/jupyter/> notebook on Windows (issue #2319 <https://github.com/HypothesisWorks/hypothesis/issues/2319>), and an internal error on Python 3.5.1 (issue #2318 <https://github.com/HypothesisWorks/hypothesis/issues/2318>).
5.1.4 - 2020-01-11
This patch fixes a bug where errors in third-party extensions such as hypothesis-trio <https://pypi.org/project/hypothesis-trio/> or hypothesis-jsonschema <https://pypi.org/project/hypothesis-jsonschema/> were incorrectly considered to be Hypothesis internal errors, which could result in confusing error messages.
Thanks to Vincent Michel for reporting and fixing the bug!
5.1.3 - 2020-01-11
This release converts the type hint comments on our public API to PEP 484 <https://peps.python.org/pep-0484/> type annotations.
Thanks to Ivan Levkivskyi for com2ann <https://pypi.org/project/com2ann/> - with the refactoring tools from 5.0.1 it made this process remarkably easy!
5.1.2 - 2020-01-09
This patch makes multiple() <#hypothesis.stateful.multiple> iterable, so that output like a, b = state.some_rule() is actually executable and can be used to reproduce failing examples.
Thanks to Vincent Michel for reporting and fixing issue #2311 <https://github.com/HypothesisWorks/hypothesis/issues/2311>!
5.1.1 - 2020-01-06
This patch contains many small refactorings to replace our Python 2 compatibility functions with their native Python 3 equivalents. Since Hypothesis is now Python 3 only, there is no user-visible change.
5.1.0 - 2020-01-03
This release teaches from_type() <#hypothesis.strategies.from_type> how to generate python:datetime.timezone. As a result, you can now generate python:datetime.tzinfo objects without having pytz <https://pypi.org/project/pytz/> installed.
If your tests specifically require pytz <https://pypi.org/project/pytz/> timezones, you should be using hypothesis.extra.pytz.timezones() <#hypothesis.extra.pytz.timezones> instead of st.from_type(tzinfo).
5.0.1 - 2020-01-01
This patch contains mostly-automated refactorings to remove code that we only needed to support Python 2. Since Hypothesis is now Python 3 only (hurray!), there is no user-visible change.
Our sincere thanks to the authors of autoflake <https://pypi.org/project/autoflake/>, black <https://pypi.org/project/black/>, isort <https://pypi.org/project/isort/>, and pyupgrade <https://pypi.org/project/pyupgrade/>, who have each and collectively made this kind of update enormously easier.
5.0.0 - 2020-01-01
Welcome to the next major version of Hypothesis!
There are no new features here, as we release those in minor versions. Instead, 5.0 is a chance for us to remove deprecated features (many already converted into no-ops), and turn a variety of warnings into errors.
If you were running on the last version of Hypothesis 4.x without any Hypothesis deprecation warnings, this will be a very boring upgrade. In fact, nothing will change for you at all.
Note:
This release drops support for Python 2, which has passed its end of life date <https://devguide.python.org/#status-of-python-branches>. The Python 3 Statement <https://python3statement.github.io> outlines our reasons, and lists many other packages that have made the same decision.
pip install hypothesis should continue to give you the latest compatible version. If you have somehow ended up with Hypothesis 5.0 on Python 2, you need to update your packaging stack to pip >= 9.0 and setuptools >= 24.2 - see here for details <https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires>. Then pip uninstall hypothesis && pip install hypothesis will get you back to a compatible version.
Strategies
- integers() <#hypothesis.strategies.integers> bounds must be equal to an integer, though they can still be other types.
- If fractions() <#hypothesis.strategies.fractions> is passed a max_denominator, the bounds must have at most that denominator.
- floats() <#hypothesis.strategies.floats> bounds must be exactly representable as a floating-point number with the given width. If not, the error message includes the nearest such number.
- sampled_from([]) <#hypothesis.strategies.sampled_from> is now an error.
- The values from the elements and fill strategies for hypothesis.extra.numpy.arrays() <#hypothesis.extra.numpy.arrays> must be losslessly representable in an array of the given dtype.
- The min_size and max_size arguments to all collection strategies must be of type python:int (or max_size may be None).
Miscellaneous
- The .example() method of strategies (intended for interactive exploration) no longer takes a random argument.
- It is now an error to apply @example <#hypothesis.example>, @seed <#hypothesis.seed>, or @reproduce_failure <#hypothesis.reproduce_failure> without also applying @given <#hypothesis.given>.
- You may pass either the target or targets argument to stateful rules, but not both.
- deadline <#hypothesis.settings.deadline> must be None (to disable), a timedelta, or an integer or float number of milliseconds.
- Both of derandomize <#hypothesis.settings.derandomize> and print_blob <#hypothesis.settings.print_blob> must be either True or False, where they previously accepted other values.
- stateful_step_count <#hypothesis.settings.stateful_step_count> must be at least one.
- max_examples <#hypothesis.settings.max_examples> must be at least one. To disable example generation, use the phases <#hypothesis.settings.phases> setting.
Removals
- hypothesis.stateful.GenericStateMachine in favor of hypothesis.stateful.RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>
- hypothesis.extra.django.models.models in favor of hypothesis.extra.django.from_model() <#hypothesis.extra.django.from_model> and hypothesis.extra.django.models.add_default_field_mapping in favor of hypothesis.extra.django.register_field_strategy() <#hypothesis.extra.django.register_field_strategy>
- hypothesis.HealthCheck.hung_test, without replacement
- hypothesis.settings.buffer, without replacement
- hypothesis.PrintSettings, because hypothesis.settings.print_blob <#hypothesis.settings.print_blob> takes True or False
- hypothesis.settings.timeout, in favor of hypothesis.settings.deadline <#hypothesis.settings.deadline>
- hypothesis.unlimited without replacement (only useful as argument to timeout)
Hypothesis 4.x
4.57.1 - 2019-12-29
This patch improves the type hints and documentation for the django extra. <#hypothesis-django> There is no runtime change.
4.57.0 - 2019-12-28
This release improves support for the SupportsOp protocols from the python:typing module when using on from_type() <#hypothesis.strategies.from_type> as outlined in issue #2292 <https://github.com/HypothesisWorks/hypothesis/issues/2292>. The following types now generate much more varied strategies when called with from_type() <#hypothesis.strategies.from_type>:
- python:typing.SupportsAbs
- python:typing.SupportsBytes
- python:typing.SupportsComplex
- python:typing.SupportsInt
- python:typing.SupportsFloat
- python:typing.SupportsRound
Note that using from_type() <#hypothesis.strategies.from_type> with one of the above strategies will not ensure that the specified function will execute successfully (ie : the strategy returned for from_type(typing.SupportsAbs) may include NaNs or things which cause the python:abs() function to error. )
Thanks to Lea Provenzano for this patch.
4.56.3 - 2019-12-22
This release fixes a small internal bug in shrinking which could have caused it to perform slightly more tests than were necessary. Fixing this shouldn't have much effect but it will make shrinking slightly faster.
4.56.2 - 2019-12-21
This release removes an internal heuristic that was no longer providing much benefit. It is unlikely that there will be any user visible effect.
4.56.1 - 2019-12-19
This release further improves the optimisation algorithm for targeted property-based testing <#targeted>.
4.56.0 - 2019-12-18
This release enables deprecation warnings even when the verbosity <#hypothesis.settings.verbosity> setting is quiet, in preparation for Hypothesis 5.0 (issue #2218 <https://github.com/HypothesisWorks/hypothesis/issues/2218>).
Warnings can still be filtered by the standard mechanisms provided in the standard-library python:warnings module.
4.55.4 - 2019-12-18
This release improves Hypothesis's management of the set of test cases it tracks between runs. It will only do anything if you have the target <#hypothesis.Phase.target> phase enabled and an example database set. In those circumstances it should result in a more thorough and faster set of examples that are tried on each run.
4.55.3 - 2019-12-18
This release makes Hypothesis better at generating test cases where generated values are duplicated in different parts of the test case. This will be especially noticeable with reasonably complex values, as it was already able to do this for simpler ones such as integers or floats.
4.55.2 - 2019-12-17
This release expands the set of test cases that Hypothesis saves in its database for future runs to include a representative set of "structurally different" test cases - e.g. it might try to save test cases where a given list is empty or not.
Currently this is unlikely to have much user visible impact except to produce slightly more consistent behaviour between consecutive runs of a test suite. It is mostly groundwork for future improvements which will exploit this functionality more effectively.
4.55.1 - 2019-12-16
This patch fixes issue #2257 <https://github.com/HypothesisWorks/hypothesis/issues/2257>, where from_type() <#hypothesis.strategies.from_type> could incorrectly generate bytestrings when passed a generic python:typing.Sequence such as Sequence[set].
4.55.0 - 2019-12-16
This release adds database support for targeted property-based testing <#targeted>, so the best examples based on the targeting will be saved and reused between runs. This is mostly laying groundwork for future features in this area, but will also make targeted property-based tests more useful during development, where the same tests tend to get run over and over again.
If max_examples <#hypothesis.settings.max_examples> is large, this may increase memory usage significantly under some circumstances, but these should be relatively rare.
This release also adds a dependency on the sortedcontainers <https://pypi.org/project/sortedcontainers/> package.
4.54.2 - 2019-12-16
This release improves the optimisation algorithm for targeted property-based testing <#targeted>, so that it will find higher quality results more reliably. Specifically, in cases where it would previously have got near a local optimum, it will now tend to achieve the locally optimal value.
4.54.1 - 2019-12-16
This release is mostly internal changes in support of better testing of the core engine. You are unlikely to see much effect, although some internal heuristics have changed slightly.
4.54.0 - 2019-12-15
This release adds a dedicated phase for targeted property-based testing <#targeted>, and (somewhat) improves the targeting algorithm so that it will find higher quality results more reliably. This comes at a cost of making it more likely to get stuck in a local optimum.
4.53.3 - 2019-12-15
This patch fixes from_type() <#hypothesis.strategies.from_type> with python:typing.Hashable and python:typing.Sized, which previously failed with an internal error on Python 3.7 or later.
Thanks to Lea Provenzano for both reporting issue #2272 <https://github.com/HypothesisWorks/hypothesis/issues/2272> and writing the patch!
4.53.2 - 2019-12-11
This release reorganises a number of the Hypothesis internal modules into a package structure. If you are only depending on the public API it should have no effect. If you are depending on the internal API (which you shouldn't be, and which we don't guarantee compatibility on) you may have to rename some imports.
4.53.1 - 2019-12-09
This release changes the size distribution of the number of steps run in stateful testing: It will now almost always run the maximum number of steps permitted.
4.53.0 - 2019-12-09
Test statistics <#statistics> now include the best score seen for each label, which can help avoid the threshold problem <https://hypothesis.works/articles/threshold-problem/> when the minimal example shrinks right down to the threshold of failure (issue #2180 <https://github.com/HypothesisWorks/hypothesis/issues/2180>).
4.52.0 - 2019-12-09
This release changes the stateful_step_count setting to raise an error if set to 0. This is a backwards compatible change because a value of 0 would never have worked and attempting to run it would have resulted in an internal assertion error.
4.51.1 - 2019-12-09
This release makes a small internal change to the distribution of test cases. It is unlikely to have much user visible impact.
4.51.0 - 2019-12-07
This release deprecates use of @example <#hypothesis.example>, @seed <#hypothesis.seed>, or @reproduce_failure <#hypothesis.reproduce_failure> without @given <#hypothesis.given>.
Thanks to Nick Anyos for the patch!
4.50.8 - 2019-12-05
This patch makes certain uses of Bundles more efficient in stateful testing (issue #2078 <https://github.com/HypothesisWorks/hypothesis/issues/2078>).
4.50.7 - 2019-12-05
This release refactors some of Hypothesis's internal interfaces for representing data generation. It should have no user visible effect.
4.50.6 - 2019-12-02
This patch removes some old debugging helpers in our Numpy extra which have not been needed since issue #1963 <https://github.com/HypothesisWorks/hypothesis/issues/1963> and issue #2245 <https://github.com/HypothesisWorks/hypothesis/issues/2245>.
4.50.5 - 2019-12-01
This patch fixes issue #2229 <https://github.com/HypothesisWorks/hypothesis/issues/2229>, where Numpy arrays of unsized strings would only ever have strings of size one due to an interaction between our generation logic and Numpy's allocation strategy.
4.50.4 - 2019-12-01
This patch fixes a rare internal error in strategies for a list of unique items sampled from a short non-unique sequence (issue #2247 <https://github.com/HypothesisWorks/hypothesis/issues/2247>). The bug was discovered via hypothesis-jsonschema <https://pypi.org/project/hypothesis-jsonschema/>.
4.50.3 - 2019-12-01
This release improves the error message when @settings <#hypothesis.settings> tries to inherit settings from a parent argument that isn't a settings instance.
4.50.2 - 2019-11-29
This release improves Hypothesis's "Falsifying example" output, by breaking output across multiple lines where necessary, and by removing irrelevant information from the stateful testing output.
4.50.1 - 2019-11-29
This patch adds flake8-comprehensions <https://pypi.org/project/flake8-comprehensions/> to our linter suite. There is no user-visible change - expect perhaps via some strange microbenchmarks - but certain parts of the code now have a clear and more consistent style.
4.50.0 - 2019-11-28
This release fixes some cases where we might previously have failed to run the validation logic for some strategies. As a result tests which would previously have been silently testing significantly less than they should may now start to raise InvalidArgument now that these errors are caught.
4.49.0 - 2019-11-28
This release significantly improves the data distribution in rule based stateful testing <#stateful>, by using a technique called Swarm Testing (Groce, Alex, et al. "Swarm testing." Proceedings of the 2012 International Symposium on Software Testing and Analysis. ACM, 2012.) <https://agroce.github.io/issta12.pdf> to select which rules are run in any given test case. This should allow it to find many issues that it would previously have missed.
This change is likely to be especially beneficial for stateful tests with large numbers of rules.
4.48.1 - 2019-11-28
This release adds some heuristics to test case generation that try to ensure that test cases generated early on will be relatively small.
This fixes a bug introduced in Hypothesis 4.42.0 which would cause occasional too_slow <#hypothesis.HealthCheck.too_slow> failures on some tests.
4.48.0 - 2019-11-28
This release revokes the deprecation of find, as we've now rebuilt it on top of @given, which means it has minimal maintenance burden and we're happy to support it.
4.47.5 - 2019-11-28
This release rebuilds find() on top of @given in order to have more code in common. It should have minimal user visible effect.
4.47.4 - 2019-11-27
This patch removes an internal compatibility shim that we no longer need.
4.47.3 - 2019-11-26
This patch fixes several typos in our docstrings and comments, with no change in behaviour. Thanks to Dmitry Dygalo for identifying and fixing them!
4.47.2 - 2019-11-25
This release fixes an internal issue where Hypothesis would sometimes generate test cases that were above its intended maximum size. This would only have happened rarely and probably would not have caused major problems when it did.
Users of the new targeted property-based testing <#targeted> might see minor impact (possibly slightly faster tests and slightly worse target scores), but only in the unlikely event that they were hitting this problem. Other users should not see any effect at all.
4.47.1 - 2019-11-24
This release removes some unused code from the core engine. There is no user-visible change.
4.47.0 - 2019-11-24
This release commonizes some code between running explicit examples and normal test execution. The main user visible impact of this is that deadlines are now enforced when running explicit examples.
4.46.1 - 2019-11-23
This patch ensures that a KeyboardInterrupt received during example generation is not treated as a mystery test failure but instead propagates to the top level, not recording the interrupted generation in the conjecture data tree. Thanks to Anne Archibald for identifying and fixing the problem.
4.46.0 - 2019-11-22
This release changes the behaviour of floats() <#hypothesis.strategies.floats> when excluding signed zeros - floats(max_value=0.0, exclude_max=True) can no longer generate -0.0 nor the much rarer floats(min_value=-0.0, exclude_min=True) generate +0.0.
The correct interaction between signed zeros and exclusive endpoints was unclear; we now enforce the invariant that floats() <#hypothesis.strategies.floats> will never generate a value equal to an excluded endpoint (issue #2201 <https://github.com/HypothesisWorks/hypothesis/issues/2201>).
If you prefer the old behaviour, you can pass floats(max_value=-0.0) or floats(min_value=0.0) which is exactly equivalent and has not changed. If you had two endpoints equal to zero, we recommend clarifying your tests by using just() <#hypothesis.strategies.just> or sampled_from() <#hypothesis.strategies.sampled_from> instead of floats() <#hypothesis.strategies.floats>.
4.45.1 - 2019-11-20
This patch improves the error message when invalid arguments are passed to rule() <#hypothesis.stateful.rule> or invariant() <#hypothesis.stateful.invariant> (issue #2149 <https://github.com/HypothesisWorks/hypothesis/issues/2149>).
Thanks to Benjamin Palmer for this bugfix!
4.45.0 - 2019-11-20
This release supports python:typing.Final and python:typing.TypedDict in from_type() <#hypothesis.strategies.from_type>.
4.44.5 - 2019-11-20
This patch disables our pytest <https://pypi.org/project/pytest/> plugin when running on versions of pytest <https://pypi.org/project/pytest/> before 4.3, the oldest our plugin supports. Note that at time of writing the Pytest developers only support 4.6 and later!
Hypothesis tests using @given() <#hypothesis.given> work on any test runner, but our integrations to e.g. avoid example database collisions when combined with @pytest.mark.parametrize eventually drop support for obsolete versions.
4.44.4 - 2019-11-20
This patch adds some internal comments and clarifications to the Hypothesis implementation. There is no user-visible change.
4.44.3 - 2019-11-20
This patch avoids importing test runners such as pytest <https://pypi.org/project/pytest/>, unittest2 <https://pypi.org/project/unittest2/>, or nose <https://pypi.org/project/nose/> solely to access their special "skip test" exception types - if the module is not in sys.modules, the exception can't be raised anyway.
This fixes a problem where importing an otherwise unused module could cause spurious errors due to import-time side effects (and possibly -Werror).
4.44.2 - 2019-11-12
This release fixes @given <#hypothesis.given> to only complain about missing keyword-only arguments if the associated test function is actually called.
This matches the behaviour of other InvalidArgument errors produced by @given.
4.44.1 - 2019-11-11
This patch allows Hypothesis to run in environments that do not specify a __file__, such as a python:zipapp (issue #2196 <https://github.com/HypothesisWorks/hypothesis/issues/2196>).
4.44.0 - 2019-11-11
This release adds a signature argument to mutually_broadcastable_shapes() <#hypothesis.extra.numpy.mutually_broadcastable_shapes> (issue #2174 <https://github.com/HypothesisWorks/hypothesis/issues/2174>), which allows us to generate shapes which are valid for functions like np.matmul() that require shapes which are not simply broadcastable.
Thanks to everyone who has contributed to this feature over the last year, and a particular shout-out to Zac Hatfield-Dodds and Ryan Soklaski for mutually_broadcastable_shapes() <#hypothesis.extra.numpy.mutually_broadcastable_shapes> and to Ryan Turner for the downstream hypothesis-gufunc <https://pypi.org/project/hypothesis-gufunc/> project.
4.43.9 - 2019-11-11
This patch fixes issue #2108 <https://github.com/HypothesisWorks/hypothesis/issues/2108>, where the first test using data() <#hypothesis.strategies.data> to draw from characters() <#hypothesis.strategies.characters> or text() <#hypothesis.strategies.text> would be flaky due to unreliable test timings.
Time taken by lazy instantiation of strategies is now counted towards drawing from the strategy, rather than towards the deadline for the test function.
4.43.8 - 2019-11-08
This release ensures that the strategies passed to @given <#hypothesis.given> are properly validated when applied to a test method inside a test class.
This should result in clearer error messages when some of those strategies are invalid.
4.43.7 - 2019-11-08
This release changes how Hypothesis manages its search space in cases where it generates redundant data. This should cause it to generate significantly fewer duplicated examples (especially with short integer ranges), and may cause it to produce more useful examples in some cases (especially ones where there is a significant amount of filtering).
4.43.6 - 2019-11-07
This patch refactors width handling in floats() <#hypothesis.strategies.floats>; you may notice small performance improvements but the main purpose is to enable work on issue #1704 <https://github.com/HypothesisWorks/hypothesis/issues/1704> (improving shrinking of bounded floats).
4.43.5 - 2019-11-06
This patch removes an unused internal flag. There is no user-visible change.
4.43.4 - 2019-11-05
This patch corrects the exception type and error message you get if you attempt to use data() <#hypothesis.strategies.data> to draw from something which is not a strategy. This never worked, but the error is more helpful now.
4.43.3 - 2019-11-05
We've adopted flake8-bugbear <https://pypi.org/project/flake8-bugbear/> to check for a few more style issues, and this patch implements the minor internal cleanups it suggested. There is no user-visible change.
4.43.2 - 2019-11-05
This patch fixes the formatting of some documentation, but there is no change to any executed code.
4.43.1 - 2019-11-04
Python 3.8's new python:typing.Literal type - see PEP 586 <https://peps.python.org/pep-0586/> for details - is now supported in from_type() <#hypothesis.strategies.from_type>.
4.43.0 - 2019-11-04
This release adds the strategy mutually_broadcastable_shapes() <#hypothesis.extra.numpy.mutually_broadcastable_shapes>, which generates multiple array shapes that are mutually broadcast-compatible with an optional user-specified base-shape.
This is a generalisation of broadcastable_shapes() <#hypothesis.extra.numpy.broadcastable_shapes>. It relies heavily on non-public internals for performance when generating and shrinking examples. We intend to support generating shapes matching a ufunc signature in a future version (issue #2174 <https://github.com/HypothesisWorks/hypothesis/issues/2174>).
Thanks to Ryan Soklaski, Zac Hatfield-Dodds, and @rdturnermtl who contributed to this new feature.
4.42.10 - 2019-11-03
This release fixes from_type() <#hypothesis.strategies.from_type> when used with bounded or constrained python:typing.TypeVar objects (issue #2094 <https://github.com/HypothesisWorks/hypothesis/issues/2094>).
Previously, distinct typevars with the same constraints would be treated as all single typevar, and in cases where a typevar bound was resolved to a union of subclasses this could result in mixed types being generated for that typevar.
4.42.9 - 2019-11-03
This patch ensures that the default value broadcastable_shapes() <#hypothesis.extra.numpy.broadcastable_shapes> chooses for max_dims is always valid (at most 32), even if you pass min_dims=32.
4.42.8 - 2019-11-02
This patch ensures that we only add profile information to the pytest header if running either pytest or Hypothesis in verbose mode, matching the builtin cache plugin <https://docs.pytest.org/en/latest/how-to/cache.html> (issue #2155 <https://github.com/HypothesisWorks/hypothesis/issues/2155>).
4.42.7 - 2019-11-02
This patch makes stateful step printing expand the result of a step into multiple variables when you return multiple() <#hypothesis.stateful.multiple> (issue #2139 <https://github.com/HypothesisWorks/hypothesis/issues/2139>). Thanks to Joseph Weston for reporting and fixing this bug!
4.42.6 - 2019-11-02
This release fixes a bug (issue #2166 <https://github.com/HypothesisWorks/hypothesis/issues/2166>) where a Unicode character info cache file was generated but never used on subsequent test runs, causing tests to run more slowly than they should have.
Thanks to Robert Knight for this bugfix!
4.42.5 - 2019-11-01
This patch corrects some internal documentation. There is no user-visible change.
4.42.4 - 2019-11-01
This release fixes a bug (issue #2160 <https://github.com/HypothesisWorks/hypothesis/issues/2160>) where decorators applied after @settings <#hypothesis.settings> and before @given <#hypothesis.given> were ignored.
Thanks to Tom Milligan for this bugfix!
4.42.3 - 2019-10-30
This release updates Hypothesis's formatting to the new version of black <https://pypi.org/project/black/>, and has absolutely no user visible effect.
4.42.2 - 2019-10-30
This release fixes a bug in recursive() <#hypothesis.strategies.recursive> which would have meant that in practice max_leaves was treated as if it was lower than it actually is - specifically it would be capped at the largest power of two smaller than it. It is now handled correctly.
4.42.1 - 2019-10-30
Python 3.8's new python:typing.SupportsIndex type - see PEP 357 <https://peps.python.org/pep-0357/> for details - is now supported in from_type() <#hypothesis.strategies.from_type>.
Thanks to Grigorios Giannakopoulos for the patch!
4.42.0 - 2019-10-27
This release significantly simplifies Hypothesis's internal logic for data generation, by removing a number of heuristics of questionable or unproven value.
The results of this change will vary significantly from test to test. Most test suites will see significantly faster data generation and lower memory usage. The "quality" of the generated data may go up or down depending on your particular test suites.
If you see any significant regressions in Hypothesis's ability to find bugs in your code as a result of this release, please file an issue to let us know.
Users of the new targeted property-based testing <#targeted> functionality are reasonably likely to see improvements in data generation, as this release changes the search algorithm for targeted property based testing to one that is more likely to be productive than the existing approach.
4.41.3 - 2019-10-21
This patch is to ensure that our internals remain comprehensible to mypy <https://pypi.org/project/mypy/> 0.740 - there is no user-visible change.
4.41.2 - 2019-10-17
This patch changes some internal hashes to SHA384, to better support users subject to FIPS-140. There is no user-visible API change.
Thanks to Paul Kehrer for this contribution!
4.41.1 - 2019-10-16
This release makes --hypothesis-show-statistics much more useful for tests using a RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>, by simplifying the reprs so that events are aggregated correctly.
4.41.0 - 2019-10-16
This release upgrades the fixed_dictionaries() <#hypothesis.strategies.fixed_dictionaries> strategy to support optional keys (issue #1913 <https://github.com/HypothesisWorks/hypothesis/issues/1913>).
4.40.2 - 2019-10-16
This release makes some minor internal changes in support of improving the Hypothesis test suite. It should not have any user visible impact.
4.40.1 - 2019-10-14
This release changes how Hypothesis checks if a parameter to a test function is a mock object. It is unlikely to have any noticeable effect, but may result in a small performance improvement, especially for test functions where a mock object is being passed as the first argument.
4.40.0 - 2019-10-09
This release fixes a bug where our example database logic did not distinguish between failing examples based on arguments from a @pytest.mark.parametrize(...). This could in theory cause data loss if a common failure overwrote a rare one, and in practice caused occasional file-access collisions in highly concurrent workloads (e.g. during a 300-way parametrize on 16 cores).
For internal reasons this also involves bumping the minimum supported version of pytest <https://pypi.org/project/pytest/> to 4.3
Thanks to Peter C Kroon for the Hacktoberfest patch!
4.39.3 - 2019-10-09
This patch improves our type hints on the emails() <#hypothesis.strategies.emails>, functions() <#hypothesis.strategies.functions>, integers() <#hypothesis.strategies.integers>, iterables() <#hypothesis.strategies.iterables>, and slices() <#hypothesis.strategies.slices> strategies, as well as the .filter() method.
There is no runtime change, but if you use mypy <https://pypi.org/project/mypy/> or a similar type-checker on your tests the results will be a bit more precise.
4.39.2 - 2019-10-09
This patch improves the performance of unique collections such as sets() <#hypothesis.strategies.sets> of just() <#hypothesis.strategies.just> or booleans() <#hypothesis.strategies.booleans> strategies. They were already pretty good though, so you're unlikely to notice much!
4.39.1 - 2019-10-09
If a value in a dict passed to fixed_dictionaries() <#hypothesis.strategies.fixed_dictionaries> is not a strategy, Hypothesis now tells you which one.
4.39.0 - 2019-10-07
This release adds the basic_indices() <#hypothesis.extra.numpy.basic_indices> strategy, to generate basic indexes <https://numpy.org/doc/stable/user/basics.indexing.html> for arrays of the specified shape (issue #1930 <https://github.com/HypothesisWorks/hypothesis/issues/1930>).
It generates tuples containing some mix of integers, python:slice objects, ... (Ellipsis), and numpy:numpy.newaxis; which when used to index an array of the specified shape produce either a scalar or a shared-memory view of the array. Note that the index tuple may be longer or shorter than the array shape, and may produce a view with another dimensionality again!
Thanks to Lampros Mountrakis, Ryan Soklaski, and Zac Hatfield-Dodds for their collaboration on this surprisingly subtle strategy!
4.38.3 - 2019-10-04
This patch defers creation of the .hypothesis directory until we have something to store in it, meaning that it will appear when Hypothesis is used rather than simply installed.
Thanks to Peter C Kroon for the Hacktoberfest patch!
4.38.2 - 2019-10-02
This patch bumps our dependency on attrs <https://pypi.org/project/attrs/> to >=19.2.0; but there are no user-visible changes to Hypothesis.
4.38.1 - 2019-10-01
This is a comment-only patch which tells mypy <https://pypi.org/project/mypy/> 0.730 to ignore some internal compatibility shims we use to support older Pythons.
4.38.0 - 2019-10-01
This release adds the hypothesis.target() <#hypothesis.target> function, which implements targeted property-based testing <#targeted> (issue #1779 <https://github.com/HypothesisWorks/hypothesis/issues/1779>).
By calling target() <#hypothesis.target> in your test function, Hypothesis can do a hill-climbing search for bugs. If you can calculate a suitable metric such as the load factor or length of a queue, this can help you find bugs with inputs that are highly improbably from unguided generation - however good our heuristics, example diversity, and deduplication logic might be. After all, those features are at work in targeted PBT too!
4.37.0 - 2019-09-28
This release emits a warning if you use the .example() method of a strategy in a non-interactive context.
given() <#hypothesis.given> is a much better choice for writing tests, whether you care about performance, minimal examples, reproducing failures, or even just the variety of inputs that will be tested!
4.36.2 - 2019-09-20
This patch disables part of the typing-based inference for the attrs <https://pypi.org/project/attrs/> package under Python 3.5.0, which has some incompatible internal details (issue #2095 <https://github.com/HypothesisWorks/hypothesis/issues/2095>).
4.36.1 - 2019-09-17
This patch fixes a bug in strategy inference for attrs <https://pypi.org/project/attrs/> classes where Hypothesis would fail to infer a strategy for attributes of a generic type such as Union[int, str] or List[bool] (issue #2091 <https://github.com/HypothesisWorks/hypothesis/issues/2091>).
Thanks to Jonathan Gayvallet for the bug report and this patch!
4.36.0 - 2019-09-09
This patch deprecates min_len or max_len of 0 in byte_string_dtypes() <#hypothesis.extra.numpy.byte_string_dtypes> and unicode_string_dtypes() <#hypothesis.extra.numpy.unicode_string_dtypes>. The lower limit is now 1.
Numpy uses a length of 0 in these dtypes to indicate an undetermined size, chosen from the data at array creation. However, as the arrays() <#hypothesis.extra.numpy.arrays> strategy creates arrays before filling them, strings were truncated to 1 byte.
4.35.1 - 2019-09-09
This patch improves the messaging that comes from invalid size arguments to collection strategies such as lists() <#hypothesis.strategies.lists>.
4.35.0 - 2019-09-04
This release improves the from_lark() <#hypothesis.extra.lark.from_lark> strategy, tightening argument validation and adding the explicit argument to allow use with terminals that use @declare instead of a string or regular expression.
This feature is required to handle features such as indent and dedent tokens in Python code, which can be generated with the hypothesmith <https://pypi.org/project/hypothesmith/> package.
4.34.0 - 2019-08-23
The from_type() <#hypothesis.strategies.from_type> strategy now knows to look up the subclasses of abstract types, which cannot be instantiated directly.
This is very useful for hypothesmith <https://pypi.org/project/hypothesmith/> to support libCST <https://pypi.org/project/libcst/>.
4.33.1 - 2019-08-21
This patch works around a crash when an incompatible version of Numpy is installed under PyPy 5.10 (Python 2.7).
If you are still using Python 2, please upgrade to Python 3 as soon as possible - it will be unsupported at the end of this year.
4.33.0 - 2019-08-20
This release improves the domains() <#hypothesis.provisional.domains> strategy, as well as the urls() <#hypothesis.provisional.urls> and the emails() <#hypothesis.strategies.emails> strategies which use it. These strategies now use the full IANA list of Top Level Domains and are correct as per RFC 1035 <https://datatracker.ietf.org/doc/html/rfc1035.html>.
Passing tests using these strategies may now fail.
Thanks to TechDragon <https://github.com/techdragon> for this improvement.
4.32.3 - 2019-08-05
This patch tidies up the repr of several settings-related objects, at runtime and in the documentation, and deprecates the undocumented edge case that phases=None was treated like phases=tuple(Phase).
It also fixes from_lark() <#hypothesis.extra.lark.from_lark> with lark 0.7.2 <https://pypi.org/project/lark-parser/> and later.
4.32.2 - 2019-07-30
This patch updates some internal comments for mypy <https://pypi.org/project/mypy/> 0.720. There is no user-visible impact.
4.32.1 - 2019-07-29
This release changes how the shrinker represents its progress internally. For large generated test cases this should result in significantly less memory usage and possibly faster shrinking. Small generated test cases may be slightly slower to shrink but this shouldn't be very noticeable.
4.32.0 - 2019-07-28
This release makes arrays() <#hypothesis.extra.numpy.arrays> more pedantic about elements strategies that cannot be exactly represented as array elements.
In practice, you will see new warnings if you were using a float16 or float32 dtype without passing floats() <#hypothesis.strategies.floats> the width=16 or width=32 arguments respectively.
The previous behaviour could lead to silent truncation, and thus some elements being equal to an explicitly excluded bound (issue #1899 <https://github.com/HypothesisWorks/hypothesis/issues/1899>).
4.31.1 - 2019-07-28
This patch changes an internal use of MD5 to SHA hashes, to better support users subject to FIPS-140. There is no user-visible or API change.
Thanks to Alex Gaynor for this patch.
4.31.0 - 2019-07-24
This release simplifies the logic of the print_blob <#hypothesis.settings.print_blob> setting by removing the option to set it to PrintSettings.INFER. As a result the print_blob setting now takes a single boolean value, and the use of PrintSettings is deprecated.
4.28.2 - 2019-07-14
This patch improves the docstrings of several Hypothesis strategies, by clarifying markup and adding cross-references. There is no runtime change.
Thanks to Elizabeth Williams and Serah Njambi Rono for their contributions at the SciPy 2019 sprints!
4.28.1 - 2019-07-12
This patch improves the behaviour of the text() <#hypothesis.strategies.text> strategy when passed an alphabet which is not a strategy. The value is now interpreted as include_characters to characters() <#hypothesis.strategies.characters> instead of a sequence for sampled_from() <#hypothesis.strategies.sampled_from>, which standardises the distribution of examples and the shrinking behaviour.
You can get the previous behaviour by using lists(sampled_from(alphabet)).map("".map) instead.
4.28.0 - 2019-07-11
This release deprecates find(). The .example() method is a better replacement if you want an example, and for the rare occasions where you want the minimal example you can get it from @given <#hypothesis.given>.
@given <#hypothesis.given> has steadily outstripped find() in both features and performance over recent years, and as we do not have the resources to maintain and test both we think it is better to focus on just one.
4.27.0 - 2019-07-08
This release refactors the implementation of the .example() method, to more accurately represent the data which will be generated by @given <#hypothesis.given>.
As a result, calling s.example() on an empty strategy s (such as nothing() <#hypothesis.strategies.nothing>) now raises Unsatisfiable instead of the old NoExamples exception.
4.26.4 - 2019-07-07
This patch ensures that the Pandas extra will keep working when Python 3.8 removes abstract base classes from the top-level python:collections namespace. This also fixes the relevant warning in Python 3.7, but there is no other difference in behaviour and you do not need to do anything.
4.26.3 - 2019-07-05
This release fixes issue #2027 <https://github.com/HypothesisWorks/hypothesis/issues/2027>, by changing the way Hypothesis tries to generate distinct examples to be more efficient.
This may result in slightly different data distribution, and should improve generation performance in general, but should otherwise have minimal user impact.
4.26.2 - 2019-07-04
This release fixes issue #1864 <https://github.com/HypothesisWorks/hypothesis/issues/1864>, where some simple tests would perform very slowly, because they would run many times with each subsequent run being progressively slower. They will now stop after a more reasonable number of runs without hitting this problem.
Unless you are hitting exactly this issue, it is unlikely that this release will have any effect, but certain classes of custom generators that are currently very slow may become a bit faster, or start to trigger health check failures.
4.26.1 - 2019-07-04
This release adds the strategy integer_array_indices() <#hypothesis.extra.numpy.integer_array_indices>, which generates tuples of Numpy arrays that can be used for advanced indexing <http://www.pythonlikeyoumeanit.com/Module3_IntroducingNumpy/AdvancedIndexing.html#Integer-Array-Indexing> to select an array of a specified shape.
4.26.0 - 2019-07-04
This release significantly improves the performance of drawing unique collections whose elements are drawn from sampled_from() <#hypothesis.strategies.sampled_from> strategies.
As a side effect, this detects an error condition that would previously have passed silently: When the min_size argument on a collection with distinct elements is greater than the number of elements being sampled, this will now raise an error.
4.25.1 - 2019-07-03
This release removes some defunct internal functionality that was only being used for testing. It should have no user visible impact.
4.25.0 - 2019-07-03
This release deprecates and disables the buffer_size setting, which should have been treated as a private implementation detail all along. We recommend simply deleting this settings argument.
4.24.6 - 2019-06-26
This patch makes datetimes() <#hypothesis.strategies.datetimes> more efficient, as it now handles short months correctly by construction instead of filtering.
4.24.5 - 2019-06-23
This patch improves the development experience by simplifying the tracebacks you will see when e.g. you have used the .map(...) method of a strategy and the mapped function raises an exception.
No new exceptions can be raised, nor existing exceptions change anything but their traceback. We're simply using if-statements rather than exceptions for control flow in a certain part of the internals!
4.24.4 - 2019-06-21
This patch fixes issue #2014 <https://github.com/HypothesisWorks/hypothesis/issues/2014>, where our compatibility layer broke with version 3.7.4 of the typing <https://pypi.org/project/typing/> module backport on PyPI.
This issue only affects Python 2. We remind users that Hypothesis, like many other packages, will drop Python 2 support on 2020-01-01 <https://python3statement.github.io> and already has several features that are only available on Python 3.
4.24.3 - 2019-06-07
This patch improves the implementation of an internal wrapper on Python 3.8 beta1 (and will break on the alphas; but they're not meant to be stable). On other versions, there is no change at all.
Thanks to Daniel Hahler for the patch, and Victor Stinner for his work on bpo-37032 <https://bugs.python.org/issue37032> that made it possible.
4.24.2 - 2019-06-06
Deprecation messages for functions in hypothesis.extra.django.models now explicitly name the deprecated function to make it easier to track down usages. Thanks to Kristian Glass for this contribution!
4.24.1 - 2019-06-04
This patch fixes issue #1999 <https://github.com/HypothesisWorks/hypothesis/issues/1999>, a spurious bug raised when a @st.composite <#hypothesis.strategies.composite> function was passed a keyword-only argument.
Thanks to Jim Nicholls for his fantastic bug report.
4.24.0 - 2019-05-29
This release deprecates GenericStateMachine, in favor of RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine>. Rule-based stateful testing is significantly faster, especially during shrinking.
If your use-case truly does not fit rule-based stateful testing, we recommend writing a custom test function which drives your specific control-flow using data() <#hypothesis.strategies.data>.
4.23.9 - 2019-05-28
This patch fixes a very rare example database issue with file permissions.
When running a test that uses both @given <#hypothesis.given> and pytest.mark.parametrize, using pytest-xdist <https://pypi.org/project/pytest-xdist/> on Windows, with failing examples in the database, two attempts to read a file could overlap and we caught FileNotFound but not other OSErrors.
4.23.8 - 2019-05-26
This patch has a minor cleanup of the internal engine. There is no user-visible impact.
4.23.7 - 2019-05-26
This patch clarifies some error messages when the test function signature is incompatible with the arguments to @given <#hypothesis.given>, especially when the @settings() <#hypothesis.settings> decorator is also used (issue #1978 <https://github.com/HypothesisWorks/hypothesis/issues/1978>).
4.23.6 - 2019-05-19
This release adds the pyupgrade <https://pypi.org/project/pyupgrade/> fixer to our code style, for consistent use of dict and set literals and comprehensions.
4.23.5 - 2019-05-16
This release slightly simplifies a small part of the core engine. There is no user-visible change.
4.23.4 - 2019-05-09
Fixes a minor formatting issue the docstring of from_type() <#hypothesis.strategies.from_type>
4.23.3 - 2019-05-09
Adds a recipe to the docstring of from_type() <#hypothesis.strategies.from_type> that describes a means for drawing values for "everything except" a specified type. This recipe is especially useful for writing tests that perform input-type validation.
4.23.2 - 2019-05-08
This patch uses autoflake <https://pypi.org/project/autoflake/> to remove some pointless pass statements, which improves our workflow but has no user-visible impact.
4.23.1 - 2019-05-08
This patch fixes an OverflowError in from_type(xrange) <#hypothesis.strategies.from_type> on Python 2.
It turns out that not only do the start and stop values have to fit in a C long, but so does stop - start. We now handle this even on 32bit platforms, but remind users that Python2 will not be supported after 2019 without specific funding.
4.23.0 - 2019-05-08
This release implements the slices() <#hypothesis.strategies.slices> strategy, to generate slices of a length-size sequence.
Thanks to Daniel J. West for writing this patch at the PyCon 2019 sprints!
4.22.3 - 2019-05-07
This patch exposes DataObject <#hypothesis.strategies.DataObject>, solely to support more precise type hints. Objects of this type are provided by data() <#hypothesis.strategies.data>, and can be used to draw examples from strategies intermixed with your test code.
4.22.2 - 2019-05-07
This patch fixes the very rare issue #1798 <https://github.com/HypothesisWorks/hypothesis/issues/1798> in array_dtypes() <#hypothesis.extra.numpy.array_dtypes>, which caused an internal error in our tests.
4.22.1 - 2019-05-07
This patch fixes a rare bug in from_type(range) <#hypothesis.strategies.from_type>.
Thanks to Zebulun Arendsee for fixing the bug at the PyCon 2019 Sprints.
4.22.0 - 2019-05-07
The unique_by argument to lists <#hypothesis.strategies.lists> now accepts a tuple of callables such that every element of the generated list will be unique with respect to each callable in the tuple (issue #1916 <https://github.com/HypothesisWorks/hypothesis/issues/1916>).
Thanks to Marco Sirabella for this feature at the PyCon 2019 sprints!
4.21.1 - 2019-05-06
This patch cleans up the internals of one_of() <#hypothesis.strategies.one_of>. You may see a slight change to the distribution of examples from this strategy but there is no change to the public API.
Thanks to Marco Sirabella for writing this patch at the PyCon 2019 sprints!
4.21.0 - 2019-05-05
The from_type() <#hypothesis.strategies.from_type> strategy now supports python:slice objects.
Thanks to Charlie El. Awbery for writing this feature at the PyCon 2019 Mentored Sprints <https://us.pycon.org/2019/hatchery/mentoredsprints/>.
4.20.0 - 2019-05-05
This release improves the array_shapes() <#hypothesis.extra.numpy.array_shapes> strategy, to choose an appropriate default for max_side based on the min_side, and max_dims based on the min_dims. An explicit error is raised for dimensions greater than 32, which are not supported by Numpy, as for other invalid combinations of arguments.
Thanks to Jenny Rouleau for writing this feature at the PyCon 2019 Mentored Sprints <https://us.pycon.org/2019/hatchery/mentoredsprints/>.
4.19.0 - 2019-05-05
The from_type() <#hypothesis.strategies.from_type> strategy now supports python:range objects (or xrange on Python 2).
Thanks to Katrina Durance for writing this feature at the PyCon 2019 Mentored Sprints <https://us.pycon.org/2019/hatchery/mentoredsprints/>.
4.18.3 - 2019-04-30
This release fixes a very rare edge case in the test-case mutator, which could cause an internal error with certain unusual tests.
4.18.2 - 2019-04-30
This patch makes Hypothesis compatible with the Python 3.8 alpha, which changed the representation of code objects to support positional-only arguments. Note however that Hypothesis does not (yet) support such functions as e.g. arguments to builds() <#hypothesis.strategies.builds> or inputs to @given <#hypothesis.given>.
Thanks to Paul Ganssle for identifying and fixing this bug.
4.18.1 - 2019-04-29
This patch improves the performance of unique collections such as sets() <#hypothesis.strategies.sets> when the elements are drawn from a sampled_from() <#hypothesis.strategies.sampled_from> strategy (issue #1115 <https://github.com/HypothesisWorks/hypothesis/issues/1115>).
4.18.0 - 2019-04-24
This release adds the functions() <#hypothesis.strategies.functions> strategy, which can be used to imitate your 'real' function for callbacks.
4.17.2 - 2019-04-19
This release refactors stateful rule selection to share the new machinery with sampled_from() <#hypothesis.strategies.sampled_from> instead of using the original independent implementation.
4.17.1 - 2019-04-16
This patch allows Hypothesis to try a few more examples after finding the first bug, in hopes of reporting multiple distinct bugs. The heuristics described in issue #847 <https://github.com/HypothesisWorks/hypothesis/issues/847> ensure that we avoid wasting time on fruitless searches, while still surfacing each bug as soon as possible.
4.17.0 - 2019-04-16
This release adds the strategy broadcastable_shapes() <#hypothesis.extra.numpy.broadcastable_shapes>, which generates array shapes that are broadcast-compatible <https://www.pythonlikeyoumeanit.com/Module3_IntroducingNumpy/Broadcasting.html#Rules-of-Broadcasting> with a provided shape.
4.16.0 - 2019-04-12
This release allows register_type_strategy() <#hypothesis.strategies.register_type_strategy> to be used with python:typing.NewType instances. This may be useful to e.g. provide only positive integers for from_type(UserId) <#hypothesis.strategies.from_type> with a UserId = NewType('UserId', int) type.
Thanks to PJCampi for suggesting and writing the patch!
4.15.0 - 2019-04-09
This release supports passing a timedelta as the deadline <#hypothesis.settings.deadline> setting, so you no longer have to remember that the number is in milliseconds (issue #1900 <https://github.com/HypothesisWorks/hypothesis/issues/1900>).
Thanks to Damon Francisco for this change!
4.14.7 - 2019-04-09
This patch makes the type annotations on hypothesis.extra.dateutil compatible with mypy <https://pypi.org/project/mypy/> 0.700.
4.14.6 - 2019-04-07
This release fixes a bug introduced in Hypothesis 4.14.3 that would sometimes cause sampled_from(...).filter(...) <#hypothesis.strategies.sampled_from> to encounter an internal assertion failure when there are three or fewer elements, and every element is rejected by the filter.
4.14.5 - 2019-04-05
This patch takes the previous efficiency improvements to sampled_from(...).filter(...) <#hypothesis.strategies.sampled_from> strategies that reject most elements, and generalises them to also apply to sampled_from(...).filter(...).filter(...) and longer chains of filters.
4.14.4 - 2019-04-05
This release fixes a bug that prevented random_module() <#hypothesis.strategies.random_module> from correctly restoring the previous state of the random module.
The random state was instead being restored to a temporary deterministic state, which accidentally caused subsequent tests to see the same random values across multiple test runs.
4.14.3 - 2019-04-03
This patch adds an internal special case to make sampled_from(...).filter(...) <#hypothesis.strategies.sampled_from> much more efficient when the filter rejects most elements (issue #1885 <https://github.com/HypothesisWorks/hypothesis/issues/1885>).
4.14.2 - 2019-03-31
This patch improves the error message if the function f in s.flatmap(f) <#hypothesis.strategies.SearchStrategy.flatmap> does not return a strategy.
Thanks to Kai Chen for this change!
4.14.1 - 2019-03-30
This release modifies how Hypothesis selects operations to run during shrinking, by causing it to deprioritise previously useless classes of shrink until others have reached a fixed point.
This avoids certain pathological cases where the shrinker gets very close to finishing and then takes a very long time to finish the last small changes because it tries many useless shrinks for each useful one towards the end. It also should cause a more modest improvement (probably no more than about 30%) in shrinking performance for most tests.
4.14.0 - 2019-03-19
This release blocks installation of Hypothesis on Python 3.4, which reached its end of life date on 2019-03-18 <https://peps.python.org/pep-0429/>.
This should not be of interest to anyone but downstream maintainers - if you are affected, migrate to a secure version of Python as soon as possible or at least seek commercial support.
4.13.0 - 2019-03-19
This release makes it an explicit error to call floats(min_value=inf, exclude_min=True) <#hypothesis.strategies.floats> or floats(max_value=-inf, exclude_max=True) <#hypothesis.strategies.floats>, as there are no possible values that can be generated (issue #1859 <https://github.com/HypothesisWorks/hypothesis/issues/1859>).
floats(min_value=0.0, max_value=-0.0) <#hypothesis.strategies.floats> is now deprecated. While 0. == -0. and we could thus generate either if comparing by value, violating the sequence ordering of floats is a special case we don't want or need.
4.12.1 - 2019-03-18
This release should significantly reduce the amount of memory that Hypothesis uses for representing large test cases, by storing information in a more compact representation and only unpacking it lazily when it is first needed.
4.12.0 - 2019-03-18
This update adds the report_multiple_bugs <#hypothesis.settings.report_multiple_bugs> setting, which you can use to disable multi-bug reporting and only raise whichever bug had the smallest minimal example. This is occasionally useful when using a debugger or tools that annotate tracebacks via introspection.
4.11.7 - 2019-03-18
This change makes a tiny improvement to the core engine's bookkeeping. There is no user-visible change.
4.11.6 - 2019-03-15
This release changes some of Hypothesis's internal shrinking behaviour in order to reduce memory usage and hopefully improve performance.
4.11.5 - 2019-03-13
This release adds a micro-optimisation to how Hypothesis handles debug reporting internally. Hard to shrink test may see a slight performance improvement, but in most common scenarios it is unlikely to be noticeable.
4.11.4 - 2019-03-13
This release removes some redundant code that was no longer needed but was still running a significant amount of computation and allocation on the hot path. This should result in a modest speed improvement for most tests, especially those with large test cases.
4.11.3 - 2019-03-13
This release adds a micro-optimisation to how Hypothesis caches test cases. This will cause a small improvement in speed and memory usage for large test cases, but in most common scenarios it is unlikely to be noticeable.
4.11.2 - 2019-03-13
This release removes some internal code that populates a field that is no longer used anywhere. This should result in some modest performance and speed improvements and no other user visible effects.
4.11.1 - 2019-03-13
This is a formatting-only patch, enabled by a new version of isort <https://pypi.org/project/isort/>.
4.11.0 - 2019-03-12
This release deprecates sampled_from() <#hypothesis.strategies.sampled_from> with empty sequences. This returns nothing() <#hypothesis.strategies.nothing>, which gives a clear error if used directly... but simply vanishes if combined with another strategy.
Tests that silently generate less than expected are a serious problem for anyone relying on them to find bugs, and we think reliability more important than convenience in this case.
4.10.0 - 2019-03-11
This release improves Hypothesis's to detect flaky tests, by noticing when the behaviour of the test changes between runs. In particular this will notice many new cases where data generation depends on external state (e.g. external sources of randomness) and flag those as flaky sooner and more reliably.
The basis of this feature is a considerable reengineering of how Hypothesis stores its history of test cases, so on top of this its memory usage should be considerably reduced.
4.9.0 - 2019-03-09
This release adds the strategy valid_tuple_axes() <#hypothesis.extra.numpy.valid_tuple_axes>, which generates tuples of axis-indices that can be passed to the axis argument in NumPy's sequential functions (e.g. numpy:numpy.sum()).
Thanks to Ryan Soklaski for this strategy.
4.8.0 - 2019-03-06
This release significantly tightens validation in hypothesis.settings <#hypothesis.settings>. max_examples <#hypothesis.settings.max_examples>, buffer_size, and stateful_step_count <#hypothesis.settings.stateful_step_count> must be positive integers; deadline <#hypothesis.settings.deadline> must be a positive number or None; and derandomize <#hypothesis.settings.derandomize> must be either True or False.
As usual, this replaces existing errors with a more helpful error and starts new validation checks as deprecation warnings.
4.7.19 - 2019-03-04
This release makes some micro-optimisations to certain calculations performed in the shrinker. These should particularly speed up large test cases where the shrinker makes many small changes. It will also reduce the amount allocated, but most of this is garbage that would have been immediately thrown away, so you probably won't see much effect specifically from that.
4.7.18 - 2019-03-03
This patch removes some overhead from arrays() <#hypothesis.extra.numpy.arrays> with a constant shape and dtype. The resulting performance improvement is modest, but worthwhile for small arrays.
4.7.17 - 2019-03-01
This release makes some micro-optimisations within Hypothesis's internal representation of test cases. This should cause heavily nested test cases to allocate less during generation and shrinking, which should speed things up slightly.
4.7.16 - 2019-02-28
This changes the order in which Hypothesis runs certain operations during shrinking. This should significantly decrease memory usage and speed up shrinking of large examples.
4.7.15 - 2019-02-28
This release allows Hypothesis to calculate a number of attributes of generated test cases lazily. This should significantly reduce memory usage and modestly improve performance, especially for large test cases.
4.7.14 - 2019-02-28
This release reduces the number of operations the shrinker will try when reordering parts of a test case. This should in some circumstances significantly speed up shrinking. It may result in different final test cases, and if so usually slightly worse ones, but it should not generally have much impact on the end result as the operations removed were typically useless.
4.7.13 - 2019-02-27
This release changes how Hypothesis reorders examples within a test case during shrinking. This should make shrinking considerably faster.
4.7.12 - 2019-02-27
This release slightly improves the shrinker's ability to replace parts of a test case with their minimal version, by allowing it to do so in bulk rather than one at a time. Where this is effective, shrinker performance should be modestly improved.
4.7.11 - 2019-02-25
This release makes some micro-optimisations to common operations performed during shrinking. Shrinking should now be slightly faster, especially for large examples with relatively fast test functions.
4.7.10 - 2019-02-25
This release is a purely internal refactoring of Hypothesis's API for representing test cases. There should be no user visible effect.
4.7.9 - 2019-02-24
This release changes certain shrink passes to make them more efficient when they aren't making progress.
4.7.8 - 2019-02-23
This patch removes some unused code, which makes the internals a bit easier to understand. There is no user-visible impact.
4.7.7 - 2019-02-23
This release reduces the number of operations the shrinker will try when reordering parts of a test case. This should in some circumstances significantly speed up shrinking. It may result in different final test cases, and if so usually slightly worse ones, but it should not generally have much impact on the end result as the operations removed were typically useless.
4.7.6 - 2019-02-23
This patch removes some unused code from the shrinker. There is no user-visible change.
4.7.5 - 2019-02-23
This release changes certain shrink passes to make them adaptive - that is, in cases where they are successfully making progress they may now do so significantly faster.
4.7.4 - 2019-02-22
This is a docs-only patch, noting that because the lark-parser <https://pypi.org/project/lark-parser/> is under active development at version 0.x, hypothesis[lark] APIs may break in minor releases if necessary to keep up with the upstream package.
4.7.3 - 2019-02-22
This changes Hypothesis to no longer import various test frameworks by default (if they are installed). which will speed up the initial import hypothesis call.
4.7.2 - 2019-02-22
This release changes Hypothesis's internal representation of a test case to calculate some expensive structural information on demand rather than eagerly. This should reduce memory usage a fair bit, and may make generation somewhat faster.
4.7.1 - 2019-02-21
This release refactors the internal representation of previously run test cases. The main thing you should see as a result is that Hypothesis becomes somewhat less memory hungry.
4.7.0 - 2019-02-21
This patch allows array_shapes() <#hypothesis.extra.numpy.array_shapes> to generate shapes with side-length or even dimension zero, though the minimum still defaults to one. These shapes are rare and have some odd behavior, but are particularly important to test for just that reason!
In a related bigfix, arrays() <#hypothesis.extra.numpy.arrays> now supports generating zero-dimensional arrays with dtype=object and a strategy for iterable elements. Previously, the array element would incorrectly be set to the first item in the generated iterable.
Thanks to Ryan Turner for continuing to improve our Numpy support.
4.6.1 - 2019-02-19
This release is a trivial micro-optimisation inside Hypothesis which should result in it using significantly less memory.
4.6.0 - 2019-02-18
This release changes some inconsistent behavior of arrays() <#hypothesis.extra.numpy.arrays> from the Numpy extra when asked for an array of shape=(). arrays() <#hypothesis.extra.numpy.arrays> will now always return a Numpy ndarray, and the array will always be of the requested dtype.
Thanks to Ryan Turner for this change.
4.5.12 - 2019-02-18
This release fixes a minor typo in an internal comment. There is no user-visible change.
4.5.11 - 2019-02-15
This release fixes issue #1813 <https://github.com/HypothesisWorks/hypothesis/issues/1813>, a bug introduced in 3.59.1, which caused random_module() <#hypothesis.strategies.random_module> to no longer affect the body of the test: Although Hypothesis would claim to be seeding the random module in fact tests would always run with a seed of zero.
4.5.10 - 2019-02-14
This patch fixes an off-by-one error in the maximum length of emails() <#hypothesis.strategies.emails>. Thanks to Krzysztof Jurewicz for pull request #1812 <https://github.com/HypothesisWorks/hypothesis/pull/1812>.
4.5.9 - 2019-02-14
This patch removes some unused code from the shrinker. There is no user-visible change.
4.5.8 - 2019-02-12
This release fixes an internal IndexError in Hypothesis that could sometimes be triggered during shrinking.
4.5.7 - 2019-02-11
This release modifies the shrinker to interleave different types of reduction operations, e.g. switching between deleting data and lowering scalar values rather than trying entirely deletions then entirely lowering.
This may slow things down somewhat in the typical case, but has the major advantage that many previously difficult to shrink examples should become much faster, because the shrinker will no longer tend to stall when trying some ineffective changes to the shrink target but will instead interleave it with other more effective operations.
4.5.6 - 2019-02-11
This release makes a number of internal changes to the implementation of hypothesis.extra.lark.from_lark() <#hypothesis.extra.lark.from_lark>. These are primarily intended as a refactoring, but you may see some minor improvements to performance when generating large strings, and possibly to shrink quality.
4.5.5 - 2019-02-10
This patch prints an explanatory note when issue #1798 <https://github.com/HypothesisWorks/hypothesis/issues/1798> is triggered, because the error message from Numpy is too terse to locate the problem.
4.5.4 - 2019-02-08
In Python 2, long integers are not allowed in the shape argument to arrays() <#hypothesis.extra.numpy.arrays>. Thanks to Ryan Turner for fixing this.
4.5.3 - 2019-02-08
This release makes a small internal refactoring to clarify how Hypothesis instructs tests to stop running when appropriate. There is no user-visible change.
4.5.2 - 2019-02-06
This release standardises all of the shrinker's internal operations on running in a random order.
The main effect you will see from this that it should now be much less common for the shrinker to stall for a long time before making further progress. In some cases this will correspond to shrinking more slowly, but on average it should result in faster shrinking.
4.5.1 - 2019-02-05
This patch updates some docstrings, but has no runtime changes.
4.5.0 - 2019-02-03
This release adds exclude_min and exclude_max arguments to floats() <#hypothesis.strategies.floats>, so that you can easily generate values from open or half-open intervals <https://en.wikipedia.org/wiki/Interval_(mathematics)> (issue #1622 <https://github.com/HypothesisWorks/hypothesis/issues/1622>).
4.4.6 - 2019-02-03
This patch fixes a bug where from_regex() <#hypothesis.strategies.from_regex> could throw an internal error if the python:re.IGNORECASE flag was used (issue #1786 <https://github.com/HypothesisWorks/hypothesis/issues/1786>).
4.4.5 - 2019-02-02
This release removes two shrink passes that Hypothesis runs late in the process. These were very expensive when the test function was slow and often didn't do anything useful.
Shrinking should get faster for most failing tests. If you see any regression in example quality as a result of this release, please let us know.
4.4.4 - 2019-02-02
This release modifies the way that Hypothesis deletes data during shrinking. It will primarily be noticeable for very large examples, which should now shrink faster.
The shrinker is now also able to perform some deletions that it could not previously, but this is unlikely to be very noticeable.
4.4.3 - 2019-01-25
This release fixes an open file leak that used to cause ResourceWarnings.
4.4.2 - 2019-01-24
This release changes Hypothesis's internal approach to caching the results of executing test cases. The result should be that it is now significantly less memory hungry, especially when shrinking large test cases.
Some tests may get slower or faster depending on whether the new or old caching strategy was well suited to them, but any change in speed in either direction should be minor.
4.4.1 - 2019-01-24
This patch tightens up some of our internal heuristics to deal with shrinking floating point numbers, which will now run in fewer circumstances.
You are fairly unlikely to see much difference from this, but if you do you are likely to see shrinking become slightly faster and/or producing slightly worse results.
4.4.0 - 2019-01-24
This release adds the from_form() <#hypothesis.extra.django.from_form> function, which allows automatic testing against Django forms. (issue #35 <https://github.com/HypothesisWorks/hypothesis/issues/35>)
Thanks to Paul Stiverson for this feature, which resolves our oldest open issue!
4.3.0 - 2019-01-24
This release deprecates HealthCheck.hung_test and disables the associated runtime check for tests that ran for more than five minutes. Such a check is redundant now that we enforce the deadline and max_examples setting, which can be adjusted independently.
4.2.0 - 2019-01-23
This release adds a new module, hypothesis.extra.lark, which you can use to generate strings matching a context-free grammar.
In this initial version, only lark-parser <https://pypi.org/project/lark-parser/> EBNF grammars are supported, by the new hypothesis.extra.lark.from_lark() <#hypothesis.extra.lark.from_lark> function.
4.1.2 - 2019-01-23
This patch fixes a very rare overflow bug (issue #1748 <https://github.com/HypothesisWorks/hypothesis/issues/1748>) which could raise an InvalidArgument error in complex_numbers() <#hypothesis.strategies.complex_numbers> even though the arguments were valid.
4.1.1 - 2019-01-23
This release makes some improvements to internal code organisation and documentation and has no impact on behaviour.
4.1.0 - 2019-01-22
This release adds register_random() <#hypothesis.register_random>, which registers random.Random instances or compatible objects to be seeded and reset by Hypothesis to ensure that test cases are deterministic.
We still recommend explicitly passing a random.Random instance from randoms() <#hypothesis.strategies.randoms> if possible, but registering a framework-global state for Hypothesis to manage is better than flaky tests!
4.0.2 - 2019-01-22
This patch fixes issue #1387 <https://github.com/HypothesisWorks/hypothesis/issues/1387>, where bounded integers() <#hypothesis.strategies.integers> with a very large range would almost always generate very large numbers. Now, we usually use the same tuned distribution as unbounded integers() <#hypothesis.strategies.integers>.
4.0.1 - 2019-01-16
This release randomizes the order in which the shrinker tries some of its initial normalization operations. You are unlikely to see much difference as a result unless your generated examples are very large. In this case you may see some performance improvements in shrinking.
4.0.0 - 2019-01-14
Welcome to the next major version of Hypothesis!
There are no new features here, as we release those in minor versions. Instead, 4.0 is a chance for us to remove deprecated features (many already converted into no-ops), and turn a variety of warnings into errors.
If you were running on the last version of Hypothesis 3.x without any Hypothesis deprecation warnings (or using private APIs), this will be a very boring upgrade. In fact, nothing will change for you at all. Per our deprecation policy <#deprecation-policy>, warnings added in the last six months (after 2018-07-05) have not been converted to errors.
Removals
- hypothesis.extra.datetime has been removed, replaced by the core date and time strategies.
- hypothesis.extra.fakefactory has been removed, replaced by general expansion of Hypothesis' strategies and the third-party ecosystem.
- The SQLite example database backend has been removed.
Settings
- The deadline <#hypothesis.settings.deadline> is now enforced by default, rather than just emitting a warning when the default (200 milliseconds per test case) deadline is exceeded.
- The database_file setting has been removed; use database <#hypothesis.settings.database>.
- The perform_health_check setting has been removed; use suppress_health_check <#hypothesis.settings.suppress_health_check>.
- The max_shrinks setting has been removed; use phases <#hypothesis.settings.phases> to disable shrinking.
- The min_satisfying_examples, max_iterations, strict, timeout, and use_coverage settings have been removed without user-configurable replacements.
Strategies
- The elements argument is now required for collection strategies.
- The average_size argument was a no-op and has been removed.
- Date and time strategies now only accept min_value and max_value for bounds.
- builds() <#hypothesis.strategies.builds> now requires that the thing to build is passed as the first positional argument.
- Alphabet validation for text() <#hypothesis.strategies.text> raises errors, not warnings, as does category validation for characters() <#hypothesis.strategies.characters>.
- The choices() strategy has been removed. Instead, you can use data() <#hypothesis.strategies.data> with sampled_from() <#hypothesis.strategies.sampled_from>, so choice(elements) becomes data.draw(sampled_from(elements)).
- The streaming() strategy has been removed. Instead, you can use data() <#hypothesis.strategies.data> and replace iterating over the stream with data.draw() calls.
- sampled_from() <#hypothesis.strategies.sampled_from> and permutations() <#hypothesis.strategies.permutations> raise errors instead of warnings if passed a collection that is not a sequence.
Miscellaneous
- Applying @given <#hypothesis.given> to a test function multiple times was really inefficient, and now it's also an error.
- Using the .example() method of a strategy (intended for interactive exploration) within another strategy or a test function always weakened data generation and broke shrinking, and now it's an error too.
- The HYPOTHESIS_DATABASE_FILE environment variable is no longer supported, as the database_file setting has been removed.
- The HYPOTHESIS_VERBOSITY_LEVEL environment variable is no longer supported. You can use the --hypothesis-verbosity pytest argument instead, or write your own setup code using the settings profile system to replace it.
- Using @seed <#hypothesis.seed> or derandomize=True <#hypothesis.settings.derandomize> now forces database=None <#hypothesis.settings.database> to ensure results are in fact reproducible. If database <#hypothesis.settings.database> is not None, doing so also emits a HypothesisWarning.
- Unused exception types have been removed from hypothesis.errors; namely AbnormalExit, BadData, BadTemplateDraw, DefinitelyNoSuchExample, Timeout, and WrongFormat.
Hypothesis 3.x
3.88.3 - 2019-01-11
This changes the order that the shrinker tries certain operations in its "emergency" phase which runs late in the process. The new order should be better at avoiding long stalls where the shrinker is failing to make progress, which may be helpful if you have difficult to shrink test cases. However this will not be noticeable in the vast majority of use cases.
3.88.2 - 2019-01-11
This is a pure refactoring release that extracts some logic from the core Hypothesis engine into its own class and file. It should have no user visible impact.
3.88.1 - 2019-01-11
This patch fixes some markup in our documentation.
3.88.0 - 2019-01-10
Introduces hypothesis.stateful.multiple() <#hypothesis.stateful.multiple>, which allows rules in rule based state machines to send multiple results at once to their target Bundle, or none at all.
3.87.0 - 2019-01-10
This release contains a massive cleanup of the Hypothesis for Django extra:
- hypothesis.extra.django.models.models() is deprecated in favor of hypothesis.extra.django.from_model() <#hypothesis.extra.django.from_model>.
- hypothesis.extra.django.models.add_default_field_mapping() is deprecated in favor of hypothesis.extra.django.register_field_strategy() <#hypothesis.extra.django.register_field_strategy>.
- from_model() <#hypothesis.extra.django.from_model> does not infer a strategy for nullable fields or fields with a default unless passed infer, like builds() <#hypothesis.strategies.builds>. models.models() would usually but not always infer, and a special default_value marker object was required to disable inference.
3.86.9 - 2019-01-09
This release improves some internal logic about when a test case in Hypothesis's internal representation could lead to a valid test case. In some circumstances this can lead to a significant speed up during shrinking. It may have some minor negative impact on the quality of the final result due to certain shrink passes now having access to less information about test cases in some circumstances, but this should rarely matter.
3.86.8 - 2019-01-09
This release has no user visible changes but updates our URLs to use HTTPS.
3.86.7 - 2019-01-08
Hypothesis can now automatically generate values for Django models with a URLField, thanks to a new provisional strategy for URLs (issue #1388 <https://github.com/HypothesisWorks/hypothesis/issues/1388>).
3.86.6 - 2019-01-07
This release is a pure refactoring that extracts some internal code into its own file. It should have no user visible effect.
3.86.5 - 2019-01-06
This is a docs-only patch, which fixes some typos and removes a few hyperlinks for deprecated features.
3.86.4 - 2019-01-04
This release changes the order in which the shrinker tries to delete data. For large and slow tests this may significantly improve the performance of shrinking.
3.86.3 - 2019-01-04
This release fixes a bug where certain places Hypothesis internal errors could be raised during shrinking when a user exception occurred that suppressed an exception Hypothesis uses internally in its generation.
The two known ways to trigger this problem were:
- Errors raised in stateful tests' teardown function.
- Errors raised in finally blocks that wrapped a call to data.draw.
These cases will now be handled correctly.
3.86.2 - 2019-01-04
This patch is a docs-only change to fix a broken hyperlink.
3.86.1 - 2019-01-04
This patch fixes issue #1732 <https://github.com/HypothesisWorks/hypothesis/issues/1732>, where integers() <#hypothesis.strategies.integers> would always return long values on Python 2.
3.86.0 - 2019-01-03
This release ensures that infinite numbers are never generated by floats() <#hypothesis.strategies.floats> with allow_infinity=False, which could previously happen in some cases where one bound was also provided.
The trivially inconsistent min_value=inf, allow_infinity=False now raises an InvalidArgumentError, as does the inverse with max_value. You can still use just(inf) <#hypothesis.strategies.just> to generate inf without violating other constraints.
3.85.3 - 2019-01-02
Happy new year everyone! This release has no user visible changes but updates our copyright headers to include 2019.
3.85.2 - 2018-12-31
This release makes a small change to the way the shrinker works. You may see some improvements to speed of shrinking on especially large and hard to shrink examples, but most users are unlikely to see much difference.
3.85.1 - 2018-12-30
This patch fixes issue #1700 <https://github.com/HypothesisWorks/hypothesis/issues/1700>, where a line that contained a Unicode character before a lambda definition would cause an internal exception.
3.85.0 - 2018-12-29
Introduces the hypothesis.stateful.consumes() <#hypothesis.stateful.consumes> function. When defining a rule in stateful testing, it can be used to mark bundles from which values should be consumed, i. e. removed after use in the rule. This has been proposed in issue #136 <https://github.com/HypothesisWorks/hypothesis/issues/136>.
Thanks to Jochen Müller for this long-awaited feature.
3.84.6 - 2018-12-28
This patch makes a small internal change to fix an issue in Hypothesis's own coverage tests (issue #1718 <https://github.com/HypothesisWorks/hypothesis/issues/1718>).
There is no user-visible change.
3.84.5 - 2018-12-21
This patch refactors the hypothesis.strategies module, so that private names should no longer appear in tab-completion lists. We previously relied on __all__ for this, but not all editors respect it.
3.84.4 - 2018-12-21
This is a follow-up patch to ensure that the deprecation date is automatically recorded for any new deprecations. There is no user-visible effect.
3.84.3 - 2018-12-20
This patch updates the Hypothesis pytest plugin to avoid a recently deprecated hook interface. There is no user-visible change.
3.84.2 - 2018-12-19
This patch fixes the internals for integers() <#hypothesis.strategies.integers> with one bound. Values from this strategy now always shrink towards zero instead of towards the bound, and should shrink much more efficiently too. On Python 2, providing a bound incorrectly excluded long integers, which can now be generated.
3.84.1 - 2018-12-18
This patch adds information about when features were deprecated, but this is only recorded internally and has no user-visible effect.
3.84.0 - 2018-12-18
This release changes the stateful testing backend from find() to use @given <#hypothesis.given> (issue #1300 <https://github.com/HypothesisWorks/hypothesis/issues/1300>). This doesn't change how you create stateful tests, but does make them run more like other Hypothesis tests.
@reproduce_failure <#hypothesis.reproduce_failure> and @seed <#hypothesis.seed> now work for stateful tests.
Stateful tests now respect the deadline <#hypothesis.settings.deadline> and suppress_health_check <#hypothesis.settings.suppress_health_check> settings, though they are disabled by default. You can enable them by using @settings(...) <#hypothesis.settings> as a class decorator with whatever arguments you prefer.
3.83.2 - 2018-12-17
Hypothesis has adopted black <https://pypi.org/project/black/> as our code formatter (issue #1686 <https://github.com/HypothesisWorks/hypothesis/issues/1686>). There are no functional changes to the source, but it's prettier!
3.83.1 - 2018-12-13
This patch increases the variety of examples generated by from_type() <#hypothesis.strategies.from_type>.
3.83.0 - 2018-12-12
Our pytest plugin now warns you when strategy functions have been collected as tests, which may happen when e.g. using the @composite <#hypothesis.strategies.composite> decorator when you should be using @given(st.data()) for inline draws. Such functions always pass when treated as tests, because the lazy creation of strategies mean that the function body is never actually executed!
3.82.6 - 2018-12-11
Hypothesis can now show statistics <#statistics> when running under pytest-xdist <https://pypi.org/project/pytest-xdist/>. Previously, statistics were only reported when all tests were run in a single process (issue #700 <https://github.com/HypothesisWorks/hypothesis/issues/700>).
3.82.5 - 2018-12-08
This patch fixes issue #1667 <https://github.com/HypothesisWorks/hypothesis/issues/1667>, where passing bounds of Numpy dtype int64 to integers() <#hypothesis.strategies.integers> could cause errors on Python 3 due to internal rounding.
3.82.4 - 2018-12-08
Hypothesis now seeds and resets the global state of np.random for each test case, to ensure that tests are reproducible.
This matches and complements the existing handling of the python:random module - Numpy simply maintains an independent PRNG for performance reasons.
3.82.3 - 2018-12-08
This is a no-op release to add the new Framework :: Hypothesis trove classifier <https://pypi.org/classifiers/> to hypothesis <https://pypi.org/project/hypothesis/> on PyPI.
You can use it as a filter <https://pypi.org/search/?c=Framework+%3A%3A+Hypothesis> to find Hypothesis-related packages such as extensions as they add the tag over the coming weeks, or simply visit our curated list <>.
3.82.2 - 2018-12-08
The Hypothesis for Pandas extension <#hypothesis-pandas> is now listed in setup.py, so you can pip install hypothesis[pandas]. Thanks to jmshi for this contribution.
3.82.1 - 2018-10-29
This patch fixes from_type() <#hypothesis.strategies.from_type> on Python 2 for classes where cls.__init__ is object.__init__. Thanks to ccxcz for reporting issue #1656 <https://github.com/HypothesisWorks/hypothesis/issues/1656>.
3.82.0 - 2018-10-29
The alphabet argument for text() <#hypothesis.strategies.text> now uses its default value of characters(exclude_categories=('Cs',)) directly, instead of hiding that behind alphabet=None and replacing it within the function. Passing None is therefore deprecated.
3.81.0 - 2018-10-27
GenericStateMachine and RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine> now raise an explicit error when instances of settings <#hypothesis.settings> are assigned to the classes' settings attribute, which is a no-op (issue #1643 <https://github.com/HypothesisWorks/hypothesis/issues/1643>). Instead assign to SomeStateMachine.TestCase.settings, or use @settings(...) as a class decorator to handle this automatically.
3.80.0 - 2018-10-25
Since version 3.68.0, arrays() <#hypothesis.extra.numpy.arrays> checks that values drawn from the elements and fill strategies can be safely cast to the dtype of the array, and emits a warning otherwise.
This release expands the checks to cover overflow for finite complex64 elements and string truncation caused by too-long elements or trailing null characters (issue #1591 <https://github.com/HypothesisWorks/hypothesis/issues/1591>).
3.79.4 - 2018-10-25
Tests using @given <#hypothesis.given> now shrink errors raised from pytest <https://pypi.org/project/pytest/> helper functions, instead of reporting the first example found.
This was previously fixed in version 3.56.0, but only for stateful testing.
3.79.3 - 2018-10-23
Traceback elision is now disabled on Python 2, to avoid an import-time python:SyntaxError under Python < 2.7.9 (Python: bpo-21591 <https://bugs.python.org/issue21591>, Hypothesis 3.79.2: issue #1648 <https://github.com/HypothesisWorks/hypothesis/issues/1648>).
We encourage all users to upgrade to Python 3 before the end of 2019 <https://pythonclock.org/>.
3.79.2 - 2018-10-23
This patch shortens tracebacks from Hypothesis, so you can see exactly happened in your code without having to skip over irrelevant details about our internals (issue #848 <https://github.com/HypothesisWorks/hypothesis/issues/848>).
In the example test (see pull request #1582 <https://github.com/HypothesisWorks/hypothesis/pull/1582>), this reduces tracebacks from nine frames to just three - and for a test with multiple errors, from seven frames per error to just one!
If you do want to see the internal details, you can disable frame elision by setting verbosity <#hypothesis.settings.verbosity> to debug.
3.79.1 - 2018-10-22
The abstract number classes Number, Complex, Real, Rational, and Integral are now supported by the from_type() <#hypothesis.strategies.from_type> strategy. Previously, you would have to use register_type_strategy() <#hypothesis.strategies.register_type_strategy> before they could be resolved (issue #1636 <https://github.com/HypothesisWorks/hypothesis/issues/1636>)
3.79.0 - 2018-10-18
This release adds a CLI flag for verbosity --hypothesis-verbosity to the Hypothesis pytest plugin, applied after loading the profile specified by --hypothesis-profile. Valid options are the names of verbosity settings, quiet, normal, verbose or debug.
Thanks to Bex Dunn for writing this patch at the PyCon Australia sprints!
The pytest header now correctly reports the current profile if --hypothesis-profile has been used.
Thanks to Mathieu Paturel for the contribution at the Canberra Python Hacktoberfest.
3.78.0 - 2018-10-16
This release has deprecated the generation of integers, floats and fractions when the conversion of the upper and/ or lower bound is not 100% exact, e.g. when an integer gets passed a bound that is not a whole number. (issue #1625 <https://github.com/HypothesisWorks/hypothesis/issues/1625>)
Thanks to Felix Grünewald for this patch during Hacktoberfest 2018.
3.77.0 - 2018-10-16
This minor release adds functionality to settings <#hypothesis.settings> allowing it to be used as a decorator on RuleBasedStateMachine <#hypothesis.stateful.RuleBasedStateMachine> and GenericStateMachine.
Thanks to Tyler Nickerson for this feature in #hacktoberfest!
3.76.1 - 2018-10-16
This patch fixes some warnings added by recent releases of pydocstyle <https://pypi.org/project/pydocstyle/> and mypy <https://pypi.org/project/mypy/>.
3.76.0 - 2018-10-11
This release deprecates using floats for min_size and max_size.
The type hint for average_size arguments has been changed from Optional[int] to None, because non-None values are always ignored and deprecated.
3.75.4 - 2018-10-10
This patch adds more internal comments to the core engine's sequence-length shrinker. There should be no user-visible change.
3.75.3 - 2018-10-09
This patch adds additional comments to some of the core engine's internal data structures. There is no user-visible change.
3.75.2 - 2018-10-09
This patch avoids caching a trivial case, fixing issue #493 <https://github.com/HypothesisWorks/hypothesis/issues/493>.
3.75.1 - 2018-10-09
This patch fixes a broken link in a docstring. Thanks to Benjamin Lee for this contribution!
3.75.0 - 2018-10-08
This release deprecates the use of min_size=None, setting the default min_size to 0 (issue #1618 <https://github.com/HypothesisWorks/hypothesis/issues/1618>).
3.74.3 - 2018-10-08
This patch makes some small internal changes to comply with a new lint setting in the build. There should be no user-visible change.
3.74.2 - 2018-10-03
This patch fixes issue #1153 <https://github.com/HypothesisWorks/hypothesis/issues/1153>, where time spent reifying a strategy was also counted in the time spent generating the first example. Strategies are now fully constructed and validated before the timer is started.
3.74.1 - 2018-10-03
This patch fixes some broken formatting and links in the documentation.
3.74.0 - 2018-10-01
This release checks that the value of the print_blob <#hypothesis.settings.print_blob> setting is a PrintSettings instance.
Being able to specify a boolean value was not intended, and is now deprecated. In addition, specifying True will now cause the blob to always be printed, instead of causing it to be suppressed.
Specifying any value that is not a PrintSettings or a boolean is now an error.
3.73.5 - 2018-10-01
Changes the documentation for hypothesis.strategies.datetimes, hypothesis.strategies.dates, hypothesis.strategies.times to use the new parameter names min_value and max_value instead of the deprecated names
3.73.4 - 2018-09-30
This patch ensures that Hypothesis deprecation warnings display the code that emitted them when you're not running in -Werror mode (issue #652 <https://github.com/HypothesisWorks/hypothesis/issues/652>).
3.73.3 - 2018-09-27
Tracebacks involving @composite <#hypothesis.strategies.composite> are now slightly shorter due to some internal refactoring.
3.73.2 - 2018-09-26
This patch fixes errors in the internal comments for one of the shrinker passes. There is no user-visible change.
3.73.1 - 2018-09-25
This patch substantially improves the distribution of data generated with recursive() <#hypothesis.strategies.recursive>, and fixes a rare internal error (issue #1502 <https://github.com/HypothesisWorks/hypothesis/issues/1502>).
3.73.0 - 2018-09-24
This release adds the fulfill() function, which is designed for testing code that uses dpcontracts <https://pypi.org/project/dpcontracts/> 0.4 or later for input validation. This provides some syntactic sugar around use of assume() <#hypothesis.assume>, to automatically filter out and retry calls that cause a precondition check to fail (issue #1474 <https://github.com/HypothesisWorks/hypothesis/issues/1474>).
3.72.0 - 2018-09-24
This release makes setting attributes of the hypothesis.settings <#hypothesis.settings> class an explicit error. This has never had any effect, but could mislead users who confused it with the current settings instance hypothesis.settings.default (which is also immutable). You can change the global settings with register_profile() <#hypothesis.settings.register_profile>.
3.71.11 - 2018-09-24
This patch factors out some common code in the shrinker for iterating over pairs of data blocks. There should be no user-visible change.
3.71.10 - 2018-09-18
This patch allows from_type() <#hypothesis.strategies.from_type> to handle the empty tuple type, typing.Tuple[()].
3.71.9 - 2018-09-17
This patch updates some internal comments for mypy <https://pypi.org/project/mypy/>. There is no user-visible effect, even for Mypy users.
3.71.8 - 2018-09-17
This patch fixes a rare bug that would cause a particular shrinker pass to raise an IndexError, if a shrink improvement changed the underlying data in an unexpected way.
3.71.7 - 2018-09-17
This release fixes the broken cross-references in our docs, and adds a CI check so we don't add new ones.
3.71.6 - 2018-09-16
This patch fixes two bugs (issue #944 <https://github.com/HypothesisWorks/hypothesis/issues/944> and issue #1521 <https://github.com/HypothesisWorks/hypothesis/issues/1521>), where messages about @seed <#hypothesis.seed> did not check the current verbosity setting, and the wrong settings were active while executing explicit examples from @example <#hypothesis.example>.
3.71.5 - 2018-09-15
This patch fixes a DeprecationWarning added in Python 3.8 (issue #1576 <https://github.com/HypothesisWorks/hypothesis/issues/1576>).
Thanks to tirkarthi for this contribution!
3.71.4 - 2018-09-14
This is a no-op release, which implements automatic DOI minting and code archival of Hypothesis via Zenodo <https://zenodo.org/>. Thanks to CERN and the EU Horizon 2020 programme for providing this service!
Check our CITATION.cff <https://github.com/HypothesisWorks/hypothesis/blob/master/CITATION.cff> file for details, or head right on over to doi.org/10.5281/zenodo.1412597 <https://doi.org/10.5281/zenodo.1412597>
3.71.3 - 2018-09-10
This release adds the test name to some deprecation warnings, for easier debugging.
Thanks to Sanyam Khurana for the patch!
3.71.2 - 2018-09-10
This release makes Hypothesis's memory usage substantially smaller for tests with many examples, by bounding the number of past examples it keeps around.
You will not see much difference unless you are running tests with max_examples <#hypothesis.settings.max_examples> set to well over 1000, but if you do have such tests then you should see memory usage mostly plateau where previously it would have grown linearly with time.
3.71.1 - 2018-09-09
This patch adds internal comments to some tree traversals in the core engine. There is no user-visible change.
3.71.0 - 2018-09-08
This release deprecates the coverage-guided testing functionality, as it has proven brittle and does not really pull its weight.
We intend to replace it with something more useful in the future, but the feature in its current form does not seem to be worth the cost of using, and whatever replaces it will likely look very different.
3.70.4 - 2018-09-08
This patch changes the behaviour of reproduce_failure() <#hypothesis.reproduce_failure> so that blobs are only printed in quiet mode when the print_blob <#hypothesis.settings.print_blob> setting is set to ALWAYS.
Thanks to Cameron McGill for writing this patch at the PyCon Australia sprints!
3.70.3 - 2018-09-03
This patch removes some unnecessary code from the internals. There is no user-visible change.
3.70.2 - 2018-09-03
This patch fixes an internal bug where a corrupted argument to @reproduce_failure <#hypothesis.reproduce_failure> could raise the wrong type of error. Thanks again to Paweł T. Jochym, who maintains Hypothesis on conda-forge <https://conda-forge.org/> and consistently provides excellent bug reports including issue #1558 <https://github.com/HypothesisWorks/hypothesis/issues/1558>.
3.70.1 - 2018-09-03
This patch updates hypothesis to report its version and settings when run with pytest. (issue #1223 <https://github.com/HypothesisWorks/hypothesis/issues/1223>).
Thanks to Jack Massey for this feature.
3.70.0 - 2018-09-01
This release adds a fullmatch argument to from_regex() <#hypothesis.strategies.from_regex>. When fullmatch=True, the whole example will match the regex pattern as for python:re.fullmatch().
Thanks to Jakub Nabaglo for writing this patch at the PyCon Australia sprints!
3.69.12 - 2018-08-30
This release reverts the changes to logging handling in 3.69.11, which broke test that use the pytest <https://pypi.org/project/pytest/> caplog fixture internally because all logging was disabled (issue #1546 <https://github.com/HypothesisWorks/hypothesis/issues/1546>).
3.69.11 - 2018-08-29
This patch will hide all logging messages produced by test cases before the final, minimal, failing test case (issue #356 <https://github.com/HypothesisWorks/hypothesis/issues/356>).
Thanks to Gary Donovan for writing this patch at the PyCon Australia sprints!
3.69.10 - 2018-08-29
This patch fixes a bug that prevents coverage from reporting unexecuted Python files (issue #1085 <https://github.com/HypothesisWorks/hypothesis/issues/1085>).
Thanks to Gary Donovan for writing this patch at the PyCon Australia sprints!
3.69.9 - 2018-08-28
This patch improves the packaging of the Python package by adding LICENSE.txt to the sdist (issue #1311 <https://github.com/HypothesisWorks/hypothesis/issues/1311>), clarifying the minimum supported versions of pytz <https://pypi.org/project/pytz/> and dateutil <https://pypi.org/project/python-dateutil/> (issue #1383 <https://github.com/HypothesisWorks/hypothesis/issues/1383>), and adds keywords to the metadata (issue #1520 <https://github.com/HypothesisWorks/hypothesis/issues/1520>).
Thanks to Graham Williamson for writing this patch at the PyCon Australia sprints!
3.69.8 - 2018-08-28
This is an internal change which replaces pickle with json to prevent possible security issues.
Thanks to Vidya Rani D G for writing this patch at the PyCon Australia sprints!
3.69.7 - 2018-08-28
This patch ensures that note() <#hypothesis.note> prints the note for every test case when the verbosity <#hypothesis.settings.verbosity> setting is Verbosity.verbose. At normal verbosity it only prints from the final test case.
Thanks to Tom McDermott for writing this patch at the PyCon Australia sprints!
3.69.6 - 2018-08-27
This patch improves the testing of some internal caching. It should have no user-visible effect.
3.69.5 - 2018-08-27
This change performs a small rename and refactoring in the core engine. There is no user-visible change.
3.69.4 - 2018-08-27
This change improves the core engine's ability to avoid unnecessary work, by consulting its cache of previously-tried inputs in more cases.
3.69.3 - 2018-08-27
This patch handles passing an empty python:enum.Enum to from_type() <#hypothesis.strategies.from_type> by returning nothing() <#hypothesis.strategies.nothing>, instead of raising an internal python:AssertionError.
Thanks to Paul Amazona for writing this patch at the PyCon Australia sprints!
3.69.2 - 2018-08-23
This patch fixes a small mistake in an internal comment. There is no user-visible change.
3.69.1 - 2018-08-21
This change fixes a small bug in how the core engine consults its cache of previously-tried inputs. There is unlikely to be any user-visible change.
3.69.0 - 2018-08-20
This release improves argument validation for stateful testing.
- If the target or targets of a rule() <#hypothesis.stateful.rule> are invalid, we now raise a useful validation error rather than an internal exception.
- Passing both the target and targets arguments is deprecated - append the target bundle to the targets tuple of bundles instead.
- Passing the name of a Bundle rather than the Bundle itself is also deprecated.
3.68.3 - 2018-08-20
This is a docs-only patch, fixing some typos and formatting issues.
3.68.2 - 2018-08-19
This change fixes a small bug in how the core engine caches the results of previously-tried inputs. The effect is unlikely to be noticeable, but it might avoid unnecessary work in some cases.
3.68.1 - 2018-08-18
This patch documents the from_dtype() <#hypothesis.extra.numpy.from_dtype> function, which infers a strategy for numpy:numpy.dtypes. This is used in arrays() <#hypothesis.extra.numpy.arrays>, but can also be used directly when creating e.g. Pandas objects.
3.68.0 - 2018-08-15
arrays() <#hypothesis.extra.numpy.arrays> now checks that integer and float values drawn from elements and fill strategies can be safely cast to the dtype of the array, and emits a warning otherwise (issue #1385 <https://github.com/HypothesisWorks/hypothesis/issues/1385>).
Elements in the resulting array could previously violate constraints on the elements strategy due to floating-point overflow or truncation of integers to fit smaller types.
3.67.1 - 2018-08-14
This release contains a tiny refactoring of the internals. There is no user-visible change.
3.67.0 - 2018-08-10
This release adds a width argument to floats() <#hypothesis.strategies.floats>, to generate lower-precision floating point numbers for e.g. Numpy arrays.
The generated examples are always instances of Python's native float type, which is 64bit, but passing width=32 will ensure that all values can be exactly represented as 32bit floats. This can be useful to avoid overflow (to +/- infinity), and for efficiency of generation and shrinking.
Half-precision floats (width=16) are also supported, but require Numpy if you are running Python 3.5 or earlier.
3.66.33 - 2018-08-10
This release fixes a bug in floats() <#hypothesis.strategies.floats>, where setting allow_infinity=False and exactly one of min_value and max_value would allow infinite values to be generated.
3.66.32 - 2018-08-09
This release adds type hints to the @example() <#hypothesis.example> and seed() <#hypothesis.seed> decorators, and fixes the type hint on register_type_strategy() <#hypothesis.strategies.register_type_strategy>. The second argument to register_type_strategy() <#hypothesis.strategies.register_type_strategy> must either be a SearchStrategy, or a callable which takes a type and returns a SearchStrategy.
3.66.31 - 2018-08-08
Another set of changes designed to improve the performance of shrinking on large examples. In particular the shrinker should now spend considerably less time running useless shrinks.
3.66.30 - 2018-08-06
"Bug fixes and performance improvements".
This release is a fairly major overhaul of the shrinker designed to improve its behaviour on large examples, especially around stateful testing. You should hopefully see shrinking become much faster, with little to no quality degradation (in some cases quality may even improve).
3.66.29 - 2018-08-05
This release fixes two very minor bugs in the core engine:
- it fixes a corner case that was missing in 3.66.28, which should cause shrinking to work slightly better.
- it fixes some logic for how shrinking interacts with the database that was causing Hypothesis to be insufficiently aggressive about clearing out old keys.
3.66.28 - 2018-08-05
This release improves how Hypothesis handles reducing the size of integers' representation. This change should mostly be invisible as it's purely about the underlying representation and not the generated value, but it may result in some improvements to shrink performance.
3.66.27 - 2018-08-05
This release changes the order in which Hypothesis chooses parts of the test case to shrink. For typical usage this should be a significant performance improvement on large examples. It is unlikely to have a major impact on example quality, but where it does change the result it should usually be an improvement.
3.66.26 - 2018-08-05
This release improves the debugging information that the shrinker emits about the operations it performs, giving better summary statistics about which passes resulted in test executions and whether they were successful.
3.66.25 - 2018-08-05
This release fixes several bugs that were introduced to the shrinker in 3.66.24 which would have caused it to behave significantly less well than advertised. With any luck you should actually see the promised benefits now.
3.66.24 - 2018-08-03
This release changes how Hypothesis deletes data when shrinking in order to better handle deletion of large numbers of contiguous sequences. Most tests should see little change, but this will hopefully provide a significant speed up for stateful testing <#stateful>.
3.66.23 - 2018-08-02
This release makes some internal changes to enable further improvements to the shrinker. You may see some changes in the final shrunk examples, but they are unlikely to be significant.
3.66.22 - 2018-08-01
This release adds some more internal caching to the shrinker. This should cause a significant speed up for shrinking, especially for stateful testing and large example sizes.
3.66.21 - 2018-08-01
This patch is for downstream packagers - our tests now pass under pytest <https://pypi.org/project/pytest/> 3.7.0 (released 2018-07-30). There are no changes to the source of Hypothesis itself.
3.66.20 - 2018-08-01
This release removes some functionality from the shrinker that was taking a considerable amount of time and does not appear to be useful any more due to a number of quality improvements in the shrinker.
You may see some degradation in shrink quality as a result of this, but mostly shrinking should just get much faster.
3.66.19 - 2018-08-01
This release slightly changes the format of some debugging information emitted during shrinking, and refactors some of the internal interfaces around that.
3.66.18 - 2018-07-31
This release is a very small internal refactoring which should have no user visible impact.
3.66.17 - 2018-07-31
This release fixes a bug that could cause an IndexError to be raised from inside Hypothesis during shrinking. It is likely that it was impossible to trigger this bug in practice - it was only made visible by some currently unreleased work.
3.66.16 - 2018-07-31
This release is a very small internal refactoring which should have no user visible impact.
3.66.15 - 2018-07-31
This release makes Hypothesis's shrinking faster by removing some redundant work that it does when minimizing values in its internal representation.
3.66.14 - 2018-07-30
This release expands the deprecation of timeout from 3.16.0 to also emit the deprecation warning in find or stateful testing <#stateful>.
3.66.13 - 2018-07-30
This release adds an additional shrink pass that is able to reduce the size of examples in some cases where the transformation is non-obvious. In particular this will improve the quality of some examples which would have regressed in 3.66.12.
3.66.12 - 2018-07-28
This release changes how we group data together for shrinking. It should result in improved shrinker performance, especially in stateful testing.
3.66.11 - 2018-07-28
This patch modifies how which rule to run is selected during rule based stateful testing <#stateful>. This should result in a slight performance increase during generation and a significant performance and quality improvement when shrinking.
As a result of this change, some state machines which would previously have thrown an InvalidDefinition are no longer detected as invalid.
3.66.10 - 2018-07-28
This release weakens some minor functionality in the shrinker that had only modest benefit and made its behaviour much harder to reason about.
This is unlikely to have much user visible effect, but it is possible that in some cases shrinking may get slightly slower. It is primarily to make it easier to work on the shrinker and pave the way for future work.
3.66.9 - 2018-07-26
This release improves the information that Hypothesis emits about its shrinking when verbosity <#hypothesis.settings.verbosity> is set to debug.
3.66.8 - 2018-07-24
This patch includes some minor fixes in the documentation, and updates the minimum version of pytest <https://pypi.org/project/pytest/> to 3.0 (released August 2016).
3.66.7 - 2018-07-24
This release fixes a bug where difficult to shrink tests could sometimes trigger an internal assertion error inside the shrinker.
3.66.6 - 2018-07-23
This patch ensures that Hypothesis fully supports Python 3.7, by upgrading from_type() <#hypothesis.strategies.from_type> (issue #1264 <https://github.com/HypothesisWorks/hypothesis/issues/1264>) and fixing some minor issues in our test suite (issue #1148 <https://github.com/HypothesisWorks/hypothesis/issues/1148>).
3.66.5 - 2018-07-22
This patch fixes the online docs for various extras, by ensuring that their dependencies are installed on readthedocs.io (issue #1326 <https://github.com/HypothesisWorks/hypothesis/issues/1326>).
3.66.4 - 2018-07-20
This release improves the shrinker's ability to reorder examples.
For example, consider the following test:
import hypothesis.strategies as st
from hypothesis import given
@given(st.text(), st.text())
def test_non_equal(x, y):
assert x != yPreviously this could have failed with either of x="", y="0" or x="0", y="". Now it should always fail with x="", y="0".
This will allow the shrinker to produce more consistent results, especially in cases where test cases contain some ordered collection whose actual order does not matter.
3.66.3 - 2018-07-20
This patch fixes inference in the builds() <#hypothesis.strategies.builds> strategy with subtypes of python:typing.NamedTuple, where the __init__ method is not useful for introspection. We now use the field types instead - thanks to James Uther for identifying this bug.
3.66.2 - 2018-07-19
This release improves the shrinker's ability to handle situations where there is an additive constraint between two values.
For example, consider the following test:
import hypothesis.strategies as st
from hypothesis import given
@given(st.integers(), st.integers())
def test_does_not_exceed_100(m, n):
assert m + n < 100Previously this could have failed with almost any pair (m, n) with 0 <= m <= n and m + n == 100. Now it should almost always fail with m=0, n=100.
This is a relatively niche specialisation, but can be useful in situations where e.g. a bug is triggered by an integer overflow.
3.66.1 - 2018-07-09
This patch fixes a rare bug where an incorrect percentage drawtime could be displayed for a test, when the system clock was changed during a test running under Python 2 (we use python:time.monotonic() where it is available to avoid such problems). It also fixes a possible zero-division error that can occur when the underlying C library double-rounds an intermediate value in python:math.fsum() and gets the least significant bit wrong.
3.66.0 - 2018-07-05
This release improves validation of the alphabet argument to the text() <#hypothesis.strategies.text> strategy. The following misuses are now deprecated, and will be an error in a future version:
- passing an unordered collection (such as set('abc')), which violates invariants about shrinking and reproducibility
- passing an alphabet sequence with elements that are not strings
- passing an alphabet sequence with elements that are not of length one, which violates any size constraints that may apply
Thanks to Sushobhit for adding these warnings (issue #1329 <https://github.com/HypothesisWorks/hypothesis/issues/1329>).
3.65.3 - 2018-07-04
This release fixes a mostly theoretical bug where certain usage of the internal API could trigger an assertion error inside Hypothesis. It is unlikely that this problem is even possible to trigger through the public API.
3.65.2 - 2018-07-04
This release fixes dependency information for coverage. Previously Hypothesis would allow installing coverage <https://pypi.org/project/coverage/> with any version, but it only works with coverage 4.0 or later.
We now specify the correct metadata in our setup.py, so Hypothesis will only allow installation with compatible versions of coverage.
3.65.1 - 2018-07-03
This patch ensures that stateful tests <#stateful> which raise an error from a pytest <https://pypi.org/project/pytest/> helper still print the sequence of steps taken to reach that point (issue #1372 <https://github.com/HypothesisWorks/hypothesis/issues/1372>). This reporting was previously broken because the helpers inherit directly from python:BaseException, and therefore require special handling to catch without breaking e.g. the use of ctrl-C to quit the test.
3.65.0 - 2018-06-30
This release deprecates the max_shrinks setting in favor of an internal heuristic. If you need to avoid shrinking examples, use the phases <#hypothesis.settings.phases> setting instead. (issue #1235 <https://github.com/HypothesisWorks/hypothesis/issues/1235>)
3.64.2 - 2018-06-27
This release fixes a bug where an internal assertion error could sometimes be triggered while shrinking a failing test.
3.64.1 - 2018-06-27
This patch fixes type-checking errors in our vendored pretty-printer, which were ignored by our mypy config but visible for anyone else (whoops). Thanks to Pi Delport for reporting issue #1359 <https://github.com/HypothesisWorks/hypothesis/issues/1359> so promptly.
3.64.0 - 2018-06-26
This release adds an interface <#custom-function-execution> which can be used to insert a wrapper between the original test function and @given <#hypothesis.given> (issue #1257 <https://github.com/HypothesisWorks/hypothesis/issues/1257>). This will be particularly useful for test runner extensions such as pytest-trio <https://pypi.org/project/pytest-trio/>, but is not recommended for direct use by other users of Hypothesis.
3.63.0 - 2018-06-26
This release adds a new mechanism to infer strategies for classes defined using attrs <https://pypi.org/project/attrs/>, based on the type, converter, or validator of each attribute. This inference is now built in to builds() <#hypothesis.strategies.builds> and from_type() <#hypothesis.strategies.from_type>.
On Python 2, from_type() <#hypothesis.strategies.from_type> no longer generates instances of int when passed long, or vice-versa.
3.62.0 - 2018-06-26
This release adds PEP 484 <https://peps.python.org/pep-0484/> type hints to Hypothesis on a provisional basis, using the comment-based syntax for Python 2 compatibility.
It also adds the py.typed marker specified in PEP 561 <https://peps.python.org/pep-0561/>. After you pip install hypothesis, mypy <https://pypi.org/project/mypy/> 0.590 or later will therefore type-check your use of our public interface!
3.61.0 - 2018-06-24
This release deprecates the use of settings <#hypothesis.settings> as a context manager, the use of which is somewhat ambiguous.
Users should define settings with global state or with the @settings(...) <#hypothesis.settings> decorator.
3.60.1 - 2018-06-20
Fixed a bug in generating an instance of a Django model from a strategy where the primary key is generated as part of the strategy. See details here <#django-generating-primary-key>.
Thanks to Tim Martin for this contribution.
3.60.0 - 2018-06-20
This release adds the @initialize <#hypothesis.stateful.initialize> decorator for stateful testing (originally discussed in issue #1216 <https://github.com/HypothesisWorks/hypothesis/issues/1216>). All @initialize <#hypothesis.stateful.initialize> rules will be called once each in an arbitrary order before any normal rule is called.
3.59.3 - 2018-06-19
This is a no-op release to take into account some changes to the release process. It should have no user visible effect.
3.59.2 - 2018-06-18
This adds support for partially sorting examples which cannot be fully sorted. For example, [5, 4, 3, 2, 1, 0] with a constraint that the first element needs to be larger than the last becomes [1, 2, 3, 4, 5, 0].
Thanks to Luke for contributing.
3.59.1 - 2018-06-16
This patch uses python:random.getstate() and python:random.setstate() to restore the PRNG state after @given <#hypothesis.given> runs deterministic tests. Without restoring state, you might have noticed problems such as issue #1266 <https://github.com/HypothesisWorks/hypothesis/issues/1266>. The fix also applies to stateful testing (issue #702 <https://github.com/HypothesisWorks/hypothesis/issues/702>).
3.59.0 - 2018-06-14
This release adds the emails() <#hypothesis.strategies.emails> strategy, which generates unicode strings representing an email address.
Thanks to Sushobhit for moving this to the public API (issue #162 <https://github.com/HypothesisWorks/hypothesis/issues/162>).
3.58.1 - 2018-06-13
This improves the shrinker. It can now reorder examples: 3 1 2 becomes 1 2 3.
Thanks to Luke for contributing.
3.58.0 - 2018-06-13
This adds a new extra timezones() <#hypothesis.extra.dateutil.timezones> strategy that generates dateutil timezones <https://pypi.org/project/python-dateutil/>.
Thanks to Conrad for contributing.
3.57.0 - 2018-05-20
Using an unordered collection with the permutations() <#hypothesis.strategies.permutations> strategy has been deprecated because the order in which e.g. a set shrinks is arbitrary. This may cause different results between runs.
3.56.10 - 2018-05-16
This release makes hypothesis.settings.define_setting a private method, which has the effect of hiding it from the documentation.
3.56.9 - 2018-05-11
This is another release with no functionality changes as part of changes to Hypothesis's new release tagging scheme.
3.56.8 - 2018-05-10
This is a release with no functionality changes that moves Hypothesis over to a new release tagging scheme.
3.56.7 - 2018-05-10
This release provides a performance improvement for most tests, but in particular users of sampled_from() <#hypothesis.strategies.sampled_from> who don't have numpy installed should see a significant performance improvement.
3.56.6 - 2018-05-09
This patch contains further internal work to support Mypy. There are no user-visible changes... yet.
3.56.5 - 2018-04-22
This patch contains some internal refactoring to run mypy <https://pypi.org/project/mypy/> in CI. There are no user-visible changes.
3.56.4 - 2018-04-21
This release involves some very minor internal clean up and should have no user visible effect at all.
3.56.3 - 2018-04-20
This release fixes a problem introduced in 3.56.0 where setting the hypothesis home directory (through currently undocumented means) would no longer result in the default database location living in the new home directory.
3.56.2 - 2018-04-20
This release fixes a problem introduced in 3.56.0 where setting max_examples <#hypothesis.settings.max_examples> to 1 would result in tests failing with Unsatisfiable. This problem could also occur in other harder to trigger circumstances (e.g. by setting it to a low value, having a hard to satisfy assumption, and disabling health checks).
3.56.1 - 2018-04-20
This release fixes a problem that was introduced in 3.56.0: Use of the HYPOTHESIS_VERBOSITY_LEVEL environment variable was, rather than deprecated, actually broken due to being read before various setup the deprecation path needed was done. It now works correctly (and emits a deprecation warning).
3.56.0 - 2018-04-17
This release deprecates several redundant or internally oriented settings <#hypothesis.settings>, working towards an orthogonal set of configuration options that are widely useful without requiring any knowledge of our internals (issue #535 <https://github.com/HypothesisWorks/hypothesis/issues/535>).
- Deprecated settings that no longer have any effect are no longer shown in the __repr__ unless set to a non-default value.
- hypothesis.settings.perform_health_check is deprecated, as it duplicates suppress_health_check <#hypothesis.settings.suppress_health_check>.
- hypothesis.settings.max_iterations is deprecated and disabled, because we can usually get better behaviour from an internal heuristic than a user-controlled setting.
- hypothesis.settings.min_satisfying_examples is deprecated and disabled, due to overlap with the filter_too_much <#hypothesis.HealthCheck.filter_too_much> healthcheck and poor interaction with max_examples <#hypothesis.settings.max_examples>.
- HYPOTHESIS_VERBOSITY_LEVEL is now deprecated. Set verbosity <#hypothesis.settings.verbosity> through the profile system instead.
- Examples tried by find() are now reported at debug verbosity level (as well as verbose level).
3.55.6 - 2018-04-14
This release fixes a somewhat obscure condition (issue #1230 <https://github.com/HypothesisWorks/hypothesis/issues/1230>) under which you could occasionally see a failing test trigger an assertion error inside Hypothesis instead of failing normally.
3.55.5 - 2018-04-14
This patch fixes one possible cause of issue #966 <https://github.com/HypothesisWorks/hypothesis/issues/966>. When running Python 2 with hash randomisation, passing a python:bytes object to python:random.seed() would use version=1, which broke derandomize <#hypothesis.settings.derandomize> (because the seed depended on a randomised hash). If derandomize <#hypothesis.settings.derandomize> is still nondeterministic for you, please open an issue.
3.55.4 - 2018-04-13
This patch makes a variety of minor improvements to the documentation, and improves a few validation messages for invalid inputs.
3.55.3 - 2018-04-12
This release updates the URL metadata associated with the PyPI package (again). It has no other user visible effects.
3.55.2 - 2018-04-11
This release updates the URL metadata associated with the PyPI package. It has no other user visible effects.
3.55.1 - 2018-04-06
This patch relaxes constraints in our tests on the expected values returned by the standard library function hypot() and the internal helper function cathetus, to fix near-exact test failures on some 32-bit systems used by downstream packagers.
3.55.0 - 2018-04-05
This release includes several improvements to the handling of the database <#hypothesis.settings.database> setting.
- The database_file setting was a historical artefact, and you should just use database <#hypothesis.settings.database> directly.
- The HYPOTHESIS_DATABASE_FILE environment variable is deprecated, in favor of load_profile() <#hypothesis.settings.load_profile> and the database <#hypothesis.settings.database> setting.
- If you have not configured the example database at all and the default location is not usable (due to e.g. permissions issues), Hypothesis will fall back to an in-memory database. This is not persisted between sessions, but means that the defaults work on read-only filesystems.
3.54.0 - 2018-04-04
This release improves the complex_numbers() <#hypothesis.strategies.complex_numbers> strategy, which now supports min_magnitude and max_magnitude arguments, along with allow_nan and allow_infinity like for floats() <#hypothesis.strategies.floats>.
Thanks to J.J. Green for this feature.
3.53.0 - 2018-04-01
This release removes support for Django 1.8, which reached end of life on 2018-04-01. You can see Django's release and support schedule on the Django Project website <https://www.djangoproject.com/download/#supported-versions>.
3.52.3 - 2018-04-01
This patch fixes the min_satisfying_examples settings documentation, by explaining that example shrinking is tracked at the level of the underlying bytestream rather than the output value.
The output from find() in verbose mode has also been adjusted - see the example session for Verbosity <#hypothesis.Verbosity> - to avoid duplicating lines when the example repr is constant, even if the underlying representation has been shrunken.
3.52.2 - 2018-03-30
This release improves the output of failures with rule based stateful testing <#stateful> in two ways:
- The output from it is now usually valid Python code.
- When the same value has two different names because it belongs to two different bundles, it will now display with the name associated with the correct bundle for a rule argument where it is used.
3.52.1 - 2018-03-29
This release improves the behaviour of stateful testing <#stateful> in two ways:
- Previously some runs would run no steps (issue #376 <https://github.com/HypothesisWorks/hypothesis/issues/376>). This should no longer happen.
- RuleBasedStateMachine tests which used bundles extensively would often shrink terribly. This should now be significantly improved, though there is likely a lot more room for improvement.
This release also involves a low level change to how ranges of integers are handles which may result in other improvements to shrink quality in some cases.
3.52.0 - 2018-03-24
This release deprecates use of @settings(...) <#hypothesis.settings> as a decorator, on functions or methods that are not also decorated with @given <#hypothesis.given>. You can still apply these decorators in any order, though you should only do so once each.
Applying @given <#hypothesis.given> twice was already deprecated, and applying @settings(...) <#hypothesis.settings> twice is deprecated in this release and will become an error in a future version. Neither could ever be used twice to good effect.
Using @settings(...) <#hypothesis.settings> as the sole decorator on a test is completely pointless, so this common usage error will become an error in a future version of Hypothesis.
3.51.0 - 2018-03-24
This release deprecates the average_size argument to lists() <#hypothesis.strategies.lists> and other collection strategies. You should simply delete it wherever it was used in your tests, as it no longer has any effect.
In early versions of Hypothesis, the average_size argument was treated as a hint about the distribution of examples from a strategy. Subsequent improvements to the conceptual model and the engine for generating and shrinking examples mean it is more effective to simply describe what constitutes a valid example, and let our internals handle the distribution.
3.50.3 - 2018-03-24
This patch contains some internal refactoring so that we can run with warnings as errors in CI.
3.50.2 - 2018-03-20
This has no user-visible changes except one slight formatting change to one docstring, to avoid a deprecation warning.
3.50.1 - 2018-03-20
This patch fixes an internal error introduced in 3.48.0, where a check for the Django test runner would expose import-time errors in Django configuration (issue #1167 <https://github.com/HypothesisWorks/hypothesis/issues/1167>).
3.50.0 - 2018-03-19
This release improves validation of numeric bounds for some strategies.
- integers() <#hypothesis.strategies.integers> and floats() <#hypothesis.strategies.floats> now raise InvalidArgument if passed a min_value or max_value which is not an instance of Real, instead of various internal errors.
- floats() <#hypothesis.strategies.floats> now converts its bounding values to the nearest float above or below the min or max bound respectively, instead of just casting to float. The old behaviour was incorrect in that you could generate float(min_value), even when this was less than min_value itself (possible with eg. fractions).
- When both bounds are provided to floats() <#hypothesis.strategies.floats> but there are no floats in the interval, such as [(2**54)+1 .. (2**55)-1], InvalidArgument is raised.
- decimals() <#hypothesis.strategies.decimals> gives a more useful error message if passed a string that cannot be converted to Decimal in a context where this error is not trapped.
Code that previously seemed to work may be explicitly broken if there were no floats between min_value and max_value (only possible with non-float bounds), or if a bound was not a Real number but still allowed in python:math.isnan (some custom classes with a __float__ method).
3.49.1 - 2018-03-15
This patch fixes our tests for Numpy dtype strategies on big-endian platforms, where the strategy behaved correctly but the test assumed that the native byte order was little-endian.
There is no user impact unless you are running our test suite on big-endian platforms. Thanks to Graham Inggs for reporting issue #1164 <https://github.com/HypothesisWorks/hypothesis/issues/1164>.
3.49.0 - 2018-03-12
This release deprecates passing elements=None to collection strategies, such as lists() <#hypothesis.strategies.lists>.
Requiring lists(nothing()) or builds(list) instead of lists() means slightly more typing, but also improves the consistency and discoverability of our API - as well as showing how to compose or construct strategies in ways that still work in more complex situations.
Passing a nonzero max_size to a collection strategy where the elements strategy contains no values is now deprecated, and will be an error in a future version. The equivalent with elements=None is already an error.
3.48.1 - 2018-03-05
This patch will minimize examples that would come out non-minimal in previous versions. Thanks to Kyle Reeve for this patch.
3.48.0 - 2018-03-05
This release improves some "unhappy paths" when using Hypothesis with the standard library python:unittest module:
- Applying @given <#hypothesis.given> to a non-test method which is overridden from python:unittest.TestCase, such as setUp, raises a new health check <#hypothesis.HealthCheck.not_a_test_method>. (issue #991 <https://github.com/HypothesisWorks/hypothesis/issues/991>)
- Using subTest() within a test decorated with @given <#hypothesis.given> would leak intermediate results when tests were run under the python:unittest test runner. Individual reporting of failing subtests is now disabled during a test using @given <#hypothesis.given>. (issue #1071 <https://github.com/HypothesisWorks/hypothesis/issues/1071>)
- @given <#hypothesis.given> is still not a class decorator, but the error message if you try using it on a class has been improved.
As a related improvement, using django:django.test.TestCase with @given <#hypothesis.given> instead of hypothesis.extra.django.TestCase <#hypothesis.extra.django.TestCase> raises an explicit error instead of running all examples in a single database transaction.
3.47.0 - 2018-03-02
register_profile <#hypothesis.settings.register_profile> now accepts keyword arguments for specific settings, and the parent settings object is now optional. Using a name for a registered profile which is not a string was never suggested, but it is now also deprecated and will eventually be an error.
3.46.2 - 2018-03-01
This release removes an unnecessary branch from the code, and has no user-visible impact.
3.46.1 - 2018-03-01
This changes only the formatting of our docstrings and should have no user-visible effects.
3.46.0 - 2018-02-26
characters() <#hypothesis.strategies.characters> has improved docs about what arguments are valid, and additional validation logic to raise a clear error early (instead of e.g. silently ignoring a bad argument). Categories may be specified as the Unicode 'general category' (eg 'Nd'), or as the 'major category' (eg ['N', 'Lu'] is equivalent to ['Nd', 'Nl', 'No', 'Lu']).
In previous versions, general categories were supported and all other input was silently ignored. Now, major categories are supported in addition to general categories (which may change the behaviour of some existing code), and all other input is deprecated.
3.45.5 - 2018-02-26
This patch improves strategy inference in hypothesis.extra.django to account for some validators in addition to field type - see issue #1116 <https://github.com/HypothesisWorks/hypothesis/issues/1116> for ongoing work in this space.
Specifically, if a CharField or TextField has an attached RegexValidator, we now use from_regex() <#hypothesis.strategies.from_regex> instead of text() <#hypothesis.strategies.text> as the underlying strategy. This allows us to generate examples of the default User model, closing issue #1112 <https://github.com/HypothesisWorks/hypothesis/issues/1112>.
3.45.4 - 2018-02-25
This patch improves some internal debugging information, fixes a typo in a validation error message, and expands the documentation for new contributors.
3.45.3 - 2018-02-23
This patch may improve example shrinking slightly for some strategies.
3.45.2 - 2018-02-18
This release makes our docstring style more consistent, thanks to flake8-docstrings <https://pypi.org/project/flake8-docstrings/>. There are no user-visible changes.
3.45.1 - 2018-02-17
This fixes an indentation issue in docstrings for datetimes() <#hypothesis.strategies.datetimes>, dates() <#hypothesis.strategies.dates>, times() <#hypothesis.strategies.times>, and timedeltas() <#hypothesis.strategies.timedeltas>.
3.45.0 - 2018-02-13
This release fixes builds() <#hypothesis.strategies.builds> so that target can be used as a keyword argument for passing values to the target. The target itself can still be specified as a keyword argument, but that behavior is now deprecated. The target should be provided as the first positional argument.
3.44.26 - 2018-02-06
This release fixes some formatting issues in the Hypothesis source code. It should have no externally visible effects.
3.44.25 - 2018-02-05
This release changes the way in which Hypothesis tries to shrink the size of examples. It probably won't have much impact, but might make shrinking faster in some cases. It is unlikely but not impossible that it will change the resulting examples.
3.44.24 - 2018-01-27
This release fixes dependency information when installing Hypothesis from a binary "wheel" distribution.
- The install_requires for enum34 <https://pypi.org/project/enum34/> is resolved at install time, rather than at build time (with potentially different results).
- Django has fixed their python_requires for versions 2.0.0 onward, simplifying Python2-compatible constraints for downstream projects.
3.44.23 - 2018-01-24
This release improves shrinking in a class of pathological examples that you are probably never hitting in practice. If you are hitting them in practice this should be a significant speed up in shrinking. If you are not, you are very unlikely to notice any difference. You might see a slight slow down and/or slightly better falsifying examples.
3.44.22 - 2018-01-23
This release fixes a dependency problem. It was possible to install Hypothesis with an old version of attrs <https://pypi.org/project/attrs/>, which would throw a TypeError as soon as you tried to import hypothesis. Specifically, you need attrs 16.0.0 or newer.
Hypothesis will now require the correct version of attrs when installing.
3.44.21 - 2018-01-22
This change adds some additional structural information that Hypothesis will use to guide its search.
You mostly shouldn't see much difference from this. The two most likely effects you would notice are:
- Hypothesis stores slightly more examples in its database for passing tests.
- Hypothesis may find new bugs that it was previously missing, but it probably won't (this is a basic implementation of the feature that is intended to support future work. Although it is useful on its own, it's not very useful on its own).
3.44.20 - 2018-01-21
This is a small refactoring release that changes how Hypothesis tracks some information about the boundary of examples in its internal representation.
You are unlikely to see much difference in behaviour, but memory usage and run time may both go down slightly during normal test execution, and when failing Hypothesis might print its failing example slightly sooner.
3.44.19 - 2018-01-21
This changes how we compute the default average_size for all collection strategies. Previously setting a max_size without setting an average_size would have the seemingly paradoxical effect of making data generation slower, because it would raise the average size from its default. Now setting max_size will either leave the default unchanged or lower it from its default.
If you are currently experiencing this problem, this may make your tests substantially faster. If you are not, this will likely have no effect on you.
3.44.18 - 2018-01-20
This is a small refactoring release that changes how Hypothesis detects when the structure of data generation depends on earlier values generated (e.g. when using .flatmap() <#hypothesis.strategies.SearchStrategy.flatmap> or composite() <#hypothesis.strategies.composite>). It should not have any observable effect on behaviour.
3.44.17 - 2018-01-15
This release fixes a typo in internal documentation, and has no user-visible impact.
3.44.16 - 2018-01-13
This release improves test case reduction for recursive data structures. Hypothesis now guarantees that whenever a strategy calls itself recursively (usually this will happen because you are using deferred() <#hypothesis.strategies.deferred>), any recursive call may replace the top level value. e.g. given a tree structure, Hypothesis will always try replacing it with a subtree.
Additionally this introduces a new heuristic that may in some circumstances significantly speed up test case reduction - Hypothesis should be better at immediately replacing elements drawn inside another strategy with their minimal possible value.
3.44.15 - 2018-01-13
from_type() <#hypothesis.strategies.from_type> can now resolve recursive types such as binary trees (issue #1004 <https://github.com/HypothesisWorks/hypothesis/issues/1004>). Detection of non-type arguments has also improved, leading to better error messages in many cases involving forward references <https://peps.python.org/pep-0484/#forward-references>.
3.44.14 - 2018-01-08
This release fixes a bug in the shrinker that prevented the optimisations in 3.44.6 from working in some cases. It would not have worked correctly when filtered examples were nested (e.g. with a set of integers in some range).
This would not have resulted in any correctness problems, but shrinking may have been slower than it otherwise could be.
3.44.13 - 2018-01-08
This release changes the average bit length of values drawn from integers() <#hypothesis.strategies.integers> to be much smaller. Additionally it changes the shrinking order so that now size is considered before sign - e.g. -1 will be preferred to +10.
The new internal format for integers required some changes to the minimizer to make work well, so you may also see some improvements to example quality in unrelated areas.
3.44.12 - 2018-01-07
This changes Hypothesis's internal implementation of weighted sampling. This will affect example distribution and quality, but you shouldn't see any other effects.
3.44.11 - 2018-01-06
This is a change to some internals around how Hypothesis handles avoiding generating duplicate examples and seeking out novel regions of the search space.
You are unlikely to see much difference as a result of it, but it fixes a bug where an internal assertion could theoretically be triggered and has some minor effects on the distribution of examples so could potentially find bugs that have previously been missed.
3.44.10 - 2018-01-06
This patch avoids creating debug statements when debugging is disabled. Profiling suggests this is a 5-10% performance improvement (issue #1040 <https://github.com/HypothesisWorks/hypothesis/issues/1040>).
3.44.9 - 2018-01-06
This patch blacklists null characters ('\x00') in automatically created strategies for Django CharField and TextField, due to a database issue which was recently fixed upstream <https://code.djangoproject.com/ticket/28201> (Hypothesis issue #1045 <https://github.com/HypothesisWorks/hypothesis/issues/1045>).
3.44.8 - 2018-01-06
This release makes the Hypothesis shrinker slightly less greedy in order to avoid local minima - when it gets stuck, it makes a small attempt to search around the final example it would previously have returned to find a new starting point to shrink from. This should improve example quality in some cases, especially ones where the test data has dependencies among parts of it that make it difficult for Hypothesis to proceed.
3.44.7 - 2018-01-04
This release adds support for Django 2 <https://www.djangoproject.com/weblog/2017/dec/02/django-20-released/> in the hypothesis-django extra.
This release drops support for Django 1.10, as it is no longer supported by the Django team.
3.44.6 - 2018-01-02
This release speeds up test case reduction in many examples by being better at detecting large shrinks it can use to discard redundant parts of its input. This will be particularly noticeable in examples that make use of filtering and for some integer ranges.
3.44.5 - 2018-01-02
Happy new year!
This is a no-op release that updates the year range on all of the copyright headers in our source to include 2018.
3.44.4 - 2017-12-23
This release fixes issue #1041 <https://github.com/HypothesisWorks/hypothesis/issues/1041>, which slowed tests by up to 6% due to broken caching.
3.44.3 - 2017-12-21
This release improves the shrinker in cases where examples drawn earlier can affect how much data is drawn later (e.g. when you draw a length parameter in a composite and then draw that many elements). Examples found in cases like this should now be much closer to minimal.
3.44.2 - 2017-12-20
This is a pure refactoring release which changes how Hypothesis manages its set of examples internally. It should have no externally visible effects.
3.44.1 - 2017-12-18
This release fixes issue #997 <https://github.com/HypothesisWorks/hypothesis/issues/997>, in which under some circumstances the body of tests run under Hypothesis would not show up when run under coverage even though the tests were run and the code they called outside of the test file would show up normally.
3.44.0 - 2017-12-17
This release adds a new feature: The @reproduce_failure <#hypothesis.reproduce_failure> decorator, designed to make it easy to use Hypothesis's binary format for examples to reproduce a problem locally without having to share your example database between machines.
This also changes when seeds are printed:
- They will no longer be printed for normal falsifying examples, as there are now adequate ways of reproducing those for all cases, so it just contributes noise.
- They will once again be printed when reusing examples from the database, as health check failures should now be more reliable in this scenario so it will almost always work in this case.
This work was funded by Smarkets <https://smarkets.com/>.
3.43.1 - 2017-12-17
This release fixes a bug with Hypothesis's database management - examples that were found in the course of shrinking were saved in a way that indicated that they had distinct causes, and so they would all be retried on the start of the next test. The intended behaviour, which is now what is implemented, is that only a bounded subset of these examples would be retried.
3.43.0 - 2017-12-17
HypothesisDeprecationWarning <#hypothesis.errors.HypothesisDeprecationWarning> now inherits from python:FutureWarning instead of python:DeprecationWarning, as recommended by PEP 565 <https://peps.python.org/pep-0565/> for user-facing warnings (issue #618 <https://github.com/HypothesisWorks/hypothesis/issues/618>). If you have not changed the default warnings settings, you will now see each distinct HypothesisDeprecationWarning <#hypothesis.errors.HypothesisDeprecationWarning> instead of only the first.
3.42.2 - 2017-12-12
This patch fixes issue #1017 <https://github.com/HypothesisWorks/hypothesis/issues/1017>, where instances of a list or tuple subtype used as an argument to a strategy would be coerced to tuple.
3.42.1 - 2017-12-10
This release has some internal cleanup, which makes reading the code more pleasant and may shrink large examples slightly faster.
3.42.0 - 2017-12-09
This release deprecates faker-extra, which was designed as a transition strategy but does not support example shrinking or coverage-guided discovery.
3.41.0 - 2017-12-06
sampled_from() <#hypothesis.strategies.sampled_from> can now sample from one-dimensional numpy ndarrays. Sampling from multi-dimensional ndarrays still results in a deprecation warning. Thanks to Charlie Tanksley for this patch.
3.40.1 - 2017-12-04
This release makes two changes:
- It makes the calculation of some of the metadata that Hypothesis uses for shrinking occur lazily. This should speed up performance of test case generation a bit because it no longer calculates information it doesn't need.
- It improves the shrinker for certain classes of nested examples. e.g. when shrinking lists of lists, the shrinker is now able to concatenate two adjacent lists together into a single list. As a result of this change, shrinking may get somewhat slower when the minimal example found is large.
3.40.0 - 2017-12-02
This release improves how various ways of seeding Hypothesis interact with the example database:
- Using the example database with seed() <#hypothesis.seed> is now deprecated. You should set database=None if you are doing that. This will only warn if you actually load examples from the database while using @seed.
- The derandomize <#hypothesis.settings.derandomize> will behave the same way as @seed.
- Using --hypothesis-seed will disable use of the database.
- If a test used examples from the database, it will not suggest using a seed to reproduce it, because that won't work.
This work was funded by Smarkets <https://smarkets.com/>.
3.39.0 - 2017-12-01
This release adds a new health check that checks if the smallest "natural" possible example of your test case is very large - this will tend to cause Hypothesis to generate bad examples and be quite slow.
This work was funded by Smarkets <https://smarkets.com/>.
3.38.9 - 2017-11-29
This is a documentation release to improve the documentation of shrinking behaviour for Hypothesis's strategies.
3.38.8 - 2017-11-29
This release improves the performance of characters() <#hypothesis.strategies.characters> when using exclude_characters and from_regex() <#hypothesis.strategies.from_regex> when using negative character classes.
The problems this fixes were found in the course of work funded by Smarkets <https://smarkets.com/>.
3.38.7 - 2017-11-29
This is a patch release for from_regex() <#hypothesis.strategies.from_regex>, which had a bug in handling of the python:re.VERBOSE flag (issue #992 <https://github.com/HypothesisWorks/hypothesis/issues/992>). Flags are now handled correctly when parsing regex.
3.38.6 - 2017-11-28
This patch changes a few byte-string literals from double to single quotes, thanks to an update in unify <https://pypi.org/project/unify/>. There are no user-visible changes.
3.38.5 - 2017-11-23
This fixes the repr of strategies using lambda that are defined inside decorators to include the lambda source.
This would mostly have been visible when using the statistics <#statistics> functionality - lambdas used for e.g. filtering would have shown up with a <unknown> as their body. This can still happen, but it should happen less often now.
3.38.4 - 2017-11-22
This release updates the reported statistics <#statistics> so that they show approximately what fraction of your test run time is spent in data generation (as opposed to test execution).
This work was funded by Smarkets <https://smarkets.com/>.
3.38.3 - 2017-11-21
This is a documentation release, which ensures code examples are up to date by running them as doctests in CI (issue #711 <https://github.com/HypothesisWorks/hypothesis/issues/711>).
3.38.2 - 2017-11-21
This release changes the behaviour of the deadline <#hypothesis.settings.deadline> setting when used with data() <#hypothesis.strategies.data>: Time spent inside calls to data.draw will no longer be counted towards the deadline time.
As a side effect of some refactoring required for this work, the way flaky tests are handled has changed slightly. You are unlikely to see much difference from this, but some error messages will have changed.
This work was funded by Smarkets <https://smarkets.com/>.
3.38.1 - 2017-11-21
This patch has a variety of non-user-visible refactorings, removing various minor warts ranging from indirect imports to typos in comments.
3.38.0 - 2017-11-18
This release overhauls the HealthCheck <#hypothesis.HealthCheck> system in a variety of small ways. It adds no new features, but is nevertheless a minor release because it changes which tests are likely to fail health checks.
The most noticeable effect is that some tests that used to fail health checks will now pass, and some that used to pass will fail. These should all be improvements in accuracy. In particular:
- New failures will usually be because they are now taking into account things like use of data() <#hypothesis.strategies.data> and assume() <#hypothesis.assume> inside the test body.
- New failures may also be because for some classes of example the way data generation performance was measured was artificially faster than real data generation (for most examples that are hitting performance health checks the opposite should be the case).
- Tests that used to fail health checks and now pass do so because the health check system used to run in a way that was subtly different than the main Hypothesis data generation and lacked some of its support for e.g. large examples.
If your data generation is especially slow, you may also see your tests get somewhat faster, as there is no longer a separate health check phase. This will be particularly noticeable when rerunning test failures.
This work was funded by Smarkets <https://smarkets.com/>.
3.37.0 - 2017-11-12
This is a deprecation release for some health check related features.
The following are now deprecated:
- Passing HealthCheck.exception_in_generation to suppress_health_check <#hypothesis.settings.suppress_health_check>. This no longer does anything even when passed - All errors that occur during data generation will now be immediately reraised rather than going through the health check mechanism.
- Passing HealthCheck.random_module to suppress_health_check <#hypothesis.settings.suppress_health_check>. This hasn't done anything for a long time, but was never explicitly deprecated. Hypothesis always seeds the random module when running @given <#hypothesis.given> tests, so this is no longer an error and suppressing it doesn't do anything.
- Passing non-HealthCheck <#hypothesis.HealthCheck> values in suppress_health_check <#hypothesis.settings.suppress_health_check>. This was previously allowed but never did anything useful.
In addition, passing a non-iterable value as suppress_health_check <#hypothesis.settings.suppress_health_check> will now raise an error immediately (it would never have worked correctly, but it would previously have failed later). Some validation error messages have also been updated.
This work was funded by Smarkets <https://smarkets.com/>.
3.36.1 - 2017-11-10
This is a yak shaving release, mostly concerned with our own tests.
While getfullargspec() was documented as deprecated in Python 3.5, it never actually emitted a warning. Our code to silence this (nonexistent) warning has therefore been removed.
We now run our tests with DeprecationWarning as an error, and made some minor changes to our own tests as a result. This required similar upstream updates to coverage <https://pypi.org/project/coverage/> and execnet <https://pypi.org/project/execnet/> (a test-time dependency via pytest-xdist <https://pypi.org/project/pytest-xdist/>).
There is no user-visible change in Hypothesis itself, but we encourage you to consider enabling deprecations as errors in your own tests.
3.36.0 - 2017-11-06
This release adds a setting to the public API, and does some internal cleanup:
- The derandomize <#hypothesis.settings.derandomize> setting is now documented (issue #890 <https://github.com/HypothesisWorks/hypothesis/issues/890>)
- Removed - and disallowed - all 'bare excepts' in Hypothesis (issue #953 <https://github.com/HypothesisWorks/hypothesis/issues/953>)
- Documented the strict setting as deprecated, and updated the build so our docs always match deprecations in the code.
3.35.0 - 2017-11-06
This minor release supports constraining uuids() <#hypothesis.strategies.uuids> to generate a particular version of UUID (issue #721 <https://github.com/HypothesisWorks/hypothesis/issues/721>).
Thanks to Dion Misic for this feature.
3.34.1 - 2017-11-02
This patch updates the documentation to suggest builds(callable) <#hypothesis.strategies.builds> instead of just(callable()) <#hypothesis.strategies.just>.
3.34.0 - 2017-11-02
Hypothesis now emits deprecation warnings if you apply @given <#hypothesis.given> more than once to a target.
Applying @given <#hypothesis.given> repeatedly wraps the target multiple times. Each wrapper will search the space of possible parameters separately. This is equivalent but will be much more inefficient than doing it with a single call to @given <#hypothesis.given>.
For example, instead of @given(booleans()) @given(integers()), you could write @given(booleans(), integers())
3.33.1 - 2017-11-02
This is a bugfix release:
- builds() <#hypothesis.strategies.builds> would try to infer a strategy for required positional arguments of the target from type hints, even if they had been given to builds() <#hypothesis.strategies.builds> as positional arguments (issue #946 <https://github.com/HypothesisWorks/hypothesis/issues/946>). Now it only infers missing required arguments.
- An internal introspection function wrongly reported self as a required argument for bound methods, which might also have affected builds() <#hypothesis.strategies.builds>. Now it knows better.
3.33.0 - 2017-10-16
This release supports strategy inference for more Django field types - you can now omit an argument for Date, Time, Duration, Slug, IP Address, and UUID fields. (issue #642 <https://github.com/HypothesisWorks/hypothesis/issues/642>)
Strategy generation for fields with grouped choices now selects choices from each group, instead of selecting from the group names.
3.32.2 - 2017-10-15
This patch removes the mergedb tool, introduced in Hypothesis 1.7.1 on an experimental basis. It has never actually worked, and the new Hypothesis example database <#database> is designed to make such a tool unnecessary.
3.32.1 - 2017-10-13
This patch has two improvements for strategies based on enumerations.
- from_type() <#hypothesis.strategies.from_type> now handles enumerations correctly, delegating to sampled_from() <#hypothesis.strategies.sampled_from>. Previously it noted that Enum.__init__ has no required arguments and therefore delegated to builds() <#hypothesis.strategies.builds>, which would subsequently fail.
- When sampling from an python:enum.Flag, we also generate combinations of members. Eg for Flag('Permissions', 'READ, WRITE, EXECUTE') we can now generate, Permissions.READ, Permissions.READ|WRITE, and so on.
3.32.0 - 2017-10-09
This changes the default value of the use_coverage setting to True when running on pypy (it was already True on CPython).
It was previously set to False because we expected it to be too slow, but recent benchmarking shows that actually performance of the feature on pypy is fairly acceptable - sometimes it's slower than on CPython, sometimes it's faster, but it's generally within a factor of two either way.
3.31.6 - 2017-10-08
This patch improves the quality of strategies inferred from Numpy dtypes:
- Integer dtypes generated examples with the upper half of their (non-sign) bits set to zero. The inferred strategies can now produce any representable integer.
- Fixed-width unicode- and byte-string dtypes now cap the internal example length, which should improve example and shrink quality.
- Numpy arrays can only store fixed-size strings internally, and allow shorter strings by right-padding them with null bytes. Inferred string strategies no longer generate such values, as they can never be retrieved from an array. This improves shrinking performance by skipping useless values.
This has already been useful in Hypothesis - we found an overflow bug in our Pandas support, and as a result indexes() <#hypothesis.extra.pandas.indexes> and range_indexes() <#hypothesis.extra.pandas.range_indexes> now check that min_size and max_size are at least zero.
3.31.5 - 2017-10-08
This release fixes a performance problem in tests where the use_coverage setting is True.
Tests experience a slow-down proportionate to the amount of code they cover. This is still the case, but the factor is now low enough that it should be unnoticeable. Previously it was large and became much larger in 3.30.4.
3.31.4 - 2017-10-08
from_type() <#hypothesis.strategies.from_type> failed with a very confusing error if passed a NewType (issue #901 <https://github.com/HypothesisWorks/hypothesis/issues/901>). These pseudo-types are now unwrapped correctly, and strategy inference works as expected.
3.31.3 - 2017-10-06
This release makes some small optimisations to our use of coverage that should reduce constant per-example overhead. This is probably only noticeable on examples where the test itself is quite fast. On no-op tests that don't test anything you may see up to a fourfold speed increase (which is still significantly slower than without coverage). On more realistic tests the speed up is likely to be less than that.
3.31.2 - 2017-09-30
This release fixes some formatting and small typos/grammar issues in the documentation, specifically the page docs/settings.rst, and the inline docs for the various settings.
3.31.1 - 2017-09-30
This release improves the handling of deadlines so that they act better with the shrinking process. This fixes issue #892 <https://github.com/HypothesisWorks/hypothesis/issues/892>.
This involves two changes:
- The deadline is raised during the initial generation and shrinking, and then lowered to the set value for final replay. This restricts our attention to examples which exceed the deadline by a more significant margin, which increases their reliability.
- When despite the above a test still becomes flaky because it is significantly faster on rerun than it was on its first run, the error message is now more explicit about the nature of this problem, and includes both the initial test run time and the new test run time.
In addition, this release also clarifies the documentation of the deadline setting slightly to be more explicit about where it applies.
This work was funded by Smarkets <https://smarkets.com/>.
3.31.0 - 2017-09-29
This release blocks installation of Hypothesis on Python 3.3, which reached its end of life date on 2017-09-29 <https://peps.python.org/pep-0398/>.
This should not be of interest to anyone but downstream maintainers - if you are affected, migrate to a secure version of Python as soon as possible or at least seek commercial support.
3.30.4 - 2017-09-27
This release makes several changes:
- It significantly improves Hypothesis's ability to use coverage information to find interesting examples.
- It reduces the default max_examples <#hypothesis.settings.max_examples> setting from 200 to 100. This takes advantage of the improved algorithm meaning fewer examples are typically needed to get the same testing and is sufficiently better at covering interesting behaviour, and offsets some of the performance problems of running under coverage.
- Hypothesis will always try to start its testing with an example that is near minimized.
The new algorithm for 1 also makes some changes to Hypothesis's low level data generation which apply even with coverage turned off. They generally reduce the total amount of data generated, which should improve test performance somewhat. Between this and 3 you should see a noticeable reduction in test runtime (how much so depends on your tests and how much example size affects their performance. On our benchmarks, where data generation dominates, we saw up to a factor of two performance improvement, but it's unlikely to be that large.
3.30.3 - 2017-09-25
This release fixes some formatting and small typos/grammar issues in the documentation, specifically the page docs/details.rst, and some inline docs linked from there.
3.30.2 - 2017-09-24
This release changes Hypothesis's caching approach for functions in hypothesis.strategies. Previously it would have cached extremely aggressively and cache entries would never be evicted. Now it adopts a least-frequently used, least recently used key invalidation policy, and is somewhat more conservative about which strategies it caches.
Workloads which create strategies based on dynamic values, e.g. by using .flatmap() <#hypothesis.strategies.SearchStrategy.flatmap> or composite() <#hypothesis.strategies.composite>, will use significantly less memory.
3.30.1 - 2017-09-22
This release fixes a bug where when running with the use_coverage=True setting inside an existing running instance of coverage, Hypothesis would frequently put files that the coveragerc excluded in the report for the enclosing coverage.
3.30.0 - 2017-09-20
This release introduces two new features:
- When a test fails, either with a health check failure or a falsifying example, Hypothesis will print out a seed that led to that failure, if the test is not already running with a fixed seed. You can then recreate that failure using either the @seed <#hypothesis.seed> decorator or (if you are running pytest) with --hypothesis-seed.
- pytest <https://pypi.org/project/pytest/> users can specify a seed to use for @given <#hypothesis.given> based tests by passing the --hypothesis-seed command line argument.
This work was funded by Smarkets <https://smarkets.com/>.
3.29.0 - 2017-09-19
This release makes Hypothesis coverage aware. Hypothesis now runs all test bodies under coverage, and uses this information to guide its testing.
The use_coverage setting can be used to disable this behaviour if you want to test code that is sensitive to coverage being enabled (either because of performance or interaction with the trace function).
The main benefits of this feature are:
- Hypothesis now observes when examples it discovers cover particular lines or branches and stores them in the database for later.
- Hypothesis will make some use of this information to guide its exploration of the search space and improve the examples it finds (this is currently used only very lightly and will likely improve significantly in future releases).
This also has the following side-effects:
- Hypothesis now has an install time dependency on the coverage <https://pypi.org/project/coverage/> package.
- Tests that are already running Hypothesis under coverage will likely get faster.
- Tests that are not running under coverage now run their test bodies under coverage by default.
This feature is only partially supported under pypy. It is significantly slower than on CPython and is turned off by default as a result, but it should still work correctly if you want to use it.
3.28.3 - 2017-09-18
This release is an internal change that affects how Hypothesis handles calculating certain properties of strategies.
The primary effect of this is that it fixes a bug where use of deferred() <#hypothesis.strategies.deferred> could sometimes trigger an internal assertion error. However the fix for this bug involved some moderately deep changes to how Hypothesis handles certain constructs so you may notice some additional knock-on effects.
In particular the way Hypothesis handles drawing data from strategies that cannot generate any values has changed to bail out sooner than it previously did. This may speed up certain tests, but it is unlikely to make much of a difference in practice for tests that were not already failing with Unsatisfiable.
3.28.2 - 2017-09-18
This is a patch release that fixes a bug in the hypothesis.extra.pandas <#module-hypothesis.extra.pandas> documentation where it incorrectly referred to column() <#hypothesis.extra.pandas.column> instead of columns() <#hypothesis.extra.pandas.columns>.
3.28.1 - 2017-09-16
This is a refactoring release. It moves a number of internal uses of namedtuple() over to using attrs based classes, and removes a couple of internal namedtuple classes that were no longer in use.
It should have no user visible impact.
3.28.0 - 2017-09-15
This release adds support for testing pandas <https://pypi.org/project/pandas/> via the hypothesis.extra.pandas <#hypothesis-pandas> module.
It also adds a dependency on attrs <https://pypi.org/project/attrs/>.
This work was funded by Stripe <https://stripe.com/>.
3.27.1 - 2017-09-14
This release fixes some formatting and broken cross-references in the documentation, which includes editing docstrings - and thus a patch release.
3.27.0 - 2017-09-13
This release introduces a deadline <#hypothesis.settings.deadline> setting to Hypothesis.
When set this turns slow tests into errors. By default it is unset but will warn if you exceed 200ms, which will become the default value in a future release.
This work was funded by Smarkets <https://smarkets.com/>.
3.26.0 - 2017-09-12
Hypothesis now emits deprecation warnings if you are using the legacy SQLite example database format, or the tool for merging them. These were already documented as deprecated, so this doesn't change their deprecation status, only that we warn about it.
3.25.1 - 2017-09-12
This release fixes a bug with generating numpy datetime and timedelta types: When inferring the strategy from the dtype, datetime and timedelta dtypes with sub-second precision would always produce examples with one second resolution. Inferring a strategy from a time dtype will now always produce example with the same precision.
3.25.0 - 2017-09-12
This release changes how Hypothesis shrinks and replays examples to take into account that it can encounter new bugs while shrinking the bug it originally found. Previously it would end up replacing the originally found bug with the new bug and show you only that one. Now it is (often) able to recognise when two bugs are distinct and when it finds more than one will show both.
3.24.2 - 2017-09-11
This release removes the (purely internal and no longer useful) strategy_test_suite function and the corresponding strategytests module.
3.24.1 - 2017-09-06
This release improves the reduction of examples involving floating point numbers to produce more human readable examples.
It also has some general purpose changes to the way the minimizer works internally, which may see some improvement in quality and slow down of test case reduction in cases that have nothing to do with floating point numbers.
3.24.0 - 2017-09-05
Hypothesis now emits deprecation warnings if you use some_strategy.example() inside a test function or strategy definition (this was never intended to be supported, but is sufficiently widespread that it warrants a deprecation path).
3.23.3 - 2017-09-05
This is a bugfix release for decimals() <#hypothesis.strategies.decimals> with the places argument.
- No longer fails health checks (issue #725 <https://github.com/HypothesisWorks/hypothesis/issues/725>, due to internal filtering)
- Specifying a min_value and max_value without any decimals with places places between them gives a more useful error message.
- Works for any valid arguments, regardless of the decimal precision context.
3.23.2 - 2017-09-01
This is a small refactoring release that removes a now-unused parameter to an internal API. It shouldn't have any user visible effect.
3.23.1 - 2017-09-01
Hypothesis no longer propagates the dynamic scope of settings into strategy definitions.
This release is a small change to something that was never part of the public API and you will almost certainly not notice any effect unless you're doing something surprising, but for example the following code will now give a different answer in some circumstances:
import hypothesis.strategies as st from hypothesis import settings CURRENT_SETTINGS = st.builds(lambda: settings.default)
(We don't actually encourage you writing code like this)
Previously this would have generated the settings that were in effect at the point of definition of CURRENT_SETTINGS. Now it will generate the settings that are used for the current test.
It is very unlikely to be significant enough to be visible, but you may also notice a small performance improvement.
3.23.0 - 2017-08-31
This release adds a unique argument to arrays() <#hypothesis.extra.numpy.arrays> which behaves the same ways as the corresponding one for lists() <#hypothesis.strategies.lists>, requiring all of the elements in the generated array to be distinct.
3.22.2 - 2017-08-29
This release fixes an issue where Hypothesis would raise a TypeError when using the datetime-related strategies if running with PYTHONOPTIMIZE=2. This bug was introduced in 3.20.0. (See issue #822 <https://github.com/HypothesisWorks/hypothesis/issues/822>)
3.22.1 - 2017-08-28
Hypothesis now transparently handles problems with an internal unicode cache, including file truncation or read-only filesystems (issue #767 <https://github.com/HypothesisWorks/hypothesis/issues/767>). Thanks to Sam Hames for the patch.
3.22.0 - 2017-08-26
This release provides what should be a substantial performance improvement to numpy arrays generated using provided numpy support <#hypothesis-numpy>, and adds a new fill_value argument to arrays() <#hypothesis.extra.numpy.arrays> to control this behaviour.
This work was funded by Stripe <https://stripe.com/>.
3.21.3 - 2017-08-26
This release fixes some extremely specific circumstances that probably have never occurred in the wild where users of deferred() <#hypothesis.strategies.deferred> might have seen a python:RuntimeError from too much recursion, usually in cases where no valid example could have been generated anyway.
3.21.2 - 2017-08-25
This release fixes some minor bugs in argument validation:
- hypothesis.extra.numpy <#hypothesis-numpy> dtype strategies would raise an internal error instead of an InvalidArgument exception when passed an invalid endianness specification.
- fractions() <#hypothesis.strategies.fractions> would raise an internal error instead of an InvalidArgument if passed float("nan") as one of its bounds.
- The error message for passing float("nan") as a bound to various strategies has been improved.
- Various bound arguments will now raise InvalidArgument in cases where they would previously have raised an internal TypeError or ValueError from the relevant conversion function.
- streaming() would not have emitted a deprecation warning when called with an invalid argument.
3.21.1 - 2017-08-24
This release fixes a bug where test failures that were the result of an @example <#hypothesis.example> would print an extra stack trace before re-raising the exception.
3.21.0 - 2017-08-23
This release deprecates Hypothesis's strict mode, which turned Hypothesis's deprecation warnings into errors. Similar functionality can be achieved by using simplefilter('error', HypothesisDeprecationWarning).
3.20.0 - 2017-08-22
This release renames the relevant arguments on the datetimes() <#hypothesis.strategies.datetimes>, dates() <#hypothesis.strategies.dates>, times() <#hypothesis.strategies.times>, and timedeltas() <#hypothesis.strategies.timedeltas> strategies to min_value and max_value, to make them consistent with the other strategies in the module.
The old argument names are still supported but will emit a deprecation warning when used explicitly as keyword arguments. Arguments passed positionally will go to the new argument names and are not deprecated.
3.19.3 - 2017-08-22
This release provides a major overhaul to the internals of how Hypothesis handles shrinking.
This should mostly be visible in terms of getting better examples for tests which make heavy use of composite() <#hypothesis.strategies.composite>, data() <#hypothesis.strategies.data> or .flatmap() <#hypothesis.strategies.SearchStrategy.flatmap> where the data drawn depends a lot on previous choices, especially where size parameters are affected. Previously Hypothesis would have struggled to reliably produce good examples here. Now it should do much better. Performance should also be better for examples with a non-zero min_size.
You may see slight changes to example generation (e.g. improved example diversity) as a result of related changes to internals, but they are unlikely to be significant enough to notice.
3.19.2 - 2017-08-21
This release fixes two bugs in hypothesis.extra.numpy <#module-hypothesis.extra.numpy>:
- unicode_string_dtypes() <#hypothesis.extra.numpy.unicode_string_dtypes> didn't work at all due to an incorrect dtype specifier. Now it does.
- Various impossible conditions would have been accepted but would error when they fail to produced any example. Now they raise an explicit InvalidArgument error.
3.19.1 - 2017-08-21
This is a bugfix release for issue #739 <https://github.com/HypothesisWorks/hypothesis/issues/739>, where bounds for fractions() <#hypothesis.strategies.fractions> or floating-point decimals() <#hypothesis.strategies.decimals> were not properly converted to integers before passing them to the integers strategy. This excluded some values that should have been possible, and could trigger internal errors if the bounds lay between adjacent integers.
You can now bound fractions() <#hypothesis.strategies.fractions> with two arbitrarily close fractions.
It is now an explicit error to supply a min_value, max_value, and max_denominator to fractions() <#hypothesis.strategies.fractions> where the value bounds do not include a fraction with denominator at most max_denominator.
3.19.0 - 2017-08-20
This release adds the from_regex() <#hypothesis.strategies.from_regex> strategy, which generates strings that contain a match of a regular expression.
Thanks to Maxim Kulkin for creating the hypothesis-regex <https://github.com/maximkulkin/hypothesis-regex> package and then helping to upstream it! (issue #662 <https://github.com/HypothesisWorks/hypothesis/issues/662>)
3.18.5 - 2017-08-18
This is a bugfix release for integers() <#hypothesis.strategies.integers>. Previously the strategy would hit an internal assertion if passed non-integer bounds for min_value and max_value that had no integers between them. The strategy now raises InvalidArgument instead.
3.18.4 - 2017-08-18
Release to fix a bug where mocks can be used as test runners under certain conditions. Specifically, if a mock is injected into a test via pytest fixtures or patch decorators, and that mock is the first argument in the list, hypothesis will think it represents self and turns the mock into a test runner. If this happens, the affected test always passes because the mock is executed instead of the test body. Sometimes, it will also fail health checks.
Fixes issue #491 <https://github.com/HypothesisWorks/hypothesis/issues/491> and a section of issue #198 <https://github.com/HypothesisWorks/hypothesis/issues/198>. Thanks to Ben Peterson for this bug fix.
3.18.3 - 2017-08-17
This release should improve the performance of some tests which experienced a slow down as a result of the 3.13.0 release.
Tests most likely to benefit from this are ones that make extensive use of min_size parameters, but others may see some improvement as well.
3.18.2 - 2017-08-16
This release fixes a bug introduced in 3.18.0. If the arguments include_characters and exclude_characters to characters() <#hypothesis.strategies.characters> contained overlapping elements, then an InvalidArgument exception would be raised.
Thanks to Zac Hatfield-Dodds for reporting and fixing this.
3.18.1 - 2017-08-14
This is a bug fix release to fix issue #780 <https://github.com/HypothesisWorks/hypothesis/issues/780>, where sets() <#hypothesis.strategies.sets> and similar would trigger health check errors if their element strategy could only produce one element (e.g. if it was just() <#hypothesis.strategies.just>).
3.18.0 - 2017-08-13
This is a feature release:
- characters() <#hypothesis.strategies.characters> now accepts include_characters, particular characters which will be added to those it produces. (issue #668 <https://github.com/HypothesisWorks/hypothesis/issues/668>)
- A bug fix for the internal function _union_interval_lists(), and a rename to _union_intervals(). It now correctly handles all cases where intervals overlap, and it always returns the result as a tuple for tuples.
Thanks to Alex Willmer for these.
3.17.0 - 2017-08-07
This release documents the previously undocumented phases feature in Phase <#hypothesis.Phase>, making it part of the public API. It also updates how the example database is used. Principally:
- The reuse <#hypothesis.Phase.reuse> phase will now correctly control whether examples from the database are run (it previously did exactly the wrong thing and controlled whether examples would be saved).
- Hypothesis will no longer try to rerun all previously failing examples. Instead it will replay the smallest previously failing example and a selection of other examples that are likely to trigger any other bugs that will found. This prevents a previous failure from dominating your tests unnecessarily.
- As a result of the previous change, Hypothesis will be slower about clearing out old examples from the database that are no longer failing (because it can only clear out ones that it actually runs).
3.16.1 - 2017-08-07
This release makes an implementation change to how Hypothesis handles certain internal constructs.
The main effect you should see is improvement to the behaviour and performance of collection types, especially ones with a min_size parameter. Many cases that would previously fail due to being unable to generate enough valid examples will now succeed, and other cases should run slightly faster.
3.16.0 - 2017-08-04
This release introduces a deprecation of the timeout feature. This results in the following changes:
- Creating a settings object with an explicit timeout will emit a deprecation warning.
- If your test stops because it hits the timeout (and has not found a bug) then it will emit a deprecation warning.
- There is a new value unlimited which you can import from hypothesis. settings(timeout=unlimited) will not cause a deprecation warning.
- There is a new health check, hung_test, which will trigger after a test has been running for five minutes if it is not suppressed.
3.15.0 - 2017-08-04
This release deprecates two strategies, choices() and streaming().
Both of these are somewhat confusing to use and are entirely redundant since the introduction of the data() <#hypothesis.strategies.data> strategy for interactive drawing in tests, and their use should be replaced with direct use of data() <#hypothesis.strategies.data> instead.
3.14.2 - 2017-08-03
This fixes a bug where Hypothesis would not work correctly on Python 2.7 if you had the python:typing module backport <https://pypi.org/project/typing/> installed.
3.14.1 - 2017-08-02
This raises the maximum depth at which Hypothesis starts cutting off data generation to a more reasonable value which it is harder to hit by accident.
This resolves (issue #751 <https://github.com/HypothesisWorks/hypothesis/issues/751>), in which some examples which previously worked would start timing out, but it will also likely improve the data generation quality for complex data types.
3.14.0 - 2017-07-23
Hypothesis now understands inline type annotations (issue #293 <https://github.com/HypothesisWorks/hypothesis/issues/293>):
- If the target of builds() <#hypothesis.strategies.builds> has type annotations, a default strategy for missing required arguments is selected based on the type. Type-based strategy selection will only override a default if you pass hypothesis.infer <#hypothesis.infer> as a keyword argument.
- If @given <#hypothesis.given> wraps a function with type annotations, you can pass infer <#hypothesis.infer> as a keyword argument and the appropriate strategy will be substituted.
- You can check what strategy will be inferred for a type with the new from_type() <#hypothesis.strategies.from_type> function.
- register_type_strategy() <#hypothesis.strategies.register_type_strategy> teaches Hypothesis which strategy to infer for custom or unknown types. You can provide a strategy, or for more complex cases a function which takes the type and returns a strategy.
3.13.1 - 2017-07-20
This is a bug fix release for issue #514 <https://github.com/HypothesisWorks/hypothesis/issues/514> - Hypothesis would continue running examples after a SkipTest exception was raised, including printing a falsifying example. Skip exceptions from the standard python:unittest module, and pytest, nose, or unittest2 modules now abort the test immediately without printing output.
3.13.0 - 2017-07-16
This release has two major aspects to it: The first is the introduction of deferred() <#hypothesis.strategies.deferred>, which allows more natural definition of recursive (including mutually recursive) strategies.
The second is a number of engine changes designed to support this sort of strategy better. These should have a knock-on effect of also improving the performance of any existing strategies that currently generate a lot of data or involve heavy nesting by reducing their typical example size.
3.12.0 - 2017-07-07
This release makes some major internal changes to how Hypothesis represents data internally, as a prelude to some major engine changes that should improve data quality. There are no API changes, but it's a significant enough internal change that a minor version bump seemed warranted.
User facing impact should be fairly mild, but includes:
- All existing examples in the database will probably be invalidated. Hypothesis handles this automatically, so you don't need to do anything, but if you see all your examples disappear that's why.
- Almost all data distributions have changed significantly. Possibly for the better, possibly for the worse. This may result in new bugs being found, but it may also result in Hypothesis being unable to find bugs it previously did.
- Data generation may be somewhat faster if your existing bottleneck was in draw_bytes (which is often the case for large examples).
- Shrinking will probably be slower, possibly significantly.
If you notice any effects you consider to be a significant regression, please open an issue about them.
3.11.6 - 2017-06-19
This release involves no functionality changes, but is the first to ship wheels as well as an sdist.
3.11.5 - 2017-06-18
This release provides a performance improvement to shrinking. For cases where there is some non-trivial "boundary" value (e.g. the bug happens for all values greater than some other value), shrinking should now be substantially faster. Other types of bug will likely see improvements too.
This may also result in some changes to the quality of the final examples - it may sometimes be better, but is more likely to get slightly worse in some edge cases. If you see any examples where this happens in practice, please report them.
3.11.4 - 2017-06-17
This is a bugfix release: Hypothesis now prints explicit examples when running in verbose mode. (issue #313 <https://github.com/HypothesisWorks/hypothesis/issues/313>)
3.11.3 - 2017-06-11
This is a bugfix release: Hypothesis no longer emits a warning if you try to use sampled_from() <#hypothesis.strategies.sampled_from> with python:collections.OrderedDict. (issue #688 <https://github.com/HypothesisWorks/hypothesis/issues/688>)
3.11.2 - 2017-06-10
This is a documentation release. Several outdated snippets have been updated or removed, and many cross-references are now hyperlinks.
3.11.1 - 2017-05-28
This is a minor ergonomics release. Tracebacks shown by pytest no longer include Hypothesis internals for test functions decorated with @given <#hypothesis.given>.
3.11.0 - 2017-05-23
This is a feature release, adding datetime-related strategies to the core strategies.
timezones() <#hypothesis.extra.pytz.timezones> allows you to sample pytz timezones from the Olsen database. Use directly in a recipe for tz-aware datetimes, or compose with none() <#hypothesis.strategies.none> to allow a mix of aware and naive output.
The new dates() <#hypothesis.strategies.dates>, times() <#hypothesis.strategies.times>, datetimes() <#hypothesis.strategies.datetimes>, and timedeltas() <#hypothesis.strategies.timedeltas> strategies are all constrained by objects of their type. This means that you can generate dates bounded by a single day (i.e. a single date), or datetimes constrained to the microsecond.
times() <#hypothesis.strategies.times> and datetimes() <#hypothesis.strategies.datetimes> take an optional timezones= argument, which defaults to none() <#hypothesis.strategies.none> for naive times. You can use our extra strategy based on pytz, or roll your own timezones strategy with dateutil or even the standard library.
The old dates, times, and datetimes strategies in hypothesis.extra.datetimes are deprecated in favor of the new core strategies, which are more flexible and have no dependencies.
3.10.0 - 2017-05-22
Hypothesis now uses python:inspect.getfullargspec() internally. On Python 2, there are no visible changes.
On Python 3 @given <#hypothesis.given> and @composite <#hypothesis.strategies.composite> now preserve PEP 3107 <https://peps.python.org/pep-3107/> annotations on the decorated function. Keyword-only arguments are now either handled correctly (e.g. @composite <#hypothesis.strategies.composite>), or caught in validation instead of silently discarded or raising an unrelated error later (e.g. @given <#hypothesis.given>).
3.9.1 - 2017-05-22
This is a bugfix release: the default field mapping for a DateTimeField in the Django extra now respects the USE_TZ setting when choosing a strategy.
3.9.0 - 2017-05-19
This is feature release, expanding the capabilities of the decimals() <#hypothesis.strategies.decimals> strategy.
- The new (optional) places argument allows you to generate decimals with a certain number of places (e.g. cents, thousandths, satoshis).
- If allow_infinity is None, setting min_bound no longer excludes positive infinity and setting max_value no longer excludes negative infinity.
- All of NaN, -Nan, sNaN, and -sNaN may now be drawn if allow_nan is True, or if allow_nan is None and min_value or max_value is None.
- min_value and max_value may be given as decimal strings, e.g. "1.234".
3.8.5 - 2017-05-16
Hypothesis now imports python:sqlite3 when a SQLite database is used, rather than at module load, improving compatibility with Python implementations compiled without SQLite support (such as BSD or Jython).
3.8.4 - 2017-05-16
This is a compatibility bugfix release. sampled_from() <#hypothesis.strategies.sampled_from> no longer raises a deprecation warning when sampling from an python:enum.Enum, as all enums have a reliable iteration order.
3.8.3 - 2017-05-09
This release removes a version check for older versions of pytest <https://pypi.org/project/pytest/> when using the Hypothesis pytest plugin. The pytest plugin will now run unconditionally on all versions of pytest. This breaks compatibility with any version of pytest prior to 2.7.0 (which is more than two years old).
The primary reason for this change is that the version check was a frequent source of breakage when pytest change their versioning scheme. If you are not working on pytest itself and are not running a very old version of it, this release probably doesn't affect you.
3.8.2 - 2017-04-26
This is a code reorganisation release that moves some internal test helpers out of the main source tree so as to not have changes to them trigger releases in future.
3.8.1 - 2017-04-26
This is a documentation release. Almost all code examples are now doctests checked in CI, eliminating stale examples.
3.8.0 - 2017-04-23
This is a feature release, adding the iterables() <#hypothesis.strategies.iterables> strategy, equivalent to lists(...).map(iter) but with a much more useful repr. You can use this strategy to check that code doesn't accidentally depend on sequence properties such as indexing support or repeated iteration.
3.7.4 - 2017-04-22
This patch fixes a bug in 3.7.3, where using @example <#hypothesis.example> and a pytest fixture in the same test could cause the test to fail to fill the arguments, and throw a TypeError.
3.7.3 - 2017-04-21
This release should include no user visible changes and is purely a refactoring release. This modularises the behaviour of the core given() <#hypothesis.given> function, breaking it up into smaller and more accessible parts, but its actual behaviour should remain unchanged.
3.7.2 - 2017-04-21
This reverts an undocumented change in 3.7.1 which broke installation on debian stable: The specifier for the hypothesis[django] extra_requires had introduced a wild card, which was not supported on the default version of pip.
3.7.1 - 2017-04-21
This is a bug fix and internal improvements release.
- In particular Hypothesis now tracks a tree of where it has already explored. This allows it to avoid some classes of duplicate examples, and significantly improves the performance of shrinking failing examples by allowing it to skip some shrinks that it can determine can't possibly work.
- Hypothesis will no longer seed the global random arbitrarily unless you have asked it to using random_module() <#hypothesis.strategies.random_module>
- Shrinking would previously have not worked correctly in some special cases on Python 2, and would have resulted in suboptimal examples.
3.7.0 - 2017-03-20
This is a feature release.
New features:
- Rule based stateful testing now has an @invariant <#hypothesis.stateful.invariant> decorator that specifies methods that are run after init and after every step, allowing you to encode properties that should be true at all times. Thanks to Tom Prince for this feature.
- The decimals() <#hypothesis.strategies.decimals> strategy now supports allow_nan and allow_infinity flags.
- There are significantly more strategies available for numpy <#hypothesis-numpy>, including for generating arbitrary data types. Thanks to Zac Hatfield Dodds for this feature.
- When using the data() <#hypothesis.strategies.data> strategy you can now add a label as an argument to draw(), which will be printed along with the value when an example fails. Thanks to Peter Inglesby for this feature.
Bug fixes:
- Bug fix: composite() <#hypothesis.strategies.composite> now preserves functions' docstrings.
- The build is now reproducible and doesn't depend on the path you build it from. Thanks to Chris Lamb for this feature.
- numpy strategies for the void data type did not work correctly. Thanks to Zac Hatfield Dodds for this fix.
There have also been a number of performance optimizations:
- The permutations() <#hypothesis.strategies.permutations> strategy is now significantly faster to use for large lists (the underlying algorithm has gone from O(n^2) to O(n)).
- Shrinking of failing test cases should have got significantly faster in some circumstances where it was previously struggling for a long time.
- Example generation now involves less indirection, which results in a small speedup in some cases (small enough that you won't really notice it except in pathological cases).
3.6.1 - 2016-12-20
This release fixes a dependency problem and makes some small behind the scenes improvements.
- The fake-factory dependency was renamed to faker. If you were depending on it through hypothesis[django] or hypothesis[fake-factory] without pinning it yourself then it would have failed to install properly. This release changes it so that hypothesis[fakefactory] (which can now also be installed as hypothesis[faker]) will install the renamed faker package instead.
- This release also removed the dependency of hypothesis[django] on hypothesis[fakefactory] - it was only being used for emails. These now use a custom strategy that isn't from fakefactory. As a result you should also see performance improvements of tests which generated User objects or other things with email fields, as well as better shrinking of email addresses.
- The distribution of code using nested calls to one_of() <#hypothesis.strategies.one_of> or the | operator for combining strategies has been improved, as branches are now flattened to give a more uniform distribution.
- Examples using composite() <#hypothesis.strategies.composite> or .flatmap() <#hypothesis.strategies.SearchStrategy.flatmap> should now shrink better. In particular this will affect things which work by first generating a length and then generating that many items, which have historically not shrunk very well.
3.6.0 - 2016-10-31
This release reverts Hypothesis to its old pretty printing of lambda functions based on attempting to extract the source code rather than decompile the bytecode. This is unfortunately slightly inferior in some cases and may result in you occasionally seeing things like lambda x: <unknown> in statistics reports and strategy reprs.
This removes the dependencies on uncompyle6, xdis and spark-parser.
The reason for this is that the new functionality was based on uncompyle6, which turns out to introduce a hidden GPLed dependency - it in turn depended on xdis, and although the library was licensed under the MIT license, it contained some GPL licensed source code and thus should have been released under the GPL.
My interpretation is that Hypothesis itself was never in violation of the GPL (because the license it is under, the Mozilla Public License v2, is fully compatible with being included in a GPL licensed work), but I have not consulted a lawyer on the subject. Regardless of the answer to this question, adding a GPLed dependency will likely cause a lot of users of Hypothesis to inadvertently be in violation of the GPL.
As a result, if you are running Hypothesis 3.5.x you really should upgrade to this release immediately.
3.5.3 - 2016-10-05
This is a bug fix release.
Bugs fixed:
- If the same test was running concurrently in two processes and there were examples already in the test database which no longer failed, Hypothesis would sometimes fail with a FileNotFoundError (IOError on Python 2) because an example it was trying to read was deleted before it was read. (issue #372 <https://github.com/HypothesisWorks/hypothesis/issues/372>).
- Drawing from an integers() <#hypothesis.strategies.integers> strategy with both a min_value and a max_value would reject too many examples needlessly. Now it repeatedly redraws until satisfied. (pull request #366 <https://github.com/HypothesisWorks/hypothesis/pull/366>. Thanks to Calen Pennington for the contribution).
3.5.2 - 2016-09-24
This is a bug fix release.
- The Hypothesis pytest plugin broke pytest support for doctests. Now it doesn't.
3.5.1 - 2016-09-23
This is a bug fix release.
- Hypothesis now runs cleanly in -B and -BB modes, avoiding mixing bytes and unicode.
- python:unittest.TestCase tests would not have shown up in the new statistics mode. Now they do.
- Similarly, stateful tests would not have shown up in statistics and now they do.
- Statistics now print with pytest node IDs (the names you'd get in pytest verbose mode).
3.5.0 - 2016-09-22
This is a feature release.
- fractions() <#hypothesis.strategies.fractions> and decimals() <#hypothesis.strategies.decimals> strategies now support min_value and max_value parameters. Thanks go to Anne Mulhern for the development of this feature.
- The Hypothesis pytest plugin now supports a --hypothesis-show-statistics parameter that gives detailed statistics about the tests that were run. Huge thanks to Jean-Louis Fuchs and Adfinis-SyGroup for funding the development of this feature.
- There is a new event() <#hypothesis.event> function that can be used to add custom statistics.
Additionally there have been some minor bug fixes:
- In some cases Hypothesis should produce fewer duplicate examples (this will mostly only affect cases with a single parameter).
- pytest <https://pypi.org/project/pytest/> command line parameters are now under an option group for Hypothesis (thanks to David Keijser for fixing this)
- Hypothesis would previously error if you used PEP 3107 <https://peps.python.org/pep-3107/> function annotations on your tests under Python 3.4.
- The repr of many strategies using lambdas has been improved to include the lambda body (this was previously supported in many but not all cases).
3.4.2 - 2016-07-13
This is a bug fix release, fixing a number of problems with the settings system:
- Test functions defined using @given <#hypothesis.given> can now be called from other threads (issue #337 <https://github.com/HypothesisWorks/hypothesis/issues/337>)
- Attempting to delete a settings property would previously have silently done the wrong thing. Now it raises an AttributeError.
- Creating a settings object with a custom database_file parameter was silently getting ignored and the default was being used instead. Now it's not.
3.4.1 - 2016-07-07
This is a bug fix release for a single bug:
- On Windows when running two Hypothesis processes in parallel (e.g. using pytest-xdist <https://pypi.org/project/pytest-xdist/>) they could race with each other and one would raise an exception due to the non-atomic nature of file renaming on Windows and the fact that you can't rename over an existing file. This is now fixed.
3.4.0 - 2016-05-27
This release is entirely provided by Lucas Wiman <https://github.com/lucaswiman>:
Strategies constructed by the Django extra will now respect much more of Django's validations out of the box. Wherever possible, full_clean() should succeed.
In particular:
- The max_length, blank and choices kwargs are now respected.
- Add support for DecimalField.
- If a field includes validators, the list of validators are used to filter the field strategy.
3.3.0 - 2016-05-27
This release went wrong and is functionally equivalent to 3.2.0. Ignore it.
3.2.0 - 2016-05-19
This is a small single-feature release:
- All tests using @given <#hypothesis.given> now fix the global random seed. This removes the health check for that. If a non-zero seed is required for the final falsifying example, it will be reported. Otherwise Hypothesis will assume randomization was not a significant factor for the test and be silent on the subject. If you use random_module() <#hypothesis.strategies.random_module> this will continue to work and will always display the seed.
3.1.3 - 2016-05-01
Single bug fix release
- Another charmap problem. In 3.1.2 text() <#hypothesis.strategies.text> and characters() <#hypothesis.strategies.characters> would break on systems which had /tmp mounted on a different partition than the Hypothesis storage directory (usually in home). This fixes that.
3.1.2 - 2016-04-30
Single bug fix release:
- Anything which used a text() <#hypothesis.strategies.text> or characters() <#hypothesis.strategies.characters> strategy was broken on Windows and I hadn't updated appveyor to use the new repository location so I didn't notice. This is now fixed and windows support should work correctly.
3.1.1 - 2016-04-29
Minor bug fix release.
- Fix concurrency issue when running tests that use text() <#hypothesis.strategies.text> from multiple processes at once (issue #302 <https://github.com/HypothesisWorks/hypothesis/issues/302>, thanks to Alex Chan).
- Improve performance of code using lists() <#hypothesis.strategies.lists> with max_size (thanks to Cristi Cobzarenco).
- Fix install on Python 2 with ancient versions of pip so that it installs the enum34 <https://pypi.org/project/enum34/> backport (thanks to Donald Stufft for telling me how to do this).
- Remove duplicated __all__ exports from hypothesis.strategies (thanks to Piët Delport).
- Update headers to point to new repository location.
- Allow use of strategies that can't be used in find() (e.g. choices()) in stateful testing.
3.1.0 - 2016-03-06
- Add a nothing() <#hypothesis.strategies.nothing> strategy that never successfully generates values.
- sampled_from() <#hypothesis.strategies.sampled_from> and one_of() <#hypothesis.strategies.one_of> can both now be called with an empty argument list, in which case they also never generate any values.
- one_of() <#hypothesis.strategies.one_of> may now be called with a single argument that is a collection of strategies as well as varargs.
- Add a runner() <#hypothesis.strategies.runner> strategy which returns the instance of the current test object if there is one.
- 'Bundle' for RuleBasedStateMachine is now a normal(ish) strategy and can be used as such.
- Tests using RuleBasedStateMachine should now shrink significantly better.
- Hypothesis now uses a pretty-printing library internally, compatible with IPython's pretty printing protocol (actually using the same code). This may improve the quality of output in some cases.
- Add a 'phases' setting that allows more fine grained control over which parts of the process Hypothesis runs
- Add a suppress_health_check setting which allows you to turn off specific health checks in a fine grained manner.
- Fix a bug where lists of non fixed size would always draw one more element than they included. This mostly didn't matter, but if would cause problems with empty strategies or ones with side effects.
- Add a mechanism to the Django model generator to allow you to explicitly request the default value (thanks to Jeremy Thurgood for this one).
3.0.5 - 2016-02-25
- Fix a bug where Hypothesis would now error on pytest <https://pypi.org/project/pytest/> development versions.
3.0.4 - 2016-02-24
- Fix a bug where Hypothesis would error when running on Python 2.7.3 or earlier because it was trying to pass a python:bytearray object to python:struct.unpack() (which is only supported since 2.7.4).
3.0.3 - 2016-02-23
- Fix version parsing of pytest to work with pytest release candidates
- More general handling of the health check problem where things could fail because of a cache miss - now one "free" example is generated before the start of the health check run.
3.0.2 - 2016-02-18
- Under certain circumstances, strategies involving text() <#hypothesis.strategies.text> buried inside some other strategy (e.g. text().filter(...) or recursive(text(), ...)) would cause a test to fail its health checks the first time it ran. This was caused by having to compute some related data and cache it to disk. On travis or anywhere else where the .hypothesis directory was recreated this would have caused the tests to fail their health check on every run. This is now fixed for all the known cases, although there could be others lurking.
3.0.1 - 2016-02-18
- Fix a case where it was possible to trigger an "Unreachable" assertion when running certain flaky stateful tests.
- Improve shrinking of large stateful tests by eliminating a case where it was hard to delete early steps.
- Improve efficiency of drawing binary(min_size=n, max_size=n) <#hypothesis.strategies.binary> significantly by provide a custom implementation for fixed size blocks that can bypass a lot of machinery.
- Set default home directory based on the current working directory at the point Hypothesis is imported, not whenever the function first happens to be called.
3.0.0 - 2016-02-17
Codename: This really should have been 2.1.
Externally this looks like a very small release. It has one small breaking change that probably doesn't affect anyone at all (some behaviour that never really worked correctly is now outright forbidden) but necessitated a major version bump and one visible new feature.
Internally this is a complete rewrite. Almost nothing other than the public API is the same.
New features:
- Addition of data() <#hypothesis.strategies.data> strategy which allows you to draw arbitrary data interactively within the test.
- New "exploded" database format which allows you to more easily check the example database into a source repository while supporting merging.
- Better management of how examples are saved in the database.
- Health checks will now raise as errors when they fail. It was too easy to have the warnings be swallowed entirely.
New limitations:
- choices() and streaming() strategies may no longer be used with find(). Neither may data() <#hypothesis.strategies.data> (this is the change that necessitated a major version bump).
Feature removal:
- The ForkingTestCase executor has gone away. It may return in some more working form at a later date.
Performance improvements:
- A new model which allows flatmap, composite strategies and stateful testing to perform much better. They should also be more reliable.
- Filtering may in some circumstances have improved significantly. This will help especially in cases where you have lots of values with individual filters on them, such as lists(x.filter(...)).
- Modest performance improvements to the general test runner by avoiding expensive operations
In general your tests should have got faster. If they've instead got significantly slower, I'm interested in hearing about it.
Data distribution:
The data distribution should have changed significantly. This may uncover bugs the previous version missed. It may also miss bugs the previous version could have uncovered. Hypothesis is now producing less strongly correlated data than it used to, but the correlations are extended over more of the structure.
Shrinking:
Shrinking quality should have improved. In particular Hypothesis can now perform simultaneous shrinking of separate examples within a single test (previously it was only able to do this for elements of a single collection). In some cases performance will have improved, in some cases it will have got worse but generally shouldn't have by much.
Older versions
2.0.0 - 2016-01-10
Codename: A new beginning
This release cleans up all of the legacy that accrued in the course of Hypothesis 1.0. These are mostly things that were emitting deprecation warnings in 1.19.0, but there were a few additional changes.
In particular:
- non-strategy values will no longer be converted to strategies when used in given or find.
- FailedHealthCheck is now an error and not a warning.
- Handling of non-ascii reprs in user types have been simplified by using raw strings in more places in Python 2.
- given no longer allows mixing positional and keyword arguments.
- given no longer works with functions with defaults.
- given no longer turns provided arguments into defaults - they will not appear in the argspec at all.
- the basic() strategy no longer exists.
- the n_ary_tree strategy no longer exists.
- the average_list_length setting no longer exists. Note: If you're using using recursive() this will cause you a significant slow down. You should pass explicit average_size parameters to collections in recursive calls.
- @rule can no longer be applied to the same method twice.
- Python 2.6 and 3.3 are no longer officially supported, although in practice they still work fine.
This also includes two non-deprecation changes:
- given's keyword arguments no longer have to be the rightmost arguments and can appear anywhere in the method signature.
- The max_shrinks setting would sometimes not have been respected.
1.19.0 - 2016-01-09
Codename: IT COMES
This release heralds the beginning of a new and terrible age of Hypothesis 2.0.
It's primary purpose is some final deprecations prior to said release. The goal is that if your code emits no warnings under this release then it will probably run unchanged under Hypothesis 2.0 (there are some caveats to this: 2.0 will drop support for some Python versions, and if you're using internal APIs then as usual that may break without warning).
It does have two new features:
- New @seed() decorator which allows you to manually seed a test. This may be harmlessly combined with and overrides the derandomize setting.
- settings objects may now be used as a decorator to fix those settings to a particular @given test.
API changes (old usage still works but is deprecated):
- Settings has been renamed to settings (lower casing) in order to make the decorator usage more natural.
- Functions for the storage directory that were in hypothesis.settings are now in a new hypothesis.configuration module.
Additional deprecations:
- the average_list_length setting has been deprecated in favour of being explicit.
- the basic() strategy has been deprecated as it is impossible to support it under a Conjecture based model, which will hopefully be implemented at some point in the 2.x series.
- the n_ary_tree strategy (which was never actually part of the public API) has been deprecated.
- Passing settings or random as keyword arguments to given is deprecated (use the new functionality instead)
Bug fixes:
- No longer emit PendingDeprecationWarning for __iter__ and StopIteration in streaming() values.
- When running in health check mode with non strict, don't print quite so many errors for an exception in reify.
- When an assumption made in a test or a filter is flaky, tests will now raise Flaky instead of UnsatisfiedAssumption.
1.18.1 - 2015-12-22
Two behind the scenes changes:
- Hypothesis will no longer write generated code to the file system. This will improve performance on some systems (e.g. if you're using PythonAnywhere <https://www.pythonanywhere.com/> which is running your code from NFS) and prevent some annoying interactions with auto-restarting systems.
- Hypothesis will cache the creation of some strategies. This can significantly improve performance for code that uses flatmap or composite and thus has to instantiate strategies a lot.
1.18.0 - 2015-12-21
Features:
- Tests and find are now explicitly seeded off the global random module. This means that if you nest one inside the other you will now get a health check error. It also means that you can control global randomization by seeding random.
- There is a new random_module() strategy which seeds the global random module for you and handles things so that you don't get a health check warning if you use it inside your tests.
- floats() now accepts two new arguments: allow_nan and allow_infinity. These default to the old behaviour, but when set to False will do what the names suggest.
Bug fixes:
- Fix a bug where tests that used text() on Python 3.4+ would not actually be deterministic even when explicitly seeded or using the derandomize mode, because generation depended on dictionary iteration order which was affected by hash randomization.
- Fix a bug where with complicated strategies the timing of the initial health check could affect the seeding of the subsequent test, which would also render supposedly deterministic tests non-deterministic in some scenarios.
- In some circumstances flatmap() could get confused by two structurally similar things it could generate and would produce a flaky test where the first time it produced an error but the second time it produced the other value, which was not an error. The same bug was presumably also possible in composite().
- flatmap() and composite() initial generation should now be moderately faster. This will be particularly noticeable when you have many values drawn from the same strategy in a single run, e.g. constructs like lists(s.flatmap(f)). Shrinking performance may have suffered, but this didn't actually produce an interestingly worse result in any of the standard scenarios tested.
1.17.1 - 2015-12-16
A small bug fix release, which fixes the fact that the 'note' function could not be used on tests which used the @example decorator to provide explicit examples.
1.17.0 - 2015-12-15
This is actually the same release as 1.16.1, but 1.16.1 has been pulled because it contains the following additional change that was not intended to be in a patch release (it's perfectly stable, but is a larger change that should have required a minor version bump):
- Hypothesis will now perform a series of "health checks" as part of running your tests. These detect and warn about some common error conditions that people often run into which wouldn't necessarily have caused the test to fail but would cause e.g. degraded performance or confusing results.
1.16.1 - 2015-12-14
Note: This release has been removed.
A small bugfix release that allows bdists for Hypothesis to be built under 2.7 - the compat3.py file which had Python 3 syntax wasn't intended to be loaded under Python 2, but when building a bdist it was. In particular this would break running setup.py test.
1.16.0 - 2015-12-08
There are no public API changes in this release but it includes a behaviour change that I wasn't comfortable putting in a patch release.
- Functions from hypothesis.strategies will no longer raise InvalidArgument on bad arguments. Instead the same errors will be raised when a test using such a strategy is run. This may improve startup time in some cases, but the main reason for it is so that errors in strategies won't cause errors in loading, and it can interact correctly with things like pytest.mark.skipif.
- Errors caused by accidentally invoking the legacy API are now much less confusing, although still throw NotImplementedError.
- hypothesis.extra.django is 1.9 compatible.
- When tests are run with max_shrinks=0 this will now still rerun the test on failure and will no longer print "Trying example:" before each run. Additionally note() will now work correctly when used with max_shrinks=0.
1.15.0 - 2015-11-24
A release with two new features.
- A 'characters' strategy for more flexible generation of text with particular character ranges and types, kindly contributed by Alexander Shorin <https://github.com/kxepal>.
- Add support for preconditions to the rule based stateful testing. Kindly contributed by Christopher Armstrong <https://github.com/radix>
1.14.0 - 2015-11-01
New features:
- Add 'note' function which lets you include additional information in the final test run's output.
- Add 'choices' strategy which gives you a choice function that emulates random.choice.
- Add 'uuid' strategy that generates UUIDs'
- Add 'shared' strategy that lets you create a strategy that just generates a single shared value for each test run
Bugs:
- Using strategies of the form streaming(x.flatmap(f)) with find or in stateful testing would have caused InvalidArgument errors when the resulting values were used (because code that expected to only be called within a test context would be invoked).
1.13.0 - 2015-10-29
This is quite a small release, but deprecates some public API functions and removes some internal API functionality so gets a minor version bump.
- All calls to the 'strategy' function are now deprecated, even ones which pass just a SearchStrategy instance (which is still a no-op).
- Never documented hypothesis.extra entry_points mechanism has now been removed ( it was previously how hypothesis.extra packages were loaded and has been deprecated and unused for some time)
- Some corner cases that could previously have produced an OverflowError when simplifying failing cases using hypothesis.extra.datetimes (or dates or times) have now been fixed.
- Hypothesis load time for first import has been significantly reduced - it used to be around 250ms (on my SSD laptop) and now is around 100-150ms. This almost never matters but was slightly annoying when using it in the console.
- hypothesis.strategies.randoms was previously missing from __all__.
1.12.0 - 2015-10-18
- Significantly improved performance of creating strategies using the functions from the hypothesis.strategies module by deferring the calculation of their repr until it was needed. This is unlikely to have been an performance issue for you unless you were using flatmap, composite or stateful testing, but for some cases it could be quite a significant impact.
- A number of cases where the repr of strategies build from lambdas is improved
- Add dates() and times() strategies to hypothesis.extra.datetimes
- Add new 'profiles' mechanism to the settings system
- Deprecates mutability of Settings, both the Settings.default top level property and individual settings.
- A Settings object may now be directly initialized from a parent Settings.
- @given should now give a better error message if you attempt to use it with a function that uses destructuring arguments (it still won't work, but it will error more clearly),
- A number of spelling corrections in error messages
- pytest <https://pypi.org/project/pytest/> should no longer display the intermediate modules Hypothesis generates when running in verbose mode
- Hypothesis should now correctly handle printing objects with non-ascii reprs on python 3 when running in a locale that cannot handle ascii printing to stdout.
- Add a unique=True argument to lists(). This is equivalent to unique_by=lambda x: x, but offers a more convenient syntax.
1.11.4 - 2015-09-27
- Hide modifications Hypothesis needs to make to sys.path by undoing them after we've imported the relevant modules. This is a workaround for issues cryptography experienced on windows.
- Slightly improved performance of drawing from sampled_from on large lists of alternatives.
- Significantly improved performance of drawing from one_of or strategies using | (note this includes a lot of strategies internally - floats() and integers() both fall into this category). There turned out to be a massive performance regression introduced in 1.10.0 affecting these which probably would have made tests using Hypothesis significantly slower than they should have been.
1.11.3 - 2015-09-23
- Better argument validation for datetimes() strategy - previously setting max_year < datetime.MIN_YEAR or min_year > datetime.MAX_YEAR would not have raised an InvalidArgument error and instead would have behaved confusingly.
- Compatibility with being run on pytest < 2.7 (achieved by disabling the plugin).
1.11.2 - 2015-09-23
Bug fixes:
- Settings(database=my_db) would not be correctly inherited when used as a default setting, so that newly created settings would use the database_file setting and create an SQLite example database.
- Settings.default.database = my_db would previously have raised an error and now works.
- Timeout could sometimes be significantly exceeded if during simplification there were a lot of examples tried that didn't trigger the bug.
- When loading a heavily simplified example using a basic() strategy from the database this could cause Python to trigger a recursion error.
- Remove use of deprecated API in pytest plugin so as to not emit warning
Misc:
- hypothesis-pytest is now part of hypothesis core. This should have no externally visible consequences, but you should update your dependencies to remove hypothesis-pytest and depend on only Hypothesis.
- Better repr for hypothesis.extra.datetimes() strategies.
- Add .close() method to abstract base class for Backend (it was already present in the main implementation).
1.11.1 - 2015-09-16
Bug fixes:
- When running Hypothesis tests in parallel (e.g. using pytest-xdist) there was a race condition caused by code generation.
- Example databases are now cached per thread so as to not use sqlite connections from multiple threads. This should make Hypothesis now entirely thread safe.
- floats() with only min_value or max_value set would have had a very bad distribution.
- Running on 3.5, Hypothesis would have emitted deprecation warnings because of use of inspect.getargspec
1.11.0 - 2015-08-31
- text() with a non-string alphabet would have used the repr() of the alphabet instead of its contexts. This is obviously silly. It now works with any sequence of things convertible to unicode strings.
- @given will now work on methods whose definitions contains no explicit positional arguments, only varargs (issue #118 <https://github.com/HypothesisWorks/hypothesis/issues/118>). This may have some knock on effects because it means that @given no longer changes the argspec of functions other than by adding defaults.
- Introduction of new @composite feature for more natural definition of strategies you'd previously have used flatmap for.
1.10.6 - 2015-08-26
Fix support for fixtures on Django 1.7.
1.10.4 - 2015-08-21
Tiny bug fix release:
- If the database_file setting is set to None, this would have resulted in an error when running tests. Now it does the same as setting database to None.
1.10.3 - 2015-08-19
Another small bug fix release.
- lists(elements, unique_by=some_function, min_size=n) would have raised a ValidationError if n > Settings.default.average_list_length because it would have wanted to use an average list length shorter than the minimum size of the list, which is impossible. Now it instead defaults to twice the minimum size in these circumstances.
- basic() strategy would have only ever produced at most ten distinct values per run of the test (which is bad if you e.g. have it inside a list). This was obviously silly. It will now produce a much better distribution of data, both duplicated and non duplicated.
1.10.2 - 2015-08-19
This is a small bug fix release:
- star imports from hypothesis should now work correctly.
- example quality for examples using flatmap will be better, as the way it had previously been implemented was causing problems where Hypothesis was erroneously labelling some examples as being duplicates.
1.10.0 - 2015-08-04
This is just a bugfix and performance release, but it changes some semi-public APIs, hence the minor version bump.
- Significant performance improvements for strategies which are one_of() many branches. In particular this included recursive() strategies. This should take the case where you use one recursive() strategy as the base strategy of another from unusably slow (tens of seconds per generated example) to reasonably fast.
- Better handling of just() and sampled_from() for values which have an incorrect __repr__ implementation that returns non-ASCII unicode on Python 2.
- Better performance for flatmap from changing the internal morpher API to be significantly less general purpose.
- Introduce a new semi-public BuildContext/cleanup API. This allows strategies to register cleanup activities that should run once the example is complete. Note that this will interact somewhat weirdly with find.
- Better simplification behaviour for streaming strategies.
- Don't error on lambdas which use destructuring arguments in Python 2.
- Add some better reprs for a few strategies that were missing good ones.
- The Random instances provided by randoms() are now copyable.
- Slightly more debugging information about simplify when using a debug verbosity level.
- Support using given for functions with varargs, but not passing arguments to it as positional.
1.9.0 - 2015-07-27
Codename: The great bundling.
This release contains two fairly major changes.
The first is the deprecation of the hypothesis-extra mechanism. From now on all the packages that were previously bundled under it other than hypothesis-pytest (which is a different beast and will remain separate). The functionality remains unchanged and you can still import them from exactly the same location, they just are no longer separate packages.
The second is that this introduces a new way of building strategies which lets you build up strategies recursively from other strategies.
It also contains the minor change that calling .example() on a strategy object will give you examples that are more representative of the actual data you'll get. There used to be some logic in there to make the examples artificially simple but this proved to be a bad idea.
1.8.5 - 2015-07-24
This contains no functionality changes but fixes a mistake made with building the previous package that would have broken installation on Windows.
1.8.4 - 2015-07-20
Bugs fixed:
- When a call to floats() had endpoints which were not floats but merely convertible to one (e.g. integers), these would be included in the generated data which would cause it to generate non-floats.
- Splitting lambdas used in the definition of flatmap, map or filter over multiple lines would break the repr, which would in turn break their usage.
1.8.3 - 2015-07-20
"Falsifying example" would not have been printed when the failure came from an explicit example.
1.8.2 - 2015-07-18
Another small bugfix release:
- When using ForkingTestCase you would usually not get the falsifying example printed if the process exited abnormally (e.g. due to os._exit).
- Improvements to the distribution of characters when using text() with a default alphabet. In particular produces a better distribution of ascii and whitespace in the alphabet.
1.8.1 - 2015-07-17
This is a small release that contains a workaround for people who have bad reprs returning non ascii text on Python 2.7. This is not a bug fix for Hypothesis per se because that's not a thing that is actually supposed to work, but Hypothesis leans more heavily on repr than is typical so it's worth having a workaround for.
1.8.0 - 2015-07-16
New features:
- Much more sensible reprs for strategies, especially ones that come from hypothesis.strategies. These should now have as reprs python code that would produce the same strategy.
- lists() accepts a unique_by argument which forces the generated lists to be only contain elements unique according to some function key (which must return a hashable value).
- Better error messages from flaky tests to help you debug things.
Mostly invisible implementation details that may result in finding new bugs in your code:
- Sets and dictionary generation should now produce a better range of results.
- floats with bounds now focus more on 'critical values', trying to produce values at edge cases.
- flatmap should now have better simplification for complicated cases, as well as generally being (I hope) more reliable.
Bug fixes:
- You could not previously use assume() if you were using the forking executor.
1.7.2 - 2015-07-10
This is purely a bug fix release:
- When using floats() with stale data in the database you could sometimes get values in your tests that did not respect min_value or max_value.
- When getting a Flaky error from an unreliable test it would have incorrectly displayed the example that caused it.
- 2.6 dependency on backports was incorrectly specified. This would only have caused you problems if you were building a universal wheel from Hypothesis, which is not how Hypothesis ships, so unless you're explicitly building wheels for your dependencies and support Python 2.6 plus a later version of Python this probably would never have affected you.
- If you use flatmap in a way that the strategy on the right hand side depends sensitively on the left hand side you may have occasionally seen Flaky errors caused by producing unreliable examples when minimizing a bug. This use case may still be somewhat fraught to be honest. This code is due a major rearchitecture for 1.8, but in the meantime this release fixes the only source of this error that I'm aware of.
1.7.1 - 2015-06-29
Codename: There is no 1.7.0.
A slight technical hitch with a premature upload means there's was a yanked 1.7.0 release. Oops.
The major feature of this release is Python 2.6 support. Thanks to Jeff Meadows for doing most of the work there.
Other minor features
- strategies now has a permutations() function which returns a strategy yielding permutations of values from a given collection.
- if you have a flaky test it will print the exception that it last saw before failing with Flaky, even if you do not have verbose reporting on.
- Slightly experimental git merge script available as "python -m hypothesis.tools.mergedbs". Instructions on how to use it in the docstring of that file.
Bug fixes:
- Better performance from use of filter. In particular tests which involve large numbers of heavily filtered strategies should perform a lot better.
- floats() with a negative min_value would not have worked correctly (worryingly, it would have just silently failed to run any examples). This is now fixed.
- tests using sampled_from would error if the number of sampled elements was smaller than min_satisfying_examples.
1.6.2 - 2015-06-08
This is just a few small bug fixes:
- Size bounds were not validated for values for a binary() strategy when reading examples from the database.
- sampled_from is now in __all__ in hypothesis.strategies
- floats no longer consider negative integers to be simpler than positive non-integers
- Small floating point intervals now correctly count members, so if you have a floating point interval so narrow there are only a handful of values in it, this will no longer cause an error when Hypothesis runs out of values.
1.6.1 - 2015-05-21
This is a small patch release that fixes a bug where 1.6.0 broke the use of flatmap with the deprecated API and assumed the passed in function returned a SearchStrategy instance rather than converting it to a strategy.
1.6.0 - 2015-05-21
This is a smallish release designed to fix a number of bugs and smooth out some weird behaviours.
- Fix a critical bug in flatmap where it would reuse old strategies. If all your flatmap code was pure you're fine. If it's not, I'm surprised it's working at all. In particular if you want to use flatmap with django models, you desperately need to upgrade to this version.
- flatmap simplification performance should now be better in some cases where it previously had to redo work.
- Fix for a bug where invalid unicode data with surrogates could be generated during simplification (it was already filtered out during actual generation).
- The Hypothesis database is now keyed off the name of the test instead of the type of data. This makes much more sense now with the new strategies API and is generally more robust. This means you will lose old examples on upgrade.
- The database will now not delete values which fail to deserialize correctly, just skip them. This is to handle cases where multiple incompatible strategies share the same key.
- find now also saves and loads values from the database, keyed off a hash of the function you're finding from.
- Stateful tests now serialize and load values from the database. They should have before, really. This was a bug.
- Passing a different verbosity level into a test would not have worked entirely correctly, leaving off some messages. This is now fixed.
- Fix a bug where derandomized tests with unicode characters in the function body would error on Python 2.7.
1.5.0 - 2015-05-14
Codename: Strategic withdrawal.
The purpose of this release is a radical simplification of the API for building strategies. Instead of the old approach of @strategy.extend and things that get converted to strategies, you just build strategies directly.
The old method of defining strategies will still work until Hypothesis 2.0, because it's a major breaking change, but will now emit deprecation warnings.
The new API is also a lot more powerful as the functions for defining strategies give you a lot of dials to turn. See the updated data section <> for details.
Other changes:
- Mixing keyword and positional arguments in a call to @given is deprecated as well.
- There is a new setting called 'strict'. When set to True, Hypothesis will raise warnings instead of merely printing them. Turning it on by default is inadvisable because it means that Hypothesis minor releases can break your code, but it may be useful for making sure you catch all uses of deprecated APIs.
- max_examples in settings is now interpreted as meaning the maximum number of unique (ish) examples satisfying assumptions. A new setting max_iterations which defaults to a larger value has the old interpretation.
- Example generation should be significantly faster due to a new faster parameter selection algorithm. This will mostly show up for simple data types - for complex ones the parameter selection is almost certainly dominated.
- Simplification has some new heuristics that will tend to cut down on cases where it could previously take a very long time.
- timeout would previously not have been respected in cases where there were a lot of duplicate examples. You probably wouldn't have previously noticed this because max_examples counted duplicates, so this was very hard to hit in a way that mattered.
- A number of internal simplifications to the SearchStrategy API.
- You can now access the current Hypothesis version as hypothesis.__version__.
- A top level function is provided for running the stateful tests without the TestCase infrastructure.
1.4.0 - 2015-05-04
Codename: What a state.
The big feature of this release is the new and slightly experimental stateful testing API. You can read more about that in the appropriate section <#stateful>.
Two minor features the were driven out in the course of developing this:
- You can now set settings.max_shrinks to limit the number of times Hypothesis will try to shrink arguments to your test. If this is set to <= 0 then Hypothesis will not rerun your test and will just raise the failure directly. Note that due to technical limitations if max_shrinks is <= 0 then Hypothesis will print every example it calls your test with rather than just the failing one. Note also that I don't consider settings max_shrinks to zero a sensible way to run your tests and it should really be considered a debug feature.
- There is a new debug level of verbosity which is even more verbose than verbose. You probably don't want this.
Breakage of semi-public SearchStrategy API:
- It is now a required invariant of SearchStrategy that if u simplifies to v then it is not the case that strictly_simpler(u, v). i.e. simplifying should not increase the complexity even though it is not required to decrease it. Enforcing this invariant lead to finding some bugs where simplifying of integers, floats and sets was suboptimal.
- Integers in basic data are now required to fit into 64 bits. As a result python integer types are now serialized as strings, and some types have stopped using quite so needlessly large random seeds.
Hypothesis Stateful testing was then turned upon Hypothesis itself, which lead to an amazing number of minor bugs being found in Hypothesis itself.
Bugs fixed (most but not all from the result of stateful testing) include:
- Serialization of streaming examples was flaky in a way that you would probably never notice: If you generate a template, simplify it, serialize it, deserialize it, serialize it again and then deserialize it you would get the original stream instead of the simplified one.
- If you reduced max_examples below the number of examples already saved in the database, you would have got a ValueError. Additionally, if you had more than max_examples in the database all of them would have been considered.
- @given will no longer count duplicate examples (which it never called your function with) towards max_examples. This may result in your tests running slower, but that's probably just because they're trying more examples.
- General improvements to example search which should result in better performance and higher quality examples. In particular parameters which have a history of producing useless results will be more aggressively culled. This is useful both because it decreases the chance of useless examples and also because it's much faster to not check parameters which we were unlikely to ever pick!
- integers_from and lists of types with only one value (e.g. [None]) would previously have had a very high duplication rate so you were probably only getting a handful of examples. They now have a much lower duplication rate, as well as the improvements to search making this less of a problem in the first place.
- You would sometimes see simplification taking significantly longer than your defined timeout. This would happen because timeout was only being checked after each successful simplification, so if Hypothesis was spending a lot of time unsuccessfully simplifying things it wouldn't stop in time. The timeout is now applied for unsuccessful simplifications too.
- In Python 2.7, integers_from strategies would have failed during simplification with an OverflowError if their starting point was at or near to the maximum size of a 64-bit integer.
- flatmap and map would have failed if called with a function without a __name__ attribute.
- If max_examples was less than min_satisfying_examples this would always error. Now min_satisfying_examples is capped to max_examples. Note that if you have assumptions to satisfy here this will still cause an error.
Some minor quality improvements:
- Lists of streams, flatmapped strategies and basic strategies should now now have slightly better simplification.
1.3.0 - 2015-05-22
New features:
- New verbosity level API for printing intermediate results and exceptions.
- New specifier for strings generated from a specified alphabet.
- Better error messages for tests that are failing because of a lack of enough examples.
Bug fixes:
- Fix error where use of ForkingTestCase would sometimes result in too many open files.
- Fix error where saving a failing example that used flatmap could error.
- Implement simplification for sampled_from, which apparently never supported it previously. Oops.
General improvements:
- Better range of examples when using one_of or sampled_from.
- Fix some pathological performance issues when simplifying lists of complex values.
- Fix some pathological performance issues when simplifying examples that require unicode strings with high codepoints.
- Random will now simplify to more readable examples.
1.2.1 - 2015-04-16
A small patch release for a bug in the new executors feature. Tests which require doing something to their result in order to fail would have instead reported as flaky.
1.2.0 - 2015-04-15
Codename: Finders keepers.
A bunch of new features and improvements.
- Provide a mechanism for customizing how your tests are executed.
- Provide a test runner that forks before running each example. This allows better support for testing native code which might trigger a segfault or a C level assertion failure.
- Support for using Hypothesis to find examples directly rather than as just as a test runner.
- New streaming type which lets you generate infinite lazily loaded streams of data - perfect for if you need a number of examples but don't know how many.
- Better support for large integer ranges. You can now use integers_in_range with ranges of basically any size. Previously large ranges would have eaten up all your memory and taken forever.
- Integers produce a wider range of data than before - previously they would only rarely produce integers which didn't fit into a machine word. Now it's much more common. This percolates to other numeric types which build on integers.
- Better validation of arguments to @given. Some situations that would previously have caused silently wrong behaviour will now raise an error.
- Include +/- sys.float_info.max in the set of floating point edge cases that Hypothesis specifically tries.
- Fix some bugs in floating point ranges which happen when given +/- sys.float_info.max as one of the endpoints... (really any two floats that are sufficiently far apart so that x, y are finite but y - x is infinite). This would have resulted in generating infinite values instead of ones inside the range.
1.1.1 - 2015-04-07
Codename: Nothing to see here
This is just a patch release put out because it fixed some internal bugs that would block the Django integration release but did not actually affect anything anyone could previously have been using. It also contained a minor quality fix for floats that I'd happened to have finished in time.
- Fix some internal bugs with object lifecycle management that were impossible to hit with the previously released versions but broke hypothesis-django.
- Bias floating point numbers somewhat less aggressively towards very small numbers
1.1.0 - 2015-04-06
Codename: No-one mention the M word.
- Unicode strings are more strongly biased towards ascii characters. Previously they would generate all over the space. This is mostly so that people who try to shape their unicode strings with assume() have less of a bad time.
- A number of fixes to data deserialization code that could theoretically have caused mysterious bugs when using an old version of a Hypothesis example database with a newer version. To the best of my knowledge a change that could have triggered this bug has never actually been seen in the wild. Certainly no-one ever reported a bug of this nature.
- Out of the box support for Decimal and Fraction.
- new dictionary specifier for dictionaries with variable keys.
- Significantly faster and higher quality simplification, especially for collections of data.
- New filter() and flatmap() methods on Strategy for better ways of building strategies out of other strategies.
- New BasicStrategy class which allows you to define your own strategies from scratch without needing an existing matching strategy or being exposed to the full horror or non-public nature of the SearchStrategy interface.
1.0.0 - 2015-03-27
Codename: Blast-off!
There are no code changes in this release. This is precisely the 0.9.2 release with some updated documentation.
0.9.2 - 2015-03-26
Codename: T-1 days.
- floats_in_range would not actually have produced floats_in_range unless that range happened to be (0, 1). Fix this.
0.9.1 - 2015-03-25
Codename: T-2 days.
- Fix a bug where if you defined a strategy using map on a lambda then the results would not be saved in the database.
- Significant performance improvements when simplifying examples using lists, strings or bounded integer ranges.
0.9.0 - 2015-03-23
Codename: The final countdown
This release could also be called 1.0-RC1.
It contains a teeny tiny bugfix, but the real point of this release is to declare feature freeze. There will be zero functionality changes between 0.9.0 and 1.0 unless something goes really really wrong. No new features will be added, no breaking API changes will occur, etc. This is the final shakedown before I declare Hypothesis stable and ready to use and throw a party to celebrate.
Bug bounty for any bugs found between now and 1.0: I will buy you a drink (alcoholic, caffeinated, or otherwise) and shake your hand should we ever find ourselves in the same city at the same time.
The one tiny bugfix:
- Under pypy, databases would fail to close correctly when garbage collected, leading to a memory leak and a confusing error message if you were repeatedly creating databases and not closing them. It is very unlikely you were doing this and the chances of you ever having noticed this bug are very low.
0.7.2 - 2015-03-22
Codename: Hygienic macros or bust
- You can now name an argument to @given 'f' and it won't break (issue #38 <https://github.com/HypothesisWorks/hypothesis/issues/38>)
- strategy_test_suite is now named strategy_test_suite as the documentation claims and not in fact strategy_test_suitee
- Settings objects can now be used as a context manager to temporarily override the default values inside their context.
0.7.1 - 2015-03-21
Codename: Point releases go faster
- Better string generation by parametrizing by a limited alphabet
- Faster string simplification - previously if simplifying a string with high range unicode characters it would try every unicode character smaller than that. This was pretty pointless. Now it stops after it's a short range (it can still reach smaller ones through recursive calls because of other simplifying operations).
- Faster list simplification by first trying a binary chop down the middle
- Simultaneous simplification of identical elements in a list. So if a bug only triggers when you have duplicates but you drew e.g. [-17, -17], this will now simplify to [0, 0].
0.7.0, - 2015-03-20
Codename: Starting to look suspiciously real
This is probably the last minor release prior to 1.0. It consists of stability improvements, a few usability things designed to make Hypothesis easier to try out, and filing off some final rough edges from the API.
- Significant speed and memory usage improvements
- Add an example() method to strategy objects to give an example of the sort of data that the strategy generates.
- Remove .descriptor attribute of strategies
- Rename descriptor_test_suite to strategy_test_suite
- Rename the few remaining uses of descriptor to specifier (descriptor already has a defined meaning in Python)
0.6.0 - 2015-03-13
Codename: I'm sorry, were you using that API?
This is primarily a "simplify all the weird bits of the API" release. As a result there are a lot of breaking changes. If you just use @given with core types then you're probably fine.
In particular:
- Stateful testing has been removed from the API
- The way the database is used has been rendered less useful (sorry). The feature for reassembling values saved from other tests doesn't currently work. This will probably be brought back in post 1.0.
- SpecificationMapper is no longer a thing. Instead there is an ExtMethod called strategy which you extend to specify how to convert other types to strategies.
- Settings are now extensible so you can add your own for configuring a strategy
- MappedSearchStrategy no longer needs an unpack method
- Basically all the SearchStrategy internals have changed massively. If you implemented SearchStrategy directly rather than using MappedSearchStrategy talk to me about fixing it.
- Change to the way extra packages work. You now specify the package. This must have a load() method. Additionally any modules in the package will be loaded in under hypothesis.extra
Bug fixes:
- Fix for a bug where calling falsify on a lambda with a non-ascii character in its body would error.
Hypothesis Extra:
- hypothesis-fakefactory: An extension for using faker data in hypothesis. Depends
on fake-factory.
0.5.0 - 2015-02-10
Codename: Read all about it.
Core hypothesis:
- Add support back in for pypy and python 3.2
- @given functions can now be invoked with some arguments explicitly provided. If all arguments that hypothesis would have provided are passed in then no falsification is run.
- Related to the above, this means that you can now use pytest fixtures and mark.parametrize with Hypothesis without either interfering with the other.
- Breaking change: @given no longer works for functions with varargs (varkwargs are fine). This might be added back in at a later date.
- Windows is now fully supported. A limited version (just the tests with none of the extras) of the test suite is run on windows with each commit so it is now a first class citizen of the Hypothesis world.
- Fix a bug for fuzzy equality of equal complex numbers with different reprs (this can happen when one coordinate is zero). This shouldn't affect users - that feature isn't used anywhere public facing.
- Fix generation of floats on windows and 32-bit builds of python. I was using some struct.pack logic that only worked on certain word sizes.
- When a test times out and hasn't produced enough examples this now raises a Timeout subclass of Unfalsifiable.
- Small search spaces are better supported. Previously something like a @given(bool, bool) would have failed because it couldn't find enough examples. Hypothesis is now aware of the fact that these are small search spaces and will not error in this case.
- Improvements to parameter search in the case of hard to satisfy assume. Hypothesis will now spend less time exploring parameters that are unlikely to provide anything useful.
- Increase chance of generating "nasty" floats
- Fix a bug that would have caused unicode warnings if you had a sampled_from that was mixing unicode and byte strings.
- Added a standard test suite that you can use to validate a custom strategy you've defined is working correctly.
Hypothesis extra:
First off, introducing Hypothesis extra packages!
These are packages that are separated out from core Hypothesis because they have one or more dependencies. Every hypothesis-extra package is pinned to a specific point release of Hypothesis and will have some version requirements on its dependency. They use entry_points so you will usually not need to explicitly import them, just have them installed on the path.
This release introduces two of them:
hypothesis-datetime:
Does what it says on the tin: Generates datetimes for Hypothesis. Just install the package and datetime support will start working.
Depends on pytz for timezone support
hypothesis-pytest:
A very rudimentary pytest plugin. All it does right now is hook the display of falsifying examples into pytest reporting.
Depends on pytest.
0.4.3 - 2015-02-05
Codename: TIL narrow Python builds are a thing
This just fixes the one bug.
- Apparently there is such a thing as a "narrow python build" and OS X ships with these by default for python 2.7. These are builds where you only have two bytes worth of unicode. As a result, generating unicode was completely broken on OS X. Fix this by only generating unicode codepoints in the range supported by the system.
0.4.2 - 2015-02-04
Codename: O(dear)
This is purely a bugfix release:
- Provide sensible external hashing for all core types. This will significantly improve performance of tracking seen examples which happens in literally every falsification run. For Hypothesis fixing this cut 40% off the runtime of the test suite. The behaviour is quadratic in the number of examples so if you're running the default configuration this will be less extreme (Hypothesis's test suite runs at a higher number of examples than default), but you should still see a significant improvement.
- Fix a bug in formatting of complex numbers where the string could get incorrectly truncated.
0.4.1 - 2015-02-03
Codename: Cruel and unusual edge cases
This release is mostly about better test case generation.
Enhancements:
- Has a cool release name
- text_type (str in python 3, unicode in python 2) example generation now actually produces interesting unicode instead of boring ascii strings.
- floating point numbers are generated over a much wider range, with particular attention paid to generating nasty numbers - nan, infinity, large and small values, etc.
- examples can be generated using pieces of examples previously saved in the database. This allows interesting behaviour that has previously been discovered to be propagated to other examples.
- improved parameter exploration algorithm which should allow it to more reliably hit interesting edge cases.
- Timeout can now be disabled entirely by setting it to any value <= 0.
Bug fixes:
- The descriptor on a OneOfStrategy could be wrong if you had descriptors which were equal but should not be coalesced. e.g. a strategy for one_of((frozenset({int}), {int})) would have reported its descriptor as {int}. This is unlikely to have caused you any problems
- If you had strategies that could produce NaN (which float previously couldn't but e.g. a Just(float('nan')) could) then this would have sent hypothesis into an infinite loop that would have only been terminated when it hit the timeout.
- Given elements that can take a long time to minimize, minimization of floats or tuples could be quadratic or worse in the that value. You should now see much better performance for simplification, albeit at some cost in quality.
Other:
- A lot of internals have been rewritten. This shouldn't affect you at all, but it opens the way for certain of hypothesis's oddities to be a lot more extensible by users. Whether this is a good thing may be up for debate...
0.4.0 - 2015-01-21
FLAGSHIP FEATURE: Hypothesis now persists examples for later use. It stores data in a local SQLite database and will reuse it for all tests of the same type.
LICENSING CHANGE: Hypothesis is now released under the Mozilla Public License 2.0. This applies to all versions from 0.4.0 onwards until further notice. The previous license remains applicable to all code prior to 0.4.0.
Enhancements:
- Printing of failing examples. I was finding that the pytest runner was not doing a good job of displaying these, and that Hypothesis itself could do much better.
- Drop dependency on six for cross-version compatibility. It was easy enough to write the shim for the small set of features that we care about and this lets us avoid a moderately complex dependency.
- Some improvements to statistical distribution of selecting from small (<= 3 elements)
- Improvements to parameter selection for finding examples.
Bugs fixed:
- could_have_produced for lists, dicts and other collections would not have examined the elements and thus when using a union of different types of list this could result in Hypothesis getting confused and passing a value to the wrong strategy. This could potentially result in exceptions being thrown from within simplification.
- sampled_from would not work correctly on a single element list.
- Hypothesis could get very confused by values which are equal despite having different types being used in descriptors. Hypothesis now has its own more specific version of equality it uses for descriptors and tracking. It is always more fine grained than Python equality: Things considered != are not considered equal by hypothesis, but some things that are considered == are distinguished. If your test suite uses both frozenset and set tests this bug is probably affecting you.
0.3.2 - 2015-01-16
- Fix a bug where if you specified floats_in_range with integer arguments Hypothesis would error in example simplification.
- Improve the statistical distribution of the floats you get for the floats_in_range strategy. I'm not sure whether this will affect users in practice but it took my tests for various conditions from flaky to rock solid so it at the very least improves discovery of the artificial cases I'm looking for.
- Improved repr() for strategies and RandomWithSeed instances.
- Add detection for flaky test cases where hypothesis managed to find an example which breaks it but on the final invocation of the test it does not raise an error. This will typically happen with too much recursion errors but could conceivably happen in other circumstances too.
- Provide a "derandomized" mode. This allows you to run hypothesis with zero real randomization, making your build nice and deterministic. The tests run with a seed calculated from the function they're testing so you should still get a good distribution of test cases.
- Add a mechanism for more conveniently defining tests which just sample from some collection.
- Fix for a really subtle bug deep in the internals of the strategy table. In some circumstances if you were to define instance strategies for both a parent class and one or more of its subclasses you would under some circumstances get the strategy for the wrong superclass of an instance. It is very unlikely anyone has ever encountered this in the wild, but it is conceivably possible given that a mix of namedtuple and tuple are used fairly extensively inside hypothesis which do exhibit this pattern of strategy.
0.3.1 - 2015-01-13
- Support for generation of frozenset and Random values
- Correct handling of the case where a called function mutates it argument. This involved introducing a notion of a strategies knowing how to copy their argument. The default method should be entirely acceptable and the worst case is that it will continue to have the old behaviour if you don't mark your strategy as mutable, so this shouldn't break anything.
- Fix for a bug where some strategies did not correctly implement could_have_produced. It is very unlikely that any of these would have been seen in the wild, and the consequences if they had been would have been minor.
- Re-export the @given decorator from the main hypothesis namespace. It's still available at the old location too.
- Minor performance optimisation for simplifying long lists.
0.3.0 - 2015-01-12
- Complete redesign of the data generation system. Extreme breaking change for anyone who was previously writing their own SearchStrategy implementations. These will not work any more and you'll need to modify them.
- New settings system allowing more global and modular control of Verifier behaviour.
- Decouple SearchStrategy from the StrategyTable. This leads to much more composable code which is a lot easier to understand.
- A significant amount of internal API renaming and moving. This may also break your code.
- Expanded available descriptors, allowing for generating integers or floats in a specific range.
- Significantly more robust. A very large number of small bug fixes, none of which anyone is likely to have ever noticed.
- Deprecation of support for pypy and python 3 prior to 3.3. 3.3 and 3.4. Supported versions are 2.7.x, 3.3.x, 3.4.x. I expect all of these to remain officially supported for a very long time. I would not be surprised to add pypy support back in later but I'm not going to do so until I know someone cares about it. In the meantime it will probably still work.
0.2.2 - 2015-01-08
- Fix an embarrassing complete failure of the installer caused by my being bad at version control
0.2.1 - 2015-01-07
- Fix a bug in the new stateful testing feature where you could make __init__ a @requires method. Simplification would not always work if the prune method was able to successfully shrink the test.
0.2.0 - 2015-01-07
- It's aliiive.
- Improve python 3 support using six.
- Distinguish between byte and unicode types.
- Fix issues where FloatStrategy could raise.
- Allow stateful testing to request constructor args.
- Fix for issue where test annotations would timeout based on when the module was loaded instead of when the test started
0.1.4 - 2013-12-14
- Make verification runs time bounded with a configurable timeout
0.1.3 - 2013-05-03
- Bugfix: Stateful testing behaved incorrectly with subclassing.
- Complex number support
- support for recursive strategies
- different error for hypotheses with unsatisfiable assumptions
0.1.2 - 2013-03-24
- Bugfix: Stateful testing was not minimizing correctly and could throw exceptions.
- Better support for recursive strategies.
- Support for named tuples.
- Much faster integer generation.
0.1.1 - 2013-03-24
- Python 3.x support via 2to3.
- Use new style classes (oops).
0.1.0 - 2013-03-23
- Introduce stateful testing.
- Massive rewrite of internals to add flags and strategies.
0.0.5 - 2013-03-13
- No changes except trying to fix packaging
0.0.4 - 2013-03-13
- No changes except that I checked in a failing test case for 0.0.3 so had to replace the release. Doh
0.0.3 - 2013-03-13
- Improved a few internals.
- Opened up creating generators from instances as a general API.
- Test integration.
0.0.2 - 2013-03-12
- Starting to tighten up on the internals.
- Change API to allow more flexibility in configuration.
- More testing.
0.0.1 - 2013-03-10
- Initial release.
- Basic working prototype. Demonstrates idea, probably shouldn't be used.
Hypothesis development
Hypothesis development is managed by David R. MacIver <https://www.drmaciver.com> and Zac Hatfield-Dodds <https://zhd.dev>, respectively the first author and lead maintainer.
However, these roles don't include unpaid feature development on Hypothesis. Our roles as leaders of the project are:
- Helping other people do feature development on Hypothesis
- Fixing bugs and other code health issues
- Improving documentation
- General release management work
- Planning the general roadmap of the project
- Doing sponsored development on tasks that are too large or in depth for other people to take on
So all new features must either be sponsored or implemented by someone else. That being said, the maintenance team takes an active role in shepherding pull requests and helping people write a new feature (see CONTRIBUTING.rst <https://github.com/HypothesisWorks/hypothesis/blob/master/CONTRIBUTING.rst> for details and these examples of how the process goes <https://github.com/HypothesisWorks/hypothesis/pulls?q=is%3Apr+is%3Amerged+mentored>). This isn't "patches welcome", it's "we will help you write a patch".
Release policy
Hypothesis releases follow semantic versioning <https://semver.org/>.
We maintain backwards-compatibility wherever possible, and use deprecation warnings to mark features that have been superseded by a newer alternative. If you want to detect this, you can upgrade warnings to errors using the warnings module.
We use a rapid release cycle to ensure users get features and bugfixes as soon as possible. Every change to the Hypothesis source is automatically built and published on PyPI as soon as it's merged into master. We wrote about this process here <https://hypothesis.works/articles/continuous-releases/>.
Project roadmap
Hypothesis does not have a long-term release plan. We respond to bug reports as they are made; new features are released as and when someone volunteers to write and maintain them.
Compatibility
Hypothesis generally does its level best to be compatible with everything you could need it to be compatible with. This document outlines our compatibility status and guarantees.
Hypothesis versions
Backwards compatibility is better than backporting fixes, so we use semantic versioning <#release-policy> and only support the most recent version of Hypothesis.
Documented APIs will not break except between major version bumps. All APIs mentioned in the Hypothesis documentation are public unless explicitly noted as provisional, in which case they may be changed in minor releases. Undocumented attributes, modules, and behaviour may include breaking changes in patch releases.
Deprecations
Deprecated features will emit HypothesisDeprecationWarning <#hypothesis.errors.HypothesisDeprecationWarning> for at least six months, and then be removed in the following major release.
Note however that not all warnings are subject to this grace period; sometimes we strengthen validation by adding a warning, and these may become errors immediately at a major release.
We use custom exception and warning types, so you can see exactly where an error came from, or turn only our warnings into errors.
Python versions
Hypothesis is supported and tested on CPython and PyPy 3.10+, i.e. all Python versions that are still supported <https://devguide.python.org/versions/>. 32-bit builds of CPython also work, though we only test them on Windows.
Hypothesis does not officially support anything except the latest patch release of each supported Python version. We will fix bugs in earlier patch releases if reported, but they're not tested in CI and no guarantees are made.
Operating systems
In theory, Hypothesis should work anywhere that Python does. In practice, it is known to work and regularly tested on macOS, Windows, Linux, and Emscripten <https://peps.python.org/pep-0776/>.
If you experience issues running Hypothesis on other operating systems, we are happy to accept bug reports which either clearly point to the problem or contain reproducing instructions for a Hypothesis maintainer who does not have the ability to run that OS. It's hard to fix something we can't reproduce!
Testing frameworks
In general, Hypothesis goes to quite a lot of effort to return a function from @given <#hypothesis.given> that behaves as closely to a normal test function as possible. This means that most things should work sensibly with most testing frameworks.
Maintainers of testing frameworks may be interested in our support for custom function execution <#custom-function-execution>, which may make some Hypothesis interactions possible to support.
pytest
The main interaction to be aware of between Hypothesis and pytest <https://pypi.org/project/pytest/> is fixtures.
pytest fixtures are automatically passed to @given <#hypothesis.given> tests, as usual. Note that @given <#hypothesis.given> supplies parameters from the right, so tests which use a fixture should either be written with the fixture placed first, or with keyword arguments:
@given(st.integers())
def test_use_fixture(myfixture, n):
pass
@given(n=st.integers())
def test_use_fixture(n, myfixture):
passHowever, function-scoped fixtures run only once for the entire test, not per-input. This can be surprising for fixtures which are expected to set up per-input state. To proactively warn about this, we raise HealthCheck.function_scoped_fixture <#hypothesis.HealthCheck.function_scoped_fixture> (unless suppressed with settings.suppress_health_check <#hypothesis.settings.suppress_health_check>).
Combining @given <#hypothesis.given> and pytest.mark.parametrize is fully supported, again keeping in mind that @given <#hypothesis.given> supplies parameters from the right:
@pytest.mark.parametrize("s", ["a", "b", "c"])
@given(st.integers())
def test_use_parametrize(s, n):
assert isinstance(s, str)
assert isinstance(n, int)unittest
unittest <https://pypi.org/project/unittest/> works out of the box with Hypothesis.
The python:unittest.mock.patch() decorator works with @given <#hypothesis.given>, but we recommend using it as a context manager within the test instead, to ensure that the mock is per-input, and to avoid poor interactions with Pytest fixtures.
unittest.TestCase.subTest() is a no-op under Hypothesis, because it interacts poorly with Hypothesis generating hundreds of inputs at a time.
Django
Integration with Django's testing requires use of the Django <#hypothesis-django> extra. The issue is that Django tests reset the database once per test, rather than once per input.
pytest-django <https://pypi.org/project/pytest-django/> doesn't yet implement Hypothesis compatibility. You can follow issue pytest-django#912 <https://github.com/pytest-dev/pytest-django/issues/912> for updates.
coverage.py
coverage <https://pypi.org/project/coverage/> works out of the box with Hypothesis. Our own test suite has 100% branch coverage.
HypoFuzz
HypoFuzz <https://hypofuzz.com/> is built on top of Hypothesis and has native support for it. See also the hypofuzz alternative backend <#alternative-backends>.
Optional packages
The supported versions of optional packages, for strategies in hypothesis.extra, are listed in the documentation for that extra. Our general goal is to support all versions that are supported upstream.
Thread-Safety Policy
As of version 6.136.9 <changelog.html#v6-136-9>, Hypothesis is thread-safe. Each of the following is fully supported, and tested regularly in CI:
- Running tests in multiple processes
- Running separate tests in multiple threads
- Running the same test in multiple threads
If you find a bug here, please report it. The main risks internally are global state, shared caches, and cached strategies.
Thread usage inside tests
Tests that spawn threads internally are supported by Hypothesis.
However, these as with any Hypothesis test, these tests must have deterministic test outcomes and data generation. For example, if timing changes in the threads change the sequence of dynamic draws from @composite <#hypothesis.strategies.composite> or data() <#hypothesis.strategies.data>, Hypothesis may report the test as flaky. The solution here is to refactor data generation so it does not depend on test timings.
Cross-thread API calls
In theory, Hypothesis supports cross-thread API calls, for instance spawning a thread inside of a test and using that to draw from @composite <#hypothesis.strategies.composite> or data() <#hypothesis.strategies.data>, or to call event() <#hypothesis.event>, target() <#hypothesis.target>, or assume() <#hypothesis.assume>.
However, we have not explicitly audited this behavior, and do not regularly test it in our CI. If you find a bug here, please report it. If our investigation determines that we cannot support cross-thread calls for the feature in question, we will update this page accordingly.
Type hints
We ship type hints with Hypothesis itself. Though we always try to minimize breakage, we may make breaking changes to these between minor releases and do not commit to maintaining a fully stable interface for type hints.
We may also find more precise ways to describe the type of various interfaces, or change their type and runtime behaviour together in a way which is otherwise backwards-compatible.
There are known issues with inferring the type of examples generated by deferred() <#hypothesis.strategies.deferred>, recursive() <#hypothesis.strategies.recursive>, one_of() <#hypothesis.strategies.one_of>, dictionaries() <#hypothesis.strategies.dictionaries>, and fixed_dictionaries() <#hypothesis.strategies.fixed_dictionaries>. We're following proposed updates to Python's typing standards, but unfortunately the long-standing interfaces of these strategies cannot (yet) be statically typechecked.
Projects using Hypothesis
Hypothesis is downloaded over 4 million times each week <https://pypistats.org/packages/hypothesis>, and was used by more than 5% of Python users surveyed by the PSF in 2023 <https://lp.jetbrains.com/python-developers-survey-2023/>!
The following notable open-source projects use Hypothesis to test their code: pytorch <https://github.com/pytorch/pytorch>, jax <https://github.com/jax-ml/jax>, PyPy <https://github.com/pypy/pypy>, numpy <https://github.com/numpy/numpy>, pandas <https://github.com/pandas-dev/pandas>, attrs <https://github.com/python-attrs/attrs>, chardet <https://github.com/chardet/chardet>, bidict <https://github.com/jab/bidict>, xarray <https://github.com/pydata/xarray>, array-api-tests <https://github.com/data-apis/array-api-tests>, pandera <https://github.com/unionai-oss/pandera>, ivy <https://github.com/ivy-llc/ivy>, zenml <https://github.com/zenml-io/zenml>, mercurial <https://www.mercurial-scm.org/>, qutebrowser <https://github.com/qutebrowser/qutebrowser>, dry-python/returns <https://github.com/dry-python/returns>, argon2_cffi <https://github.com/hynek/argon2-cffi>, axelrod <https://github.com/Axelrod-Python/Axelrod>, hyper-h2 <https://github.com/python-hyper/h2>, MDAnalysis <https://github.com/MDAnalysis/mdanalysis>, napari <https://github.com/napari/napari>, natsort <https://github.com/SethMMorton/natsort>, vdirsyncer <https://github.com/pimutils/vdirsyncer>, and pyrsistent <https://github.com/tobgu/pyrsistent>. You can find thousands more projects tested by Hypothesis on GitHub <https://github.com/HypothesisWorks/hypothesis/network/dependents>.
There are also dozens of first-party <> and third-party extensions <> integrating Hypothesis with a wide variety of libraries and data formats.
Research papers about Hypothesis
Looking to read more about Hypothesis and property-based testing? Hypothesis has been the subject of a number of research papers:
- Hypothesis: A new approach to property-based testing <https://doi.org/10.21105/joss.01891> (2019)*
- Test-Case Reduction via Test-Case Generation: Insights from the Hypothesis Reducer <https://doi.org/10.4230/LIPIcs.ECOOP.2020.13> (2020)*
- Deriving semantics-aware fuzzers from web API schemas <https://dl.acm.org/doi/10.1145/3510454.3528637> (2022)*
- Tyche: Making Sense of PBT Effectiveness <https://dl.acm.org/doi/10.1145/3654777.3676407> (2024)*
- An Empirical Evaluation of Property-Based Testing in Python <https://dl.acm.org/doi/10.1145/3764068> (2025)
- Agentic Property-Based Testing: Finding Bugs Across the Python Ecosystem <https://doi.org/10.48550/arXiv.2510.09907> (2025)*
* Author list includes one or more Hypothesis maintainers
Third-party extensions
There are a number of open-source community libraries that extend Hypothesis. This page lists some of them; you can find more by searching PyPI by keyword <https://pypi.org/search/?q=hypothesis> or by framework classifier <https://pypi.org/search/?c=Framework+%3A%3A+Hypothesis>.
If there's something missing which you think should be here, let us know!
Note:
Being listed on this page does not imply that the Hypothesis maintainers endorse a package.
External strategies
Some packages provide strategies directly:
- hypothesis-fspaths <https://pypi.org/project/hypothesis-fspaths/> - strategy to generate filesystem paths.
- hypothesis-geojson <https://pypi.org/project/hypothesis-geojson/> - strategy to generate GeoJson <https://geojson.org/>.
- hypothesis-geometry <https://pypi.org/project/hypothesis-geometry/> - strategies to generate geometric objects.
- hs-dbus-signature <https://pypi.org/project/hs-dbus-signature/> - strategy to generate arbitrary D-Bus signatures <https://www.freedesktop.org/wiki/Software/dbus/>.
- hypothesis-sqlalchemy <https://pypi.org/project/hypothesis-sqlalchemy/> - strategies to generate SQLAlchemy <https://pypi.org/project/SQLAlchemy/> objects.
- hypothesis-ros <https://pypi.org/project/hypothesis-ros/> - strategies to generate messages and parameters for the Robot Operating System <https://www.ros.org/>.
- hypothesis-csv <https://pypi.org/project/hypothesis-csv/> - strategy to generate CSV files.
- hypothesis-networkx <https://pypi.org/project/hypothesis-networkx/> - strategy to generate networkx <https://pypi.org/project/networkx/> graphs.
- hypothesis-bio <https://pypi.org/project/hypothesis-bio/> - strategies for bioinformatics data, such as DNA, codons, FASTA, and FASTQ formats.
- hypothesis-rdkit <https://pypi.org/project/hypothesis-rdkit/> - strategies to generate RDKit molecules and representations such as SMILES and mol blocks
- hypothesmith <https://pypi.org/project/hypothesmith/> - strategy to generate syntatically-valid Python code.
- hypothesis-torch <https://pypi.org/project/hypothesis-torch/> - strategy to generate various Pytorch <https://pytorch.org/> structures (including tensors and modules).
Others provide a function to infer a strategy from some other schema:
- hypothesis-jsonschema <https://pypi.org/project/hypothesis-jsonschema/> - infer strategies from JSON schemas <https://json-schema.org/>.
- lollipop-hypothesis <https://pypi.org/project/lollipop-hypothesis/> - infer strategies from lollipop <https://pypi.org/project/lollipop/> schemas.
- hypothesis-drf <https://pypi.org/project/hypothesis-drf/> - infer strategies from a djangorestframework <https://pypi.org/project/djangorestframework/> serialiser.
- hypothesis-graphql <https://pypi.org/project/hypothesis-graphql/> - infer strategies from GraphQL schemas <https://graphql.org/>.
- hypothesis-mongoengine <https://pypi.org/project/hypothesis-mongoengine/> - infer strategies from a mongoengine <https://pypi.org/project/mongoengine/> model.
- hypothesis-pb <https://pypi.org/project/hypothesis-pb/> - infer strategies from Protocol Buffer <https://protobuf.dev/> schemas.
Or some other custom integration, such as a "hypothesis" entry point:
- deal <https://pypi.org/project/deal/> is a design-by-contract library with built-in Hypothesis support.
- icontract-hypothesis <https://pypi.org/project/icontract-hypothesis/> infers strategies from icontract <https://pypi.org/project/icontract/> code contracts.
- pandera <https://pypi.org/project/pandera/> schemas all have a .strategy() method, which returns a strategy for matching DataFrames.
- Pydantic <https://pypi.org/project/pydantic/> automatically registers constrained types - so builds() <#hypothesis.strategies.builds> and from_type() <#hypothesis.strategies.from_type> "just work" regardless of the underlying implementation.
Other cool things
Tyche <https://marketplace.visualstudio.com/items?itemName=HarrisonGoldstein.tyche> (source <https://github.com/tyche-pbt>) is a VSCode extension which provides live insights into your property-based tests, including the distribution of generated inputs and the resulting code coverage. You can read the research paper here <https://harrisongoldste.in/papers/uist23.pdf>.
schemathesis <https://pypi.org/project/schemathesis/> is a tool for testing web applications built with Open API / Swagger specifications <https://swagger.io/>. It reads the schema and generates test cases which will ensure that the application is compliant with its schema. The application under test could be written in any language, the only thing you need is a valid API schema in a supported format. Includes CLI and convenient pytest <https://pypi.org/project/pytest/> integration. Powered by Hypothesis and hypothesis-jsonschema <https://pypi.org/project/hypothesis-jsonschema/>, inspired by the earlier swagger-conformance <https://pypi.org/project/swagger-conformance/> library.
Trio <https://trio.readthedocs.io/> is an async framework with "an obsessive focus on usability and correctness", so naturally it works with Hypothesis! pytest-trio <https://pypi.org/project/pytest-trio/> includes a custom hook <#custom-function-execution> that allows @given(...) to work with Trio-style async test functions, and hypothesis-trio <https://pypi.org/project/hypothesis-trio/> includes stateful testing extensions to support concurrent programs.
pymtl3 <https://pypi.org/project/pymtl3/> is "an open-source Python-based hardware generation, simulation, and verification framework with multi-level hardware modeling support", which ships with Hypothesis integrations to check that all of those levels are equivalent, from function-level to register-transfer level and even to hardware.
battle-tested <https://pypi.org/project/battle-tested/> is a fuzzing tool that will show you how your code can fail - by trying all kinds of inputs and reporting whatever happens.
pytest-subtesthack <https://pypi.org/project/pytest-subtesthack/> functions as a workaround for issue #377 <https://github.com/HypothesisWorks/hypothesis/issues/377>.
returns <https://pypi.org/project/returns/> uses Hypothesis to verify that Higher Kinded Types correctly implement functor, applicative, monad, and other laws; allowing a declarative approach to be combined with traditional pythonic code.
icontract-hypothesis <https://pypi.org/project/icontract-hypothesis/> includes a ghostwriter <#ghostwriter> for test files and IDE integrations such as icontract-hypothesis-vim <https://github.com/mristin/icontract-hypothesis-vim>, icontract-hypothesis-pycharm <https://github.com/mristin/icontract-hypothesis-pycharm>, and icontract-hypothesis-vscode <https://github.com/mristin/icontract-hypothesis-vscode> - you can run a quick 'smoke test' with only a few keystrokes for any type-annotated function, even if it doesn't have any contracts!
Writing an extension
- Note:
See CONTRIBUTING.rst <https://github.com/HypothesisWorks/hypothesis/blob/master/CONTRIBUTING.rst> for more information.
New strategies can be added to Hypothesis, or published as an external package on PyPI - either is fine for most strategies. If in doubt, ask!
It's generally much easier to get things working outside, because there's more freedom to experiment and fewer requirements in stability and API style. We're happy to review and help with external packages as well as pull requests!
If you're thinking about writing an extension, please name it hypothesis-{something} - a standard prefix makes the community more visible and searching for extensions easier. And make sure you use the Framework :: Hypothesis trove classifier!
On the other hand, being inside gets you access to some deeper implementation features (if you need them) and better long-term guarantees about maintenance. We particularly encourage pull requests for new composable primitives that make implementing other strategies easier, or for widely used types in the standard library. Strategies for other things are also welcome; anything with external dependencies just goes in hypothesis.extra.
Tools such as assertion helpers may also need to check whether the current test is using Hypothesis. For that, see currently_in_test_context() <#hypothesis.currently_in_test_context>.
Hypothesis integration via entry points
If you would like to ship Hypothesis strategies for a custom type - either as part of the upstream library, or as a third-party extension, there's a catch: from_type() <#hypothesis.strategies.from_type> only works after the corresponding call to register_type_strategy() <#hypothesis.strategies.register_type_strategy>, and you'll have the same problem with register_random() <#hypothesis.register_random>. This means that either
- you have to try importing Hypothesis to register the strategy when your library is imported, though that's only useful at test time, or
- the user has to call a 'register the strategies' helper that you provide before running their tests
Entry points <https://setuptools.pypa.io/en/latest/userguide/entry_point.html> are Python's standard way of automating the latter: when you register a "hypothesis" entry point in your pyproject.toml, we'll import and run it automatically when hypothesis is imported. Nothing happens unless Hypothesis is already in use, and it's totally seamless for downstream users!
Let's look at an example. You start by adding a function somewhere in your package that does all the Hypothesis-related setup work:
# mymodule.py
class MyCustomType:
def __init__(self, x: int):
assert x >= 0, f"got {x}, but only positive numbers are allowed"
self.x = x
def _hypothesis_setup_hook():
import hypothesis.strategies as st
st.register_type_strategy(MyCustomType, st.integers(min_value=0))and then declare this as your "hypothesis" entry point:
# pyproject.toml # You can list a module to import by dotted name [project.entry-points.hypothesis] _ = "mymodule.a_submodule" # Or name a specific function, and Hypothesis will call it for you [project.entry-points.hypothesis] _ = "mymodule:_hypothesis_setup_hook"
And that's all it takes!
- HYPOTHESIS_NO_PLUGINS
If set, disables automatic loading of all hypothesis plugins. This is probably only useful for our own self-tests, but documented in case it might help narrow down any particularly weird bugs in complex environments.
Interaction with pytest-cov <https://pypi.org/project/pytest-cov/>
Because pytest does not load plugins from entrypoints in any particular order, using the Hypothesis entrypoint may import your module before pytest-cov <https://pypi.org/project/pytest-cov/> starts. This is a known issue <https://github.com/pytest-dev/pytest/issues/935>, but there are workarounds.
You can use coverage run pytest ... instead of pytest --cov ..., opting out of the pytest plugin entirely. Alternatively, you can ensure that Hypothesis is loaded after coverage measurement is started by disabling the entrypoint, and loading our pytest plugin from your conftest.py instead:
echo "pytest_plugins = ['hypothesis.extra.pytestplugin']\n" > tests/conftest.py pytest -p "no:hypothesispytest" ...
Another alternative, which we in fact use in our CI self-tests because it works well also with parallel tests, is to automatically start coverage early for all new processes if an environment variable is set. This automatic starting is set up by the PyPi package coverage_enable_subprocess <https://pypi.org/project/coverage_enable_subprocess/>.
This means all configuration must be done in .coveragerc, and not on the command line:
[run] parallel = True source = ...
Then, set the relevant environment variable and run normally:
python -m pip install coverage_enable_subprocess export COVERAGE_PROCESS_START=$PATH/.coveragerc pytest [-n auto] ... coverage combine coverage report
Alternative backends for Hypothesis
See also:
See also the Alternative backends interface <#alternative-backends-internals> for details on implementing your own alternative backend.
Hypothesis supports alternative backends, which tells Hypothesis how to generate primitive types. This enables powerful generation techniques which are compatible with all parts of Hypothesis, including the database and shrinking.
Hypothesis includes the following backends:
- hypothesis
The default backend.
- hypothesis-urandom
The same as the default backend, but uses /dev/urandom as its source of randomness, instead of the standard PRNG in python:random.Random. The only reason to use this backend over the default backend="hypothesis" is if you are also using Antithesis <https://antithesis.com/>, in which case this allows Antithesis mutation of /dev/urandom to control the values generated by Hypothesis.
/dev/urandom is not available on Windows, so we emit a warning and fall back to the hypothesis backend there.
- hypofuzz
Generates inputs using coverage-guided fuzzing. See HypoFuzz <https://hypofuzz.com/> for details.
Requires pip install hypofuzz.
- crosshair
Generates inputs using SMT solvers like z3, which is particularly effective at satisfying difficult checks in your code, like if or == statements.
Requires pip install hypothesis[crosshair].
You can change the backend for a test with the settings.backend <#hypothesis.settings.backend> setting. For instance, after pip install hypothesis[crosshair], you can use crosshair <https://pypi.org/project/crosshair-tool/> to generate inputs with SMT via the hypothesis-crosshair <https://pypi.org/project/hypothesis-crosshair/> backend:
from hypothesis import given, settings, strategies as st
@settings(backend="crosshair") # pip install hypothesis[crosshair]
@given(st.integers())
def test_needs_solver(x):
assert x != 123456789Failures found by alternative backends are saved to the database and shrink just like normally generated inputs, and in general interact with every feature of Hypothesis as you would expect.
Packaging guidelines
Downstream packagers often want to package Hypothesis. Here are some guidelines.
The primary guideline is this: If you are not prepared to keep up with the Hypothesis release schedule, don't. You will be doing your users a disservice.
Hypothesis has a very frequent release schedule. We often release new versions multiple times a week.
If you are prepared to keep up with this schedule, you might find the rest of this document useful.
Release tarballs
These are available from the GitHub releases page <https://github.com/HypothesisWorks/hypothesis/releases>. The tarballs on PyPI are intended for installation from a Python tool such as pip <https://pypi.org/project/pip/> and should not be considered complete releases. Requests to include additional files in them will not be granted. Their absence is not a bug.
Dependencies
Python versions
Hypothesis is designed to work with a range of Python versions - we support all versions of CPython with upstream support <https://devguide.python.org/#status-of-python-branches>. We also support the latest versions of PyPy for Python 3.
Other Python libraries
Hypothesis has mandatory dependencies on the following libraries:
- sortedcontainers <https://pypi.org/project/sortedcontainers/>
Hypothesis has optional dependencies on the following libraries:
[project.optional-dependencies] cli = ["click>=7.0", "black>=20.8b0", "rich>=9.0.0"] codemods = ["libcst>=0.3.16"] ghostwriter = ["black>=20.8b0"] pytz = ["pytz>=2014.1"] dateutil = ["python-dateutil>=1.4"] lark = ["lark>=0.10.1"] # probably still works with old `lark-parser` too numpy = ["numpy>=1.21.6"] # oldest with wheels for non-EOL Python (for now) pandas = ["pandas>=1.1"] pytest = ["pytest>=4.6"] dpcontracts = ["dpcontracts>=0.4"] redis = ["redis>=3.0.0"] crosshair = ["hypothesis-crosshair>=0.0.27", "crosshair-tool>=0.0.102"] # zoneinfo is an odd one: every dependency is platform-conditional. zoneinfo = ["tzdata>=2025.3; sys_platform == 'win32' or sys_platform == 'emscripten'"] # We only support Django versions with upstream support - see # https://www.djangoproject.com/download/#supported-versions # We also leave the choice of timezone library to the user, since it # might be zoneinfo or pytz depending on version and configuration. django = ["django>=4.2"] watchdog = ["watchdog>=4.0.0"]
The way this works when installing Hypothesis normally is that these features become available if the relevant library is installed.
Specifically for pytest <https://pypi.org/project/pytest/>, our plugin supports versions of pytest which have been out of upstream support for some time. Hypothesis tests can still be executed by even older versions of pytest - you just won't have the plugin to provide automatic marks, helpful usage warnings, and per-test statistics.
Testing Hypothesis
If you want to test Hypothesis as part of your packaging you will probably not want to use the mechanisms Hypothesis itself uses for running its tests, because it has a lot of logic for installing and testing against different versions of Python.
The tests must be run with fairly recent tooling; check the tree/master/requirements/ <https://github.com/HypothesisWorks/hypothesis/tree/master/requirements/> directory for details.
The organisation of the tests is described in the hypothesis-python/tests/README.rst <https://github.com/HypothesisWorks/hypothesis/blob/master/hypothesis-python/tests/README.rst>.
Examples
Community
The Hypothesis community is full of excellent people who can answer your questions and help you out. Please do join us:
- You can post a question on Stack Overflow using the python-hypothesis <https://stackoverflow.com/questions/tagged/python-hypothesis> tag.
- We also have a mailing list <https://groups.google.com/forum/#!forum/hypothesis-users>. Feel free to use it to ask for help, provide feedback, or discuss anything remotely Hypothesis related at all.
Note that the Hypothesis code of conduct <https://github.com/HypothesisWorks/hypothesis/blob/master/CODE_OF_CONDUCT.rst> applies in all Hypothesis community spaces!
If you would like to cite Hypothesis, please consider our suggested citation <https://github.com/HypothesisWorks/hypothesis/blob/master/CITATION.cff>.
If you like repo badges, we suggest the following badge, which you can add with Markdown or reStructuredText respectively: [image]
[](https://hypothesis.readthedocs.io/)
.. image:: https://img.shields.io/badge/hypothesis-tested-brightgreen.svg :alt: Tested with Hypothesis :target: https://hypothesis.readthedocs.io
Finally, we have a beautiful logo which appears online, and often on stickers: [image: The Hypothesis logo, a dragonfly with rainbow wings] [image]
As well as being beautiful, dragonflies actively hunt down bugs for a living! You can find the images and a usage guide in the brand <https://github.com/HypothesisWorks/hypothesis/blob/master/brand> directory on GitHub, or find us at conferences where we often have stickers and sometimes other swag.
Author
the Hypothesis team
Copyright
2013-2026, the Hypothesis team