From 7295c14ec9d0e26025ffa37ebaedd47953168528 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sun, 31 Mar 2024 02:08:42 -0700 Subject: [PATCH 1/2] Allow excluding NaT --- hypothesis-python/RELEASE.rst | 5 +++++ hypothesis-python/src/hypothesis/extra/numpy.py | 6 +++++- hypothesis-python/tests/numpy/test_from_dtype.py | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 hypothesis-python/RELEASE.rst diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..51f3d7e0d3 --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,5 @@ +RELEASE_TYPE: minor + +The :func:`~hypothesis.extra.numpy.from_dtype` function no longer generates +``NaT`` ("not-a-time") values for the ``datetime64`` or ``timedelta64`` dtypes +if passed ``allow_nan=False`` (:issue:`3943`). diff --git a/hypothesis-python/src/hypothesis/extra/numpy.py b/hypothesis-python/src/hypothesis/extra/numpy.py index 90ecb400b4..9edca6ee00 100644 --- a/hypothesis-python/src/hypothesis/extra/numpy.py +++ b/hypothesis-python/src/hypothesis/extra/numpy.py @@ -219,7 +219,11 @@ def compat_kw(*args, **kw): # it here because we'd have to guard against equivalents in arrays() # regardless and drawing scalars is a valid use-case. res = st.sampled_from(TIME_RESOLUTIONS) - result = st.builds(dtype.type, st.integers(-(2**63), 2**63 - 1), res) + if allow_nan is not False: + elems = st.integers(-(2**63), 2**63 - 1) | st.just("NaT") + else: # NEP-7 defines the NaT value as integer -(2**63) + elems = st.integers(-(2**63) + 1, 2**63 - 1) + result = st.builds(dtype.type, elems, res) else: raise InvalidArgument(f"No strategy inference for {dtype}") return result.map(dtype.type) diff --git a/hypothesis-python/tests/numpy/test_from_dtype.py b/hypothesis-python/tests/numpy/test_from_dtype.py index b55d2ca48f..8dbb9d21c6 100644 --- a/hypothesis-python/tests/numpy/test_from_dtype.py +++ b/hypothesis-python/tests/numpy/test_from_dtype.py @@ -181,6 +181,13 @@ def test_arrays_selects_consistent_time_unit(data, dtype_str): assert (dtype_str + "[") in arr.dtype.str +@pytest.mark.parametrize("dtype", ["m8", "M8"]) +def test_from_dtype_can_include_or_exclude_nat(dtype): + find_any(nps.from_dtype(np.dtype(dtype), allow_nan=None), np.isnat) + find_any(nps.from_dtype(np.dtype(dtype), allow_nan=True), np.isnat) + assert_no_examples(nps.from_dtype(np.dtype(dtype), allow_nan=False), np.isnat) + + def test_arrays_gives_useful_error_on_inconsistent_time_unit(): with pytest.raises(InvalidArgument, match="mismatch of time units in dtypes"): check_can_generate_examples( From e6567e5b48c9dddb538ab3eb83c7862028c4de8d Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Sun, 31 Mar 2024 03:04:56 -0700 Subject: [PATCH 2/2] some updates --- hypothesis-python/setup.py | 2 +- .../src/hypothesis/vendor/tlds-alpha-by-domain.txt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/hypothesis-python/setup.py b/hypothesis-python/setup.py index e95cab1ad4..cfb11cddc4 100644 --- a/hypothesis-python/setup.py +++ b/hypothesis-python/setup.py @@ -60,7 +60,7 @@ def local_file(name): "pytest": ["pytest>=4.6"], "dpcontracts": ["dpcontracts>=0.4"], "redis": ["redis>=3.0.0"], - "crosshair": ["hypothesis-crosshair>=0.0.2", "crosshair-tool>=0.0.53"], + "crosshair": ["hypothesis-crosshair>=0.0.2", "crosshair-tool>=0.0.54"], # zoneinfo is an odd one: every dependency is conditional, because they're # only necessary on old versions of Python or Windows systems or emscripten. "zoneinfo": [ diff --git a/hypothesis-python/src/hypothesis/vendor/tlds-alpha-by-domain.txt b/hypothesis-python/src/hypothesis/vendor/tlds-alpha-by-domain.txt index 7b2f84dfad..08ad35057e 100644 --- a/hypothesis-python/src/hypothesis/vendor/tlds-alpha-by-domain.txt +++ b/hypothesis-python/src/hypothesis/vendor/tlds-alpha-by-domain.txt @@ -1,4 +1,4 @@ -# Version 2024031000, Last Updated Sun Mar 10 07:07:01 2024 UTC +# Version 2024033000, Last Updated Sat Mar 30 07:07:01 2024 UTC AAA AARP ABB @@ -85,7 +85,6 @@ AUSPOST AUTHOR AUTO AUTOS -AVIANCA AW AWS AX