Skip to content

Commit

Permalink
Merge pull request #3602 from FrancescElies/cesc/fix-typing-array-ret…
Browse files Browse the repository at this point in the history
…urn-dtype

[TYP] return array dtype is always unkown but it's actually specified as parameter
  • Loading branch information
Zac-HD committed Apr 25, 2023
2 parents be7a9f4 + 1c7cd4d commit 4590978
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -62,6 +62,7 @@ their individual contributions.
* `Felix Sheldon <https://www.github.com/darkpaw>`_
* `Florian Bruhin <https://www.github.com/The-Compiler>`_
* `follower <https://www.github.com/follower>`_
* `Francesc Elies <https://www.github.com/FrancescElies>`_
* `Gabe Joseph <https://github.com/gjoseph92>`_
* `Gary Donovan <https://www.github.com/garyd203>`_
* `George Macon <https://www.github.com/gmacon>`_
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Expand Up @@ -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!
Expand Down
4 changes: 4 additions & 0 deletions 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`.
17 changes: 11 additions & 6 deletions hypothesis-python/src/hypothesis/extra/numpy.py
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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 -
Expand Down Expand Up @@ -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``.
Expand Down

0 comments on commit 4590978

Please sign in to comment.