Skip to content

Commit 7295c14

Browse files
committed
Allow excluding NaT
1 parent 15ae3c2 commit 7295c14

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: minor
2+
3+
The :func:`~hypothesis.extra.numpy.from_dtype` function no longer generates
4+
``NaT`` ("not-a-time") values for the ``datetime64`` or ``timedelta64`` dtypes
5+
if passed ``allow_nan=False`` (:issue:`3943`).

hypothesis-python/src/hypothesis/extra/numpy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ def compat_kw(*args, **kw):
219219
# it here because we'd have to guard against equivalents in arrays()
220220
# regardless and drawing scalars is a valid use-case.
221221
res = st.sampled_from(TIME_RESOLUTIONS)
222-
result = st.builds(dtype.type, st.integers(-(2**63), 2**63 - 1), res)
222+
if allow_nan is not False:
223+
elems = st.integers(-(2**63), 2**63 - 1) | st.just("NaT")
224+
else: # NEP-7 defines the NaT value as integer -(2**63)
225+
elems = st.integers(-(2**63) + 1, 2**63 - 1)
226+
result = st.builds(dtype.type, elems, res)
223227
else:
224228
raise InvalidArgument(f"No strategy inference for {dtype}")
225229
return result.map(dtype.type)

hypothesis-python/tests/numpy/test_from_dtype.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ def test_arrays_selects_consistent_time_unit(data, dtype_str):
181181
assert (dtype_str + "[") in arr.dtype.str
182182

183183

184+
@pytest.mark.parametrize("dtype", ["m8", "M8"])
185+
def test_from_dtype_can_include_or_exclude_nat(dtype):
186+
find_any(nps.from_dtype(np.dtype(dtype), allow_nan=None), np.isnat)
187+
find_any(nps.from_dtype(np.dtype(dtype), allow_nan=True), np.isnat)
188+
assert_no_examples(nps.from_dtype(np.dtype(dtype), allow_nan=False), np.isnat)
189+
190+
184191
def test_arrays_gives_useful_error_on_inconsistent_time_unit():
185192
with pytest.raises(InvalidArgument, match="mismatch of time units in dtypes"):
186193
check_can_generate_examples(

0 commit comments

Comments
 (0)