Skip to content

Commit 3174f30

Browse files
committed
Move requires_numpy, etc. decorators to globals
test_eigen.py and test_numpy_*.py have the same @pytest.requires_eigen_and_numpy or @pytest.requires_numpy on every single test; this changes them to use pytest's global `pytestmark = ...` instead to disable the entire module when numpy and/or eigen aren't available.
1 parent b4e306f commit 3174f30

File tree

4 files changed

+8
-44
lines changed

4 files changed

+8
-44
lines changed

tests/test_eigen.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
pytestmark = pytest.requires_eigen_and_numpy
4+
35
with pytest.suppress(ImportError):
46
import numpy as np
57

@@ -18,7 +20,6 @@ def assert_sparse_equal_ref(sparse_mat):
1820
assert_equal_ref(sparse_mat.todense())
1921

2022

21-
@pytest.requires_eigen_and_numpy
2223
def test_fixed():
2324
from pybind11_tests import fixed_r, fixed_c, fixed_copy_r, fixed_copy_c
2425

@@ -30,7 +31,6 @@ def test_fixed():
3031
assert_equal_ref(fixed_copy_c(fixed_r()))
3132

3233

33-
@pytest.requires_eigen_and_numpy
3434
def test_dense():
3535
from pybind11_tests import dense_r, dense_c, dense_copy_r, dense_copy_c
3636

@@ -42,7 +42,6 @@ def test_dense():
4242
assert_equal_ref(dense_copy_c(dense_r()))
4343

4444

45-
@pytest.requires_eigen_and_numpy
4645
def test_partially_fixed():
4746
from pybind11_tests import (partial_copy_four_rm_r, partial_copy_four_rm_c,
4847
partial_copy_four_cm_r, partial_copy_four_cm_c)
@@ -65,7 +64,6 @@ def test_partially_fixed():
6564
partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
6665

6766

68-
@pytest.requires_eigen_and_numpy
6967
def test_mutator_descriptors():
7068
from pybind11_tests import fixed_mutator_r, fixed_mutator_c, fixed_mutator_a
7169
zr = np.arange(30, dtype='float32').reshape(5, 6) # row-major
@@ -94,7 +92,6 @@ def test_mutator_descriptors():
9492
fixed_mutator_a(zr)
9593

9694

97-
@pytest.requires_eigen_and_numpy
9895
def test_cpp_casting():
9996
from pybind11_tests import (cpp_copy, cpp_ref_c, cpp_ref_r, cpp_ref_any,
10097
fixed_r, fixed_c, get_cm_ref, get_rm_ref, ReturnTester)
@@ -120,7 +117,6 @@ def test_cpp_casting():
120117
assert cpp_ref_any(get_cm_ref()) == 21.
121118

122119

123-
@pytest.requires_eigen_and_numpy
124120
def test_pass_readonly_array():
125121
from pybind11_tests import fixed_copy_r, fixed_r, fixed_r_const
126122
z = np.full((5, 6), 42.0)
@@ -131,7 +127,6 @@ def test_pass_readonly_array():
131127
np.testing.assert_array_equal(fixed_copy_r(fixed_r_const()), fixed_r_const())
132128

133129

134-
@pytest.requires_eigen_and_numpy
135130
def test_nonunit_stride_from_python():
136131
from pybind11_tests import (
137132
double_row, double_col, double_mat_cm, double_mat_rm,
@@ -157,7 +152,6 @@ def test_nonunit_stride_from_python():
157152
np.testing.assert_array_equal(counting_mat, [[0., 2, 2], [6, 16, 10], [6, 14, 8]])
158153

159154

160-
@pytest.requires_eigen_and_numpy
161155
def test_nonunit_stride_to_python():
162156
from pybind11_tests import diagonal, diagonal_1, diagonal_n, block
163157

@@ -171,7 +165,6 @@ def test_nonunit_stride_to_python():
171165
assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
172166

173167

174-
@pytest.requires_eigen_and_numpy
175168
def test_eigen_ref_to_python():
176169
from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4
177170

@@ -192,7 +185,6 @@ def array_copy_but_one(a, r, c, v):
192185
return z
193186

194187

195-
@pytest.requires_eigen_and_numpy
196188
def test_eigen_return_references():
197189
"""Tests various ways of returning references and non-referencing copies"""
198190
from pybind11_tests import ReturnTester
@@ -322,7 +314,6 @@ def assert_keeps_alive(cl, method, *args):
322314
assert cstats.alive() == start_with
323315

324316

325-
@pytest.requires_eigen_and_numpy
326317
def test_eigen_keepalive():
327318
from pybind11_tests import ReturnTester, ConstructorStats
328319
a = ReturnTester()
@@ -346,7 +337,6 @@ def test_eigen_keepalive():
346337
assert_keeps_alive(ReturnTester, meth, 4, 3, 2, 1)
347338

348339

349-
@pytest.requires_eigen_and_numpy
350340
def test_eigen_ref_mutators():
351341
"""Tests whether Eigen can mutate numpy values"""
352342
from pybind11_tests import add_rm, add_cm, add_any, add1, add2
@@ -416,7 +406,6 @@ def test_eigen_ref_mutators():
416406
add_rm(zi)
417407

418408

419-
@pytest.requires_eigen_and_numpy
420409
def test_numpy_ref_mutators():
421410
"""Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
422411
from pybind11_tests import (
@@ -465,7 +454,6 @@ def test_numpy_ref_mutators():
465454
assert zc[1, 2] == 99 # Make sure we aren't referencing the original
466455

467456

468-
@pytest.requires_eigen_and_numpy
469457
def test_both_ref_mutators():
470458
"""Tests a complex chain of nested eigen/numpy references"""
471459
from pybind11_tests import (
@@ -509,7 +497,6 @@ def test_both_ref_mutators():
509497
assert np.all(y == yexpect)
510498

511499

512-
@pytest.requires_eigen_and_numpy
513500
def test_nocopy_wrapper():
514501
from pybind11_tests import get_elem, get_elem_nocopy, get_elem_rm_nocopy
515502
# get_elem requires a column-contiguous matrix reference, but should be
@@ -556,7 +543,6 @@ def test_nocopy_wrapper():
556543
', flags.c_contiguous' in str(excinfo.value))
557544

558545

559-
@pytest.requires_eigen_and_numpy
560546
def test_special_matrix_objects():
561547
from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower
562548

@@ -577,7 +563,6 @@ def test_special_matrix_objects():
577563
assert np.all(symmetric_upper(asymm) == symm_upper)
578564

579565

580-
@pytest.requires_eigen_and_numpy
581566
def test_dense_signature(doc):
582567
from pybind11_tests import double_col, double_row, double_mat_rm
583568

tests/test_numpy_array.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
pytestmark = pytest.requires_numpy
4+
35
with pytest.suppress(ImportError):
46
import numpy as np
57

@@ -9,7 +11,6 @@ def arr():
911
return np.array([[1, 2, 3], [4, 5, 6]], '<u2')
1012

1113

12-
@pytest.requires_numpy
1314
def test_array_attributes():
1415
from pybind11_tests.array import (
1516
ndim, shape, strides, writeable, size, itemsize, nbytes, owndata
@@ -53,7 +54,6 @@ def test_array_attributes():
5354
assert not owndata(a)
5455

5556

56-
@pytest.requires_numpy
5757
@pytest.mark.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
5858
def test_index_offset(arr, args, ret):
5959
from pybind11_tests.array import index_at, index_at_t, offset_at, offset_at_t
@@ -63,7 +63,6 @@ def test_index_offset(arr, args, ret):
6363
assert offset_at_t(arr, *args) == ret * arr.dtype.itemsize
6464

6565

66-
@pytest.requires_numpy
6766
def test_dim_check_fail(arr):
6867
from pybind11_tests.array import (index_at, index_at_t, offset_at, offset_at_t, data, data_t,
6968
mutate_data, mutate_data_t)
@@ -74,7 +73,6 @@ def test_dim_check_fail(arr):
7473
assert str(excinfo.value) == 'too many indices for an array: 3 (ndim = 2)'
7574

7675

77-
@pytest.requires_numpy
7876
@pytest.mark.parametrize('args, ret',
7977
[([], [1, 2, 3, 4, 5, 6]),
8078
([1], [4, 5, 6]),
@@ -87,7 +85,6 @@ def test_data(arr, args, ret):
8785
assert all(data(arr, *args)[1::2] == 0)
8886

8987

90-
@pytest.requires_numpy
9188
def test_mutate_readonly(arr):
9289
from pybind11_tests.array import mutate_data, mutate_data_t, mutate_at_t
9390
arr.flags.writeable = False
@@ -97,7 +94,6 @@ def test_mutate_readonly(arr):
9794
assert str(excinfo.value) == 'array is not writeable'
9895

9996

100-
@pytest.requires_numpy
10197
@pytest.mark.parametrize('dim', [0, 1, 3])
10298
def test_at_fail(arr, dim):
10399
from pybind11_tests.array import at_t, mutate_at_t
@@ -107,7 +103,6 @@ def test_at_fail(arr, dim):
107103
assert str(excinfo.value) == 'index dimension mismatch: {} (ndim = 2)'.format(dim)
108104

109105

110-
@pytest.requires_numpy
111106
def test_at(arr):
112107
from pybind11_tests.array import at_t, mutate_at_t
113108

@@ -118,7 +113,6 @@ def test_at(arr):
118113
assert all(mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
119114

120115

121-
@pytest.requires_numpy
122116
def test_mutate_data(arr):
123117
from pybind11_tests.array import mutate_data, mutate_data_t
124118

@@ -135,7 +129,6 @@ def test_mutate_data(arr):
135129
assert all(mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
136130

137131

138-
@pytest.requires_numpy
139132
def test_bounds_check(arr):
140133
from pybind11_tests.array import (index_at, index_at_t, data, data_t,
141134
mutate_data, mutate_data_t, at_t, mutate_at_t)
@@ -150,7 +143,6 @@ def test_bounds_check(arr):
150143
assert str(excinfo.value) == 'index 4 is out of bounds for axis 1 with size 3'
151144

152145

153-
@pytest.requires_numpy
154146
def test_make_c_f_array():
155147
from pybind11_tests.array import (
156148
make_c_array, make_f_array
@@ -161,7 +153,6 @@ def test_make_c_f_array():
161153
assert not make_f_array().flags.c_contiguous
162154

163155

164-
@pytest.requires_numpy
165156
def test_wrap():
166157
from pybind11_tests.array import wrap
167158

@@ -212,7 +203,6 @@ def assert_references(a, b, base=None):
212203
assert_references(a1d, a2, a1)
213204

214205

215-
@pytest.requires_numpy
216206
def test_numpy_view(capture):
217207
from pybind11_tests.array import ArrayClass
218208
with capture:
@@ -242,22 +232,19 @@ def test_numpy_view(capture):
242232

243233

244234
@pytest.unsupported_on_pypy
245-
@pytest.requires_numpy
246235
def test_cast_numpy_int64_to_uint64():
247236
from pybind11_tests.array import function_taking_uint64
248237
function_taking_uint64(123)
249238
function_taking_uint64(np.uint64(123))
250239

251240

252-
@pytest.requires_numpy
253241
def test_isinstance():
254242
from pybind11_tests.array import isinstance_untyped, isinstance_typed
255243

256244
assert isinstance_untyped(np.array([1, 2, 3]), "not an array")
257245
assert isinstance_typed(np.array([1.0, 2.0, 3.0]))
258246

259247

260-
@pytest.requires_numpy
261248
def test_constructors():
262249
from pybind11_tests.array import default_constructors, converting_constructors
263250

tests/test_numpy_dtypes.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
import pytest
33

4+
pytestmark = pytest.requires_numpy
5+
46
with pytest.suppress(ImportError):
57
import numpy as np
68

@@ -58,7 +60,6 @@ def assert_equal(actual, expected_data, expected_dtype):
5860
np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype))
5961

6062

61-
@pytest.requires_numpy
6263
def test_format_descriptors():
6364
from pybind11_tests import get_format_unbound, print_format_descriptors
6465

@@ -85,7 +86,6 @@ def test_format_descriptors():
8586
]
8687

8788

88-
@pytest.requires_numpy
8989
def test_dtype(simple_dtype):
9090
from pybind11_tests import (print_dtypes, test_dtype_ctors, test_dtype_methods,
9191
trailing_padding_dtype, buffer_to_dtype)
@@ -113,7 +113,6 @@ def test_dtype(simple_dtype):
113113
assert trailing_padding_dtype() == buffer_to_dtype(np.zeros(1, trailing_padding_dtype()))
114114

115115

116-
@pytest.requires_numpy
117116
def test_recarray(simple_dtype, packed_dtype):
118117
from pybind11_tests import (create_rec_simple, create_rec_packed, create_rec_nested,
119118
print_rec_simple, print_rec_packed, print_rec_nested,
@@ -178,7 +177,6 @@ def test_recarray(simple_dtype, packed_dtype):
178177
np.testing.assert_equal(arr['a'], create_rec_partial(3))
179178

180179

181-
@pytest.requires_numpy
182180
def test_array_constructors():
183181
from pybind11_tests import test_array_ctors
184182

@@ -191,7 +189,6 @@ def test_array_constructors():
191189
np.testing.assert_array_equal(test_array_ctors(40 + i), data)
192190

193191

194-
@pytest.requires_numpy
195192
def test_string_array():
196193
from pybind11_tests import create_string_array, print_string_array
197194

@@ -210,7 +207,6 @@ def test_string_array():
210207
assert dtype == arr.dtype
211208

212209

213-
@pytest.requires_numpy
214210
def test_enum_array():
215211
from pybind11_tests import create_enum_array, print_enum_array
216212

@@ -227,14 +223,12 @@ def test_enum_array():
227223
assert create_enum_array(0).dtype == dtype
228224

229225

230-
@pytest.requires_numpy
231226
def test_signature(doc):
232227
from pybind11_tests import create_rec_nested
233228

234229
assert doc(create_rec_nested) == "create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
235230

236231

237-
@pytest.requires_numpy
238232
def test_scalar_conversion():
239233
from pybind11_tests import (create_rec_simple, f_simple,
240234
create_rec_packed, f_packed,
@@ -256,7 +250,6 @@ def test_scalar_conversion():
256250
assert 'incompatible function arguments' in str(excinfo.value)
257251

258252

259-
@pytest.requires_numpy
260253
def test_register_dtype():
261254
from pybind11_tests import register_dtype
262255

tests/test_numpy_vectorize.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import pytest
22

3+
pytestmark = pytest.requires_numpy
4+
35
with pytest.suppress(ImportError):
46
import numpy as np
57

68

7-
@pytest.requires_numpy
89
def test_vectorize(capture):
910
from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3
1011

@@ -58,7 +59,6 @@ def test_vectorize(capture):
5859
"""
5960

6061

61-
@pytest.requires_numpy
6262
def test_type_selection():
6363
from pybind11_tests import selective_func
6464

@@ -67,7 +67,6 @@ def test_type_selection():
6767
assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
6868

6969

70-
@pytest.requires_numpy
7170
def test_docs(doc):
7271
from pybind11_tests import vectorized_func
7372

0 commit comments

Comments
 (0)