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

[TYP] return array dtype is always unkown but it's actually specified as parameter #3602

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -60,6 +60,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]]:
Comment on lines -385 to +389
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also declare the shape here, for constant shapes... albeit by going from two to four overloads (value or strategy for each of two arguments).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get away without overloads in this case, by defining another typevar for shape but honestly I would like to leave this point for now until we sort out the rest the comments you made in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, I'll be very happy to get in whatever improvement we have and write up an issue with the rest. 👍

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