File: test_argument_validation.py

package info (click to toggle)
python-hypothesis 6.138.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,272 kB
  • sloc: python: 62,853; ruby: 1,107; sh: 253; makefile: 41; javascript: 6
file content (236 lines) | stat: -rw-r--r-- 8,996 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

from typing import Optional

import pytest

from hypothesis.errors import InvalidArgument
from hypothesis.extra.array_api import NominalVersion, make_strategies_namespace

from tests.array_api.common import MIN_VER_FOR_COMPLEX
from tests.common.debug import check_can_generate_examples


def e(name, *, _min_version: Optional[NominalVersion] = None, **kwargs):
    kw = ", ".join(f"{k}={v!r}" for k, v in kwargs.items())
    id_ = f"{name}({kw})"
    if _min_version is None:
        marks = ()
    else:
        marks = pytest.mark.xp_min_version(_min_version)
    return pytest.param(name, kwargs, id=id_, marks=marks)


@pytest.mark.parametrize(
    ("strat_name", "kwargs"),
    [
        e("arrays", dtype=1, shape=5),
        e("arrays", dtype=None, shape=5),
        e("arrays", dtype="int8", shape=(0.5,)),
        e("arrays", dtype="int8", shape=1, fill=3),
        e("arrays", dtype="int8", shape=1, elements="not a strategy"),
        e("arrays", dtype="int8", shape="not a shape or strategy"),
        e("array_shapes", min_side=2, max_side=1),
        e("array_shapes", min_dims=3, max_dims=2),
        e("array_shapes", min_dims=-1),
        e("array_shapes", min_side=-1),
        e("array_shapes", min_side="not an int"),
        e("array_shapes", max_side="not an int"),
        e("array_shapes", min_dims="not an int"),
        e("array_shapes", max_dims="not an int"),
        e("from_dtype", dtype=1),
        e("from_dtype", dtype=None),
        e("from_dtype", dtype="int8", min_value="not an int"),
        e("from_dtype", dtype="int8", max_value="not an int"),
        e("from_dtype", dtype="float32", min_value="not a float"),
        e("from_dtype", dtype="float32", max_value="not a float"),
        e("from_dtype", dtype="int8", min_value=10, max_value=5),
        e("from_dtype", dtype="float32", min_value=10, max_value=5),
        e("from_dtype", dtype="int8", min_value=-999),
        e("from_dtype", dtype="int8", max_value=-999),
        e("from_dtype", dtype="int8", min_value=999),
        e("from_dtype", dtype="int8", max_value=999),
        e("from_dtype", dtype="uint8", min_value=-999),
        e("from_dtype", dtype="uint8", max_value=-999),
        e("from_dtype", dtype="uint8", min_value=999),
        e("from_dtype", dtype="uint8", max_value=999),
        e("from_dtype", dtype="float32", min_value=-4e38),
        e("from_dtype", dtype="float32", max_value=-4e38),
        e("from_dtype", dtype="float32", min_value=4e38),
        e("from_dtype", dtype="float32", max_value=4e38),
        e("integer_dtypes", sizes=()),
        e("integer_dtypes", sizes=(3,)),
        e("unsigned_integer_dtypes", sizes=()),
        e("unsigned_integer_dtypes", sizes=(3,)),
        e("floating_dtypes", sizes=()),
        e("floating_dtypes", sizes=(3,)),
        e("complex_dtypes", _min_version=MIN_VER_FOR_COMPLEX, sizes=()),
        e("complex_dtypes", _min_version=MIN_VER_FOR_COMPLEX, sizes=(3,)),
        e("valid_tuple_axes", ndim=-1),
        e("valid_tuple_axes", ndim=2, min_size=-1),
        e("valid_tuple_axes", ndim=2, min_size=3, max_size=10),
        e("valid_tuple_axes", ndim=2, min_size=2, max_size=1),
        e("valid_tuple_axes", ndim=2.0, min_size=2, max_size=1),
        e("valid_tuple_axes", ndim=2, min_size=1.0, max_size=2),
        e("valid_tuple_axes", ndim=2, min_size=1, max_size=2.0),
        e("valid_tuple_axes", ndim=2, min_size=1, max_size=3),
        e("broadcastable_shapes", shape="a"),
        e("broadcastable_shapes", shape=(2, 2), min_side="a"),
        e("broadcastable_shapes", shape=(2, 2), min_dims="a"),
        e("broadcastable_shapes", shape=(2, 2), max_side="a"),
        e("broadcastable_shapes", shape=(2, 2), max_dims="a"),
        e("broadcastable_shapes", shape=(2, 2), min_side=-1),
        e("broadcastable_shapes", shape=(2, 2), min_dims=-1),
        e("broadcastable_shapes", shape=(2, 2), min_side=1, max_side=0),
        e("broadcastable_shapes", shape=(2, 2), min_dims=1, max_dims=0),
        e(
            "broadcastable_shapes",  # max_side too small
            shape=(5, 1),
            min_dims=2,
            max_dims=4,
            min_side=2,
            max_side=3,
        ),
        e(
            "broadcastable_shapes",  # min_side too large
            shape=(0, 1),
            min_dims=2,
            max_dims=4,
            min_side=2,
            max_side=3,
        ),
        e(
            "broadcastable_shapes",  # default max_dims unsatisfiable
            shape=(5, 3, 2, 1),
            min_dims=3,
            max_dims=None,
            min_side=2,
            max_side=3,
        ),
        e(
            "broadcastable_shapes",  # default max_dims unsatisfiable
            shape=(0, 3, 2, 1),
            min_dims=3,
            max_dims=None,
            min_side=2,
            max_side=3,
        ),
        e("mutually_broadcastable_shapes", num_shapes=0),
        e("mutually_broadcastable_shapes", num_shapes="a"),
        e("mutually_broadcastable_shapes", num_shapes=2, base_shape="a"),
        e(
            "mutually_broadcastable_shapes",  # min_side is invalid type
            num_shapes=2,
            min_side="a",
        ),
        e(
            "mutually_broadcastable_shapes",  # min_dims is invalid type
            num_shapes=2,
            min_dims="a",
        ),
        e(
            "mutually_broadcastable_shapes",  # max_side is invalid type
            num_shapes=2,
            max_side="a",
        ),
        e(
            "mutually_broadcastable_shapes",  # max_side is invalid type
            num_shapes=2,
            max_dims="a",
        ),
        e(
            "mutually_broadcastable_shapes",  # min_side is out of domain
            num_shapes=2,
            min_side=-1,
        ),
        e(
            "mutually_broadcastable_shapes",  # min_dims is out of domain
            num_shapes=2,
            min_dims=-1,
        ),
        e(
            "mutually_broadcastable_shapes",  # max_side < min_side
            num_shapes=2,
            min_side=1,
            max_side=0,
        ),
        e(
            "mutually_broadcastable_shapes",  # max_dims < min_dims
            num_shapes=2,
            min_dims=1,
            max_dims=0,
        ),
        e(
            "mutually_broadcastable_shapes",  # max_side too small
            num_shapes=2,
            base_shape=(5, 1),
            min_dims=2,
            max_dims=4,
            min_side=2,
            max_side=3,
        ),
        e(
            "mutually_broadcastable_shapes",  # min_side too large
            num_shapes=2,
            base_shape=(0, 1),
            min_dims=2,
            max_dims=4,
            min_side=2,
            max_side=3,
        ),
        e(
            "mutually_broadcastable_shapes",  # user-specified max_dims unsatisfiable
            num_shapes=1,
            base_shape=(5, 3, 2, 1),
            min_dims=3,
            max_dims=4,
            min_side=2,
            max_side=3,
        ),
        e(
            "mutually_broadcastable_shapes",  # user-specified max_dims unsatisfiable
            num_shapes=2,
            base_shape=(0, 3, 2, 1),
            min_dims=3,
            max_dims=4,
            min_side=2,
            max_side=3,
        ),
        e("indices", shape=0),
        e("indices", shape=("1", "2")),
        e("indices", shape=(0, -1)),
        e("indices", shape=(0, 0), allow_newaxis=None),
        e("indices", shape=(0, 0), allow_ellipsis=None),
        e("indices", shape=(0, 0), min_dims=-1),
        e("indices", shape=(0, 0), min_dims=1.0),
        e("indices", shape=(0, 0), max_dims=-1),
        e("indices", shape=(0, 0), max_dims=1.0),
        e("indices", shape=(0, 0), min_dims=2, max_dims=1),
        e("indices", shape=(3, 3, 3), min_dims=4),
        e("indices", shape=(3, 3, 3), max_dims=5),
        e("indices", shape=5, min_dims=0),
        e("indices", shape=(5,), min_dims=2),
        e("indices", shape=(5,), max_dims=2),
    ],
)
def test_raise_invalid_argument(xp, xps, strat_name, kwargs):
    """Strategies raise helpful error with invalid arguments."""
    strat_func = getattr(xps, strat_name)
    strat = strat_func(**kwargs)
    with pytest.raises(InvalidArgument):
        check_can_generate_examples(strat)


@pytest.mark.parametrize("api_version", [..., "latest", "1970.01", 42])
def test_make_strategies_namespace_raise_invalid_argument(xp, api_version):
    """Function raises helpful error with invalid arguments."""
    with pytest.raises(InvalidArgument):
        make_strategies_namespace(xp, api_version=api_version)