Skip to content

Commit

Permalink
Add blacken-docs (#2134)
Browse files Browse the repository at this point in the history
Add `blacken-docs`
  • Loading branch information
Zac-HD committed Oct 15, 2019
2 parents 57ad76b + 139ffba commit c3cce7b
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 91 deletions.
3 changes: 1 addition & 2 deletions hypothesis-python/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ e.g.

.. code-block:: python
@given(st.lists(
st.floats(allow_nan=False, allow_infinity=False), min_size=1))
@given(st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1))
def test_mean(xs):
assert min(xs) <= mean(xs) <= max(xs)
Expand Down
2 changes: 2 additions & 0 deletions hypothesis-python/docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3068,6 +3068,7 @@ 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 != y
Expand Down Expand Up @@ -3107,6 +3108,7 @@ 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 < 100
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/docs/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ For instance:
@given(data())
def test_draw_sequentially(data):
x = data.draw(integers(), label='First number')
y = data.draw(integers(min_value=x), label='Second number')
x = data.draw(integers(), label="First number")
y = data.draw(integers(min_value=x), label="Second number")
assert x < y
will produce the output:
Expand Down
52 changes: 39 additions & 13 deletions hypothesis-python/docs/details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ 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):
pass
Expand Down Expand Up @@ -90,6 +91,7 @@ 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):
pass
Expand All @@ -116,6 +118,7 @@ You can also mark custom events in a test using the ``event`` function:
from hypothesis import given, event, strategies as st
@given(st.integers().filter(lambda x: x % 2 == 0))
def test_even_integers(i):
event("i mod 3 = %d" % (i % 3,))
Expand Down Expand Up @@ -181,6 +184,7 @@ So lets block off this particular example:
from math import isnan
@given(floats())
def test_negation_is_self_inverse_for_non_nan(x):
assume(not isnan(x))
Expand Down Expand Up @@ -221,7 +225,7 @@ Suppose we had the following:
@given(lists(integers()))
def test_sum_is_positive(xs):
assert sum(xs) > 0
assert sum(xs) > 0
Unsurprisingly this fails and gives the falsifying example ``[]``.

Expand Down Expand Up @@ -253,12 +257,16 @@ Here's what happens if we try to run this:
print(xs)
assert sum(xs) > 0
In: test_sum_is_positive()
In: ``test_sum_is_positive()``

.. code:: python
[17, 12, 7, 13, 11, 3, 6, 9, 8, 11, 47, 27, 1, 31, 1]
[6, 2, 29, 30, 25, 34, 19, 15, 50, 16, 10, 3, 16]
[25, 17, 9, 19, 15, 2, 2, 4, 22, 10, 10, 27, 3, 1, 14, 17, 13, 8, 16, 9, 2...
[25, 17, 9, 19, 15, 2, 2, 4, 22, 10, 10, 27, 3, 1, 14, 17, 13, 8, 16, 9, 2, ...]
[17, 65, 78, 1, 8, 29, 2, 79, 28, 18, 39]
[13, 26, 8, 3, 4, 76, 6, 14, 20, 27, 21, 32, 14, 42, 9, 24, 33, 9, 5, 15, ...
[13, 26, 8, 3, 4, 76, 6, 14, 20, 27, 21, 32, 14, 42, 9, 24, 33, 9, 5, 15, ...]
[2, 1, 2, 2, 3, 10, 12, 11, 21, 11, 1, 16]
As you can see, Hypothesis doesn't find *many* examples here, but it finds some - enough to
Expand Down Expand Up @@ -314,27 +322,32 @@ For example all of the following are valid uses:
@given(integers(), integers())
def a(x, y):
pass
pass
@given(integers())
def b(x, y):
pass
pass
@given(y=integers())
def c(x, y):
pass
pass
@given(x=integers())
def d(x, y):
pass
pass
@given(x=integers(), y=integers())
def e(x, **kwargs):
pass
pass
@given(x=integers(), y=integers())
def f(x, *args, **kwargs):
pass
pass
class SomeTest(TestCase):
Expand All @@ -350,14 +363,17 @@ The following are not:
def g(x, y):
pass
@given(integers())
def h(x, *args):
pass
@given(integers(), x=integers())
def i(x, y):
pass
@given()
def j(x, y):
pass
Expand Down Expand Up @@ -442,6 +458,7 @@ the following executor runs all its code twice:
from unittest import TestCase
class TestTryReallyHard(TestCase):
@given(integers())
def test_something(self, i):
Expand All @@ -462,6 +479,7 @@ executor is invalid:
from unittest import TestCase
class TestRunTwice(TestCase):
def execute_example(self, f):
return f()()
Expand All @@ -472,6 +490,7 @@ and should be rewritten as:
from unittest import TestCase
class TestRunTwice(TestCase):
def execute_example(self, f):
result = f()
Expand All @@ -492,6 +511,8 @@ transformation before applying :func:`@given <hypothesis.given>`.
@pytest.mark.trio
async def test(x):
...
# Illustrative code, inside the pytest-trio plugin
test.hypothesis.inner_test = lambda x: trio.run(test, x)
Expand Down Expand Up @@ -558,10 +579,14 @@ fill in an argument from its type annotation.
.. code:: python
@given(a=infer)
def test(a: int): pass
def test(a: int):
pass
# is equivalent to
@given(a=integers())
def test(a): pass
def test(a):
pass
~~~~~~~~~~~
Limitations
Expand Down Expand Up @@ -623,7 +648,8 @@ supported use-case, again on a best-effort provisional basis. For example:

.. code:: python
def foo_strategy() -> SearchStrategy[Foo]: ...
def foo_strategy() -> SearchStrategy[Foo]:
...
.. class:: hypothesis.strategies.SearchStrategy

Expand Down
22 changes: 12 additions & 10 deletions hypothesis-python/docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ a strategy for Django models:
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>`_:

.. code-block:: python
.. code-block:: pycon
>>> from hypothesis.extra.django import from_model
>>> from toystore.models import Customer
Expand Down Expand Up @@ -72,7 +72,7 @@ pass a strategy to skip validation at the strategy level:
is implemented, but there will always be some edge cases that require you
to pass an explicit strategy.

.. code-block:: python
.. code-block:: pycon
>>> from hypothesis.strategies import integers
>>> c = from_model(Customer, age=integers(min_value=0, max_value=120)).example()
Expand All @@ -93,7 +93,7 @@ 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:

.. code-block:: python
.. code-block:: pycon
>>> from toystore.models import CustomishField, Customish
>>> from_model(Customish).example()
Expand Down Expand Up @@ -123,8 +123,10 @@ the *flatmap* function as follows:
from hypothesis.strategies import lists, just
def generate_with_shops(company):
return lists(from_model(Shop, company=just(company))).map(lambda _: company)
return lists(from_model(Shop, company=just(company))).map(lambda _: company)
company_with_shops_strategy = from_model(Company).flatmap(generate_with_shops)
Expand Down Expand Up @@ -180,8 +182,8 @@ default example of this is the :class:`~django:django.forms.SplitDateTimeField`.
.. code:: python
class CustomerForm(forms.Form):
name = forms.CharField()
birth_date_time = forms.SplitDateTimeField()
name = forms.CharField()
birth_date_time = forms.SplitDateTimeField()
``from_form`` supports ``MultiValueField`` subclasses directly, however if you
want to define your own strategy be forewarned that Django binds data for a
Expand All @@ -194,14 +196,14 @@ like this:
.. code:: python
{
'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
"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:

.. code:: python
from_form(CustomerForm, birth_date_time_0=just('2018-05-19'))
from_form(CustomerForm, birth_date_time_0=just("2018-05-19"))

0 comments on commit c3cce7b

Please sign in to comment.