Debian Codegraph @

hypothesis-python/tests/pandas/test_argument_validation.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import re
12 from datetime import datetime, timedelta
13
14 import pandas as pd
15 import pytest
16
17 from hypothesis import given, strategies as st
18 from hypothesis.errors import InvalidArgument
19 from hypothesis.extra import pandas as pdst
20 from hypothesis.extra.pandas.impl import IntegerDtype
21
22 from tests.common.arguments import argument_validation_test, e
23 from tests.common.debug import check_can_generate_examples, find_any
24 from tests.common.utils import checks_deprecated_behaviour
25
26 BAD_ARGS = [
27 e(pdst.data_frames),
28 e(pdst.data_frames, pdst.columns(1, dtype="not a dtype")),
29 e(pdst.data_frames, pdst.columns(1, elements="not a strategy")),
30 e(pdst.data_frames, pdst.columns([[]])),
31 e(pdst.data_frames, [], index=[]),
32 e(pdst.data_frames, [], rows=st.fixed_dictionaries({"A": st.just(1)})),
33 e(pdst.data_frames, pdst.columns(1)),
34 e(pdst.data_frames, pdst.columns(1, dtype=float, fill=1)),
35 e(pdst.data_frames, pdst.columns(1, dtype=float, elements=1)),
36 e(pdst.data_frames, pdst.columns(1, fill=1, dtype=float)),
37 e(pdst.data_frames, pdst.columns(["A", "A"], dtype=float)),
38 pytest.param(
39 *e(pdst.data_frames, pdst.columns(1, elements=st.none(), dtype=int)),
40 marks=pytest.mark.skipif(IntegerDtype, reason="works with integer NA"),
41 ),
42 e(pdst.data_frames, pdst.columns(1, elements=st.text(), dtype=int)),
43 e(pdst.data_frames, 1),
44 e(pdst.data_frames, [1]),
45 e(pdst.data_frames, pdst.columns(1, dtype="category")),
46 e(
47 pdst.data_frames,
48 pdst.columns(["A"], dtype=bool),
49 rows=st.tuples(st.booleans(), st.booleans()),
50 ),
51 e(
52 pdst.data_frames,
53 pdst.columns(1, elements=st.booleans()),
54 rows=st.tuples(st.booleans()),
55 ),
56 e(pdst.data_frames, rows=st.integers(), index=pdst.range_indexes(0, 0)),
57 e(pdst.data_frames, rows=st.integers(), index=pdst.range_indexes(1, 1)),
58 e(pdst.data_frames, pdst.columns(1, dtype=int), rows=st.integers()),
59 e(
60 pdst.data_frames,
61 columns=pdst.columns(["a", "b"], dtype=str, elements=st.text()),
62 rows=st.just({"a": "x"}),
63 index=pdst.indexes(dtype=int, min_size=1),
64 ),
65 e(
66 pdst.data_frames,
67 columns=pdst.columns(["a", "b"], dtype=str, elements=st.text()),
68 rows=st.just(["x"]),
69 index=pdst.indexes(dtype=int, min_size=1),
70 ),
71 e(pdst.indexes),
72 e(pdst.indexes, dtype="category"),
73 e(pdst.indexes, dtype="not a dtype"),
74 e(pdst.indexes, elements="not a strategy"),
75 e(pdst.indexes, elements=st.text(), dtype=float),
76 pytest.param(
77 *e(pdst.indexes, elements=st.none(), dtype=int),
78 marks=pytest.mark.skipif(IntegerDtype, reason="works with integer NA"),
79 ),
80 e(pdst.indexes, elements=st.text(), dtype=int),
81 e(pdst.indexes, elements=st.just("9" * 30), dtype=int), # OverflowError handling
82 e(pdst.indexes, elements=st.integers(0, 10), dtype=st.sampled_from([int, float])),
83 e(pdst.indexes, dtype=int, max_size=0, min_size=1),
84 e(pdst.indexes, dtype=int, unique="true"),
85 e(pdst.indexes, dtype=int, min_size="0"),
86 e(pdst.indexes, dtype=int, max_size="1"),
87 e(pdst.range_indexes, 1, 0),
88 e(pdst.range_indexes, min_size="0"),
89 e(pdst.range_indexes, max_size="1"),
90 e(pdst.range_indexes, name=""),
91 e(pdst.series),
92 e(pdst.series, dtype="not a dtype"),
93 e(pdst.series, elements="not a strategy"),
94 pytest.param(
95 *e(pdst.series, elements=st.none(), dtype=int),
96 marks=pytest.mark.skipif(IntegerDtype, reason="works with integer NA"),
97 ),
98 e(pdst.series, elements=st.text(), dtype=int),
99 e(pdst.series, dtype="category"),
100 e(pdst.series, index="not a strategy"),
101 ]
102
103
104 test_raise_invalid_argument = argument_validation_test(BAD_ARGS)
105
106 lo, hi = pd.Timestamp(2017, 1, 1), pd.Timestamp(2084, 12, 21)
107
108
109 @given(st.datetimes(min_value=lo, max_value=hi))
110 def test_timestamp_as_datetime_bounds(dt):
111 # Would have caught https://github.com/HypothesisWorks/hypothesis/issues/2406
112 assert isinstance(dt, datetime)
113 assert lo <= dt <= hi
114 assert not isinstance(dt, pd.Timestamp)
115
116
117 @checks_deprecated_behaviour
118 def test_confusing_object_dtype_aliases():
119 check_can_generate_examples(
120 pdst.series(elements=st.tuples(st.integers()), dtype=tuple)
121 )
122
123
124 @pytest.mark.skipif(
125 not IntegerDtype, reason="Nullable types not available in this version of Pandas"
126 )
127 def test_pandas_nullable_types_class():
128 st = pdst.series(dtype=pd.core.arrays.integer.Int8Dtype)
129 with pytest.raises(
130 InvalidArgument, match="Otherwise it would be treated as dtype=object"
131 ):
132 find_any(st, lambda s: s.isna().any())
133
134
135 @pytest.mark.parametrize(
136 "dtype_,expected_unit",
137 [
138 (datetime, "datetime64[ns]"),
139 (pd.Timestamp, "datetime64[ns]"),
140 (timedelta, "timedelta64[ns]"),
141 (pd.Timedelta, "timedelta64[ns]"),
142 ],
143 )
144 def test_invalid_datetime_or_timedelta_dtype_raises_error(dtype_, expected_unit):
145 with pytest.raises(InvalidArgument, match=re.escape(expected_unit)):
146 check_can_generate_examples(pdst.series(dtype=dtype_))