diff --git a/AUTHORS.rst b/AUTHORS.rst index 1a1013e801..cd77860dcb 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -62,6 +62,7 @@ their individual contributions. * `Felix Sheldon `_ * `Florian Bruhin `_ * `follower `_ +* `Francesc Elies `_ * `Gabe Joseph `_ * `Gary Donovan `_ * `George Macon `_ diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 959aa7feb3..5a7f461faa 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -8,7 +8,7 @@ First off: It's great that you want to contribute to Hypothesis! Thanks! Just tell me how to make a pull request --------------------------------------- -1. Make you change and ensure it has adequate tests +1. Make your change and ensure it has adequate tests 2. Create ``hypothesis-python/RELEASE.rst`` with ``RELEASE_TYPE: patch`` for small bugfixes, or ``minor`` for new features. See recent PRs for examples. 3. Add yourself to the list in ``AUTHORS.rst`` and open a PR! diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..064fd87624 --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,4 @@ +RELEASE_TYPE: patch + +This patch fixes type annotations for the :func:`~hypothesis.extra.numpy.arrays` +strategy. Thanks to Francesc Elies for :pull:`3602`. diff --git a/hypothesis-python/src/hypothesis/extra/numpy.py b/hypothesis-python/src/hypothesis/extra/numpy.py index e2e1e095ec..a8a9e40497 100644 --- a/hypothesis-python/src/hypothesis/extra/numpy.py +++ b/hypothesis-python/src/hypothesis/extra/numpy.py @@ -9,9 +9,10 @@ # obtain one at https://mozilla.org/MPL/2.0/. import math -from typing import Any, Mapping, Optional, Sequence, Tuple, Union +from typing import Any, Mapping, Optional, Sequence, Tuple, TypeVar, Union import numpy as np +from numpy.typing import DTypeLike, NDArray from hypothesis import strategies as st from hypothesis._settings import note_deprecation @@ -374,15 +375,18 @@ def fill_for(elements, unique, fill, name=""): return fill +D = TypeVar("D", bound=DTypeLike) + + @defines_strategy(force_reusable_values=True) def arrays( - dtype: Any, + dtype: Union[D, st.SearchStrategy[D]], shape: Union[int, st.SearchStrategy[int], Shape, st.SearchStrategy[Shape]], *, - elements: Optional[Union[st.SearchStrategy, Mapping[str, Any]]] = None, + elements: Optional[Union[st.SearchStrategy[Any], Mapping[str, Any]]] = None, fill: Optional[st.SearchStrategy[Any]] = None, unique: bool = False, -) -> st.SearchStrategy[np.ndarray]: +) -> st.SearchStrategy[NDArray[D]]: r"""Returns a strategy for generating :class:`numpy:numpy.ndarray`\ s. * ``dtype`` may be any valid input to :class:`~numpy:numpy.dtype` @@ -460,6 +464,7 @@ def arrays( ) # From here on, we're only dealing with values and it's relatively simple. dtype = np.dtype(dtype) + assert isinstance(dtype, np.dtype) # help mypy out a bit... if elements is None or isinstance(elements, Mapping): if dtype.kind in ("m", "M") and "[" not in dtype.str: # For datetime and timedelta dtypes, we have a tricky situation - @@ -900,8 +905,8 @@ def integer_array_indices( shape: Shape, *, result_shape: st.SearchStrategy[Shape] = array_shapes(), - dtype: np.dtype = "int", -) -> st.SearchStrategy[Tuple[np.ndarray, ...]]: + dtype: D = np.int_, +) -> st.SearchStrategy[Tuple[NDArray[D], ...]]: """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``.