Skip to content

Commit

Permalink
Allow excluding NaT
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Mar 31, 2024
1 parent 15ae3c2 commit 7295c14
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -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`).
6 changes: 5 additions & 1 deletion hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions hypothesis-python/tests/numpy/test_from_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7295c14

Please sign in to comment.