Skip to content

Commit fa7d200

Browse files
committed
add conftest.txt to monkey-patch scipy sparse to fail on breaking * and **
1 parent 20a8b1b commit fa7d200

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

libpysal/conftest.txt

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# A .txt file that when renamed conftest.py will allow pytest to flag sparray troubles.
2+
#================== Added to check spmatrix usage ========================
3+
import scipy
4+
5+
def flag_this_call(*args, **kwds):
6+
raise ValueError("Old spmatrix version of bmat called")
7+
8+
scipy.sparse._construct.bmat = flag_this_call
9+
scipy.sparse._construct.rand = flag_this_call
10+
scipy.sparse._construct.rand = flag_this_call
11+
12+
class _strict_mul_mixin:
13+
def __mul__(self, other):
14+
if not scipy.sparse._sputils.isscalarlike(other):
15+
raise ValueError('Operator * used here! Change to @?')
16+
return super().__mul__(other)
17+
18+
def __rmul__(self, other):
19+
if not scipy.sparse._sputils.isscalarlike(other):
20+
raise ValueError('Operator * used here! Change to @?')
21+
return super().__rmul__(other)
22+
23+
def __imul__(self, other):
24+
if not scipy.sparse._sputils.isscalarlike(other):
25+
raise ValueError('Operator * used here! Change to @?')
26+
return super().__imul__(other)
27+
28+
def __pow__(self, *args, **kwargs):
29+
raise ValueError('spmatrix ** found! Use linalg.matrix_power?')
30+
31+
@property
32+
def A(self):
33+
raise TypeError('spmatrix A property found! Use .toarray()')
34+
35+
@property
36+
def H(self):
37+
raise TypeError('spmatrix H property found! Use .conjugate().T')
38+
39+
def asfptype(self):
40+
raise TypeError('spmatrix asfptype found! rewrite needed')
41+
42+
def get_shape(self):
43+
raise TypeError('spmatrix get_shape found! Use .shape')
44+
45+
def getformat(self):
46+
raise TypeError('spmatrix getformat found! Use .shape')
47+
48+
def getmaxprint(self):
49+
raise TypeError('spmatrix getmaxprint found! Use .shape')
50+
51+
def getnnz(self):
52+
raise TypeError('spmatrix getnnz found! Use .shape')
53+
54+
def getH(self):
55+
raise TypeError('spmatrix getH found! Use .shape')
56+
57+
def getrow(self):
58+
raise TypeError('spmatrix getrow found! Use .shape')
59+
60+
def getcol(self):
61+
raise TypeError('spmatrix getcol found! Use .shape')
62+
63+
def sum(self, *args, axis=None, **kwds):
64+
if axis is not None:
65+
raise TypeError(f'spmatrix sum found using axis={axis}!')
66+
return super().sum(*args, **kwds)
67+
68+
def sum(self, *args, axis=None, **kwds):
69+
if axis is not None:
70+
raise TypeError(f'spmatrix sum found using axis={axis}!')
71+
return super().sum(*args, **kwds)
72+
73+
def mean(self, *args, axis=None, **kwds):
74+
if axis is not None:
75+
raise TypeError(f'spmatrix sum found using axis={axis}!')
76+
return super().sum(*args, **kwds)
77+
78+
def min(self, *args, axis=None, **kwds):
79+
if axis is not None:
80+
raise TypeError(f'spmatrix sum found using axis={axis}!')
81+
return super().sum(*args, **kwds)
82+
83+
def max(self, *args, axis=None, **kwds):
84+
if axis is not None:
85+
raise TypeError(f'spmatrix sum found using axis={axis}!')
86+
return super().sum(*args, **kwds)
87+
88+
def argmin(self, *args, axis=None, **kwds):
89+
if axis is not None:
90+
raise TypeError(f'spmatrix sum found using axis={axis}!')
91+
return super().sum(*args, **kwds)
92+
93+
def argmax(self, *args, axis=None, **kwds):
94+
if axis is not None:
95+
raise TypeError(f'spmatrix sum found using axis={axis}!')
96+
return super().sum(*args, **kwds)
97+
98+
99+
class _strict_coo_matrix(_strict_mul_mixin, scipy.sparse.coo_matrix):
100+
pass
101+
102+
class _strict_bsr_matrix(_strict_mul_mixin, scipy.sparse.bsr_matrix):
103+
pass
104+
105+
class _strict_csr_matrix(_strict_mul_mixin, scipy.sparse.csr_matrix):
106+
pass
107+
108+
class _strict_csc_matrix(_strict_mul_mixin, scipy.sparse.csc_matrix):
109+
pass
110+
111+
class _strict_dok_matrix(_strict_mul_mixin, scipy.sparse.dok_matrix):
112+
pass
113+
114+
class _strict_lil_matrix(_strict_mul_mixin, scipy.sparse.lil_matrix):
115+
pass
116+
117+
class _strict_dia_matrix(_strict_mul_mixin, scipy.sparse.dia_matrix):
118+
pass
119+
120+
scipy.sparse.coo_matrix = scipy.sparse._coo.coo_matrix = _strict_coo_matrix
121+
scipy.sparse.bsr_matrix = scipy.sparse._bsr.bsr_matrix = _strict_bsr_matrix
122+
scipy.sparse.csr_matrix = scipy.sparse._csr.csr_matrix = _strict_csr_matrix
123+
scipy.sparse.csc_matrix = scipy.sparse._csc.csc_matrix = _strict_csc_matrix
124+
scipy.sparse.dok_matrix = scipy.sparse._dok.dok_matrix = _strict_dok_matrix
125+
scipy.sparse.lil_matrix = scipy.sparse._lil.lil_matrix = _strict_lil_matrix
126+
scipy.sparse.dia_matrix = scipy.sparse._dia.dia_matrix = _strict_dia_matrix
127+
128+
scipy.sparse._compressed.csr_matrix = _strict_csr_matrix
129+
130+
scipy.sparse._construct.bsr_matrix = _strict_bsr_matrix
131+
scipy.sparse._construct.coo_matrix = _strict_coo_matrix
132+
scipy.sparse._construct.csc_matrix = _strict_csc_matrix
133+
scipy.sparse._construct.csr_matrix = _strict_csr_matrix
134+
scipy.sparse._construct.dia_matrix = _strict_dia_matrix
135+
136+
scipy.sparse._extract.coo_matrix = _strict_coo_matrix
137+
138+
scipy.sparse._matrix.bsr_matrix = _strict_bsr_matrix
139+
scipy.sparse._matrix.coo_matrix = _strict_coo_matrix
140+
scipy.sparse._matrix.csc_matrix = _strict_csc_matrix
141+
scipy.sparse._matrix.csr_matrix = _strict_csr_matrix
142+
scipy.sparse._matrix.dia_matrix = _strict_dia_matrix
143+
scipy.sparse._matrix.dok_matrix = _strict_dok_matrix
144+
scipy.sparse._matrix.lil_matrix = _strict_lil_matrix
145+
146+
del _strict_coo_matrix
147+
del _strict_bsr_matrix
148+
del _strict_csr_matrix
149+
del _strict_csc_matrix
150+
del _strict_dok_matrix
151+
del _strict_lil_matrix
152+
del _strict_dia_matrix
153+
#==========================================

0 commit comments

Comments
 (0)