Repository: HypothesisWorks/hypothesis

Issue:
Should hypothesis.extra.numpy.arrays accept SearchStrategy[int] type for shape parameter?
I'm updating dependencies for an old project, and I'm now getting a mypy error for the following decorator:

```
import hypothesis.extra.numpy
import hypothesis.strategies as st
from hypothesis import given

@given(hypothesis.extra.numpy.arrays(
    hypothesis.extra.numpy.scalar_dtypes(),
    st.integers(min_value=0, max_value=100)
))
def test_check_type(arr):
    ...
```

The error is:

```
Argument 2 to "arrays" has incompatible type "SearchStrategy[int]";
expected "Union[int, Tuple[int, ...], SearchStrategy[Tuple[int, ...]]]"
```

Looking at the definition of `arrays()` in the hypothesis source code, I see that the type hint for the `shape` parameter is `Union[int, Shape, st.SearchStrategy[Shape]]`, where `Shape` is defined as `Tuple[int, ...]`. And the type hint for the return value of `st.integers()` is `SearchStrategy[int]`, so I believe the error message mypy reports is consistent with the type hints.

However, `arrays()` runs fine when a `SearchStrategy[int]` is provided as the `shape` parameter. The documentation for the `shape` parameter says:

> shape may be an integer >= 0, a tuple of such integers, or a strategy that generates such values.

The phrase "such values" in "a strategy that generates such values" is a little ambiguous. Does it mean:

> a strategy that generates a tuple of such integers

or

> a strategy that generates an integer > 0 or a tuple of such integers

The first is what the type hint in the source code implies, but the second is consistent with the runtime behavior and was my interpretation before reading the source code.

I suspect the type hint for the `shape` parameter is incorrect and that `SearchStrategy[int]` should be added, so it would look like this:

```
shape: Union[int, Shape, st.SearchStrategy[int], st.SearchStrategy[Shape]],
```

Or is my assessment incorrect? Are the type hints correct but my code needs to be updated?


Discussion:
Your assessment is correct; the proper type hint does seem to be:

```python
shape: Union[int, Shape, st.SearchStrategy[int], st.SearchStrategy[Shape]],
```

I will push a patch for this.

Resolving patch:
diff --git a/hypothesis-python/src/hypothesis/extra/numpy.py b/hypothesis-python/src/hypothesis/extra/numpy.py
--- a/hypothesis-python/src/hypothesis/extra/numpy.py
+++ b/hypothesis-python/src/hypothesis/extra/numpy.py
@@ -347,7 +347,7 @@ def fill_for(elements, unique, fill, name=""):
 @defines_strategy(force_reusable_values=True)
 def arrays(
     dtype: Any,
-    shape: Union[int, Shape, st.SearchStrategy[Shape]],
+    shape: Union[int, st.SearchStrategy[int], Shape, st.SearchStrategy[Shape]],
     *,
     elements: Optional[Union[st.SearchStrategy, Mapping[str, Any]]] = None,
     fill: Optional[st.SearchStrategy[Any]] = None,
@@ -954,7 +954,7 @@ def add_shape(loop, core):
             return tuple(x for x in (loop + core)[-32:] if x is not None)
 
         return BroadcastableShapes(
-            input_shapes=tuple(add_shape(l, c) for l, c in zip(loop_in, core_in)),
+            input_shapes=tuple(add_shape(l_in, c) for l_in, c in zip(loop_in, core_in)),
             result_shape=add_shape(loop_res, core_res),
         )
 


Test patch: