Skip to content

Commit

Permalink
Merge pull request #3858 from jobh/numpy-array-elements-without-map
Browse files Browse the repository at this point in the history
Use non-mapped strategies for populating numpy arrays
  • Loading branch information
Zac-HD committed Jan 27, 2024
2 parents 626d45d + 8726f0e commit 9b67398
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

Improves the performance of the :func:`~hypothesis.extra.numpy.arrays`
strategy when generating unique values.
14 changes: 12 additions & 2 deletions hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@
from hypothesis.internal.coverage import check_function
from hypothesis.internal.reflection import proxies
from hypothesis.internal.validation import check_type
from hypothesis.strategies._internal.lazy import unwrap_strategies
from hypothesis.strategies._internal.numbers import Real
from hypothesis.strategies._internal.strategies import Ex, T, check_strategy
from hypothesis.strategies._internal.strategies import (
Ex,
MappedSearchStrategy,
T,
check_strategy,
)
from hypothesis.strategies._internal.utils import defines_strategy


Expand Down Expand Up @@ -393,7 +399,6 @@ def do_draw(self, data):
return result


@check_function
def fill_for(elements, unique, fill, name=""):
if fill is None:
if unique or not elements.has_reusable_values:
Expand Down Expand Up @@ -508,6 +513,11 @@ def arrays(
)
elements = from_dtype(dtype, **(elements or {}))
check_strategy(elements, "elements")
# If there's a redundant cast to the requested dtype, remove it. This unlocks
# optimizations such as fast unique sampled_from, and saves some time directly too.
unwrapped = unwrap_strategies(elements)
if isinstance(unwrapped, MappedSearchStrategy) and unwrapped.pack == dtype.type:
elements = unwrapped.mapped_strategy
if isinstance(shape, int):
shape = (shape,)
shape = tuple(shape)
Expand Down

0 comments on commit 9b67398

Please sign in to comment.