Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More strategies for NumPy #472

Merged
merged 12 commits into from
Mar 8, 2017
1 change: 1 addition & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ their individual contributions.
* `Tariq Khokhar <https://www.github.com/tkb>`_ (`[email protected] <mailto:[email protected]>`_)
* `Will Hall <https://www.github.com/wrhall>`_ (`[email protected] <mailto:[email protected]>`_)
* `Will Thompson <https://www.github.com/wjt>`_ (`[email protected] <mailto:[email protected]>`_)
* `Zac Hatfield-Dodds <https://www.github.com/Zac-HD>`_ (`[email protected] <mailto:[email protected]>`_)
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
'python': ('http://docs.python.org/', None),
}

autodoc_mock_imports = ['numpy']


# -- Options for HTML output ----------------------------------------------

Expand Down
53 changes: 3 additions & 50 deletions docs/extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,56 +188,9 @@ It's large enough that it is :doc:`documented elsewhere <django>`.
hypothesis[numpy]
------------------

hypothesis.extra.numpy adds support for testing
`NumPy <http://www.numpy.org/>`_-based implementations with Hypothesis by
providing an ``arrays`` function.
hypothesis.extra.numpy adds support for testing your Numpy code with Hypothesis.

It lives in the ``hypothesis.extra.numpy`` package.
This includes generating arrays, array shapes, and both scalar or compound dtypes.

.. method:: arrays(dtype, shape, elements=None)
Like the Django extra, :doc:`Numpy has it's own page <numpy>`.

Arrays of specified `dtype` and `shape` are generated for example
like this:

.. code-block:: pycon

>>> import numpy as np
>>> arrays(np.int8, (2, 3)).example()
array([[-8, 6, 3],
[-6, 4, 6]], dtype=int8)


However, to obtain more fine grained control over the elements, use
the `elements` keyword (see also :doc:`What you can generate and how <data>`):

.. code-block:: pycon

>>> import numpy as np
>>> from hypothesis.strategies import floats
>>> arrays(np.float, 3, elements=floats(min_value=0, max_value=1)).example()
array([ 0.88974794, 0.77387938, 0.1977879 ])

By combining different strategies, the shape of the array can be modified as well:

.. code-block:: pycon

>>> import numpy as np
>>> from hypothesis.strategies import integers, floats
>>>
>>> def rnd_len_arrays(dtype, min_len=0, max_len=3, elements=None):
... lengths = integers(min_value=min_len, max_value=max_len)
... return lengths.flatmap(lambda n: arrays(dtype, n, elements=elements))
...
>>>
>>> rla = rnd_len_arrays(np.int8)
>>> rla.example()
array([], dtype=int8)
>>> rla.example()
array([-2], dtype=int8)
>>> rla.example()
array([ 7, -6, -2], dtype=int8)

**Note**: To generate large arrays/matrices, or even medium-sized matrices that
have 3 or more dimensions, it's necessary to increase the ``buffer_size``
setting to be greater than its default of ``8192`` (see the
:doc:`Settings documentation <settings>` for details on how to do this).
5 changes: 3 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ finding edge cases in your code you wouldn't have thought to look for. It is
stable, powerful and easy to add to any existing test suite.

It works by letting you write tests that assert that something should be true
for every case, not just the ones you happen to think of.
for every case, not just the ones you happen to think of.

Think of a normal unit test as being something like the following:

Expand Down Expand Up @@ -57,11 +57,12 @@ check out some of the
:hidden:

quickstart
django
details
settings
data
extras
django
numpy
healthchecks
database
stateful
Expand Down
18 changes: 18 additions & 0 deletions docs/numpy.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.. _hypothesis-numpy:

=================================
Scientific Hypothesis (for NumPy)
=================================

Hypothesis offers a number of strategies for `NumPy <http://www.numpy.org/>`_ testing,
available in the :mod:`hypothesis[numpy]` :doc:`extra </extras>`.
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.


.. automodule:: hypothesis.extra.numpy
:members: arrays, array_shapes, scalar_dtypes, boolean_dtypes, unsigned_integer_dtypes, integer_dtypes, floating_dtypes, complex_number_dtypes, datetime64_dtypes, timedelta64_dtypes, byte_string_dtypes, unicode_string_dtypes, array_dtypes, nested_dtypes
2 changes: 1 addition & 1 deletion src/hypothesis/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Flaky(HypothesisException):
depends sensitively on where it's been called from.
3. The function is timing sensitive and can fail or pass depending on
how long it takes. Try breaking it up into smaller functions which
dont' do that and testing those instead.
don't do that and testing those instead.

"""

Expand Down
Loading