From ef90ea231895471757a9d61384c1edd253590c15 Mon Sep 17 00:00:00 2001 From: Joachim B Haga Date: Thu, 8 Jun 2023 16:12:27 +0200 Subject: [PATCH] Parameterized dtypes are not supported --- hypothesis-python/RELEASE.rst | 7 +++---- hypothesis-python/src/hypothesis/extra/numpy.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst index 0594b7b155..e9b7572266 100644 --- a/hypothesis-python/RELEASE.rst +++ b/hypothesis-python/RELEASE.rst @@ -1,6 +1,5 @@ RELEASE_TYPE: minor -Implement support for :func:`hypothesis.strategies.from_type` to handle generic numpy types (such -as :class:`numpy.typing.ArrayLike`) and parameterized numpy arrays (such as -:class:`numpy.ndarray[scalar type] ` or -:class:`numpy.typing.NDArray[scalar type] `). This closes :issue:`3150`. +Implement support for :func:`hypothesis.strategies.from_type` to handle generic +and parameterized numpy array types (:class:`numpy.ndarray[scalar type] `, +numpy.typing.ArrayLike, etc). diff --git a/hypothesis-python/src/hypothesis/extra/numpy.py b/hypothesis-python/src/hypothesis/extra/numpy.py index 0808b240e2..90dbcd1580 100644 --- a/hypothesis-python/src/hypothesis/extra/numpy.py +++ b/hypothesis-python/src/hypothesis/extra/numpy.py @@ -62,7 +62,6 @@ def _try_import(mod_name: str, attr_name: str) -> Any: if TYPE_CHECKING: from numpy.typing import DTypeLike, NDArray else: - DTypeLike = _try_import("numpy.typing", "DTypeLike") NDArray = _try_import("numpy.typing", "NDArray") ArrayLike = _try_import("numpy.typing", "ArrayLike") @@ -1073,7 +1072,14 @@ def _from_type(thing: Type[Ex]) -> Optional[st.SearchStrategy[Ex]]: ) if thing == np.dtype: - return array_dtypes() + # Note: Parameterized dtypes and DTypeLike are not supported. + return st.one_of( + scalar_dtypes(), + byte_string_dtypes(), + unicode_string_dtypes(), + array_dtypes(), + nested_dtypes(), + ) if thing == ArrayLike: # We override the default type resolution to ensure the "coercible to