Skip to content

Commit abbc68d

Browse files
committed
BUG: handle numpy 2.2 array abbreviation
NumPy 2.2 added shape= to the repr of abbreviated arrays. This is not a legal argument to `array(...)` constructor, thus the result cannot be eval-ed. Handle this case by removing the shape= bit.
1 parent 78b24e3 commit abbc68d

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

scipy_doctest/impl.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ def has_masked(got):
262262
return 'masked_array' in got and '--' in got
263263

264264

265+
def remove_shape_from_abbrv(s_got):
266+
"""NumPy 2.2 added shape=(123,) to abbreviated array repr. Remove it.
267+
"""
268+
if "shape=" in s_got:
269+
# handle
270+
# array(..., shape=(1000,))
271+
# array(..., shape=(100, 100))
272+
# array(..., shape=(100, 100), dtype=uint16)
273+
grp = re.match(r'(.+) shape=\(([\d\s,]+\))(.+)', s_got, flags=re.DOTALL).groups()
274+
275+
s_got = grp[0] + grp[-1]
276+
s_got = s_got.replace(',,', ',')
277+
278+
return ''.join(s_got.split('...,'))
279+
280+
265281
class DTChecker(doctest.OutputChecker):
266282
obj_pattern = re.compile(r'at 0x[0-9a-fA-F]+>')
267283
vanilla = doctest.OutputChecker()
@@ -325,11 +341,11 @@ def check_output(self, want, got, optionflags):
325341
return self.check_output(s_want, s_got, optionflags)
326342

327343
#handle array abbreviation for n-dimensional arrays, n >= 1
328-
ndim_array = (s_want.startswith("array([") and s_want.endswith("])") and
329-
s_got.startswith("array([") and s_got.endswith("])"))
344+
ndim_array = (s_want.startswith("array([") and "..." in s_want and
345+
s_got.startswith("array([") and "..." in s_got)
330346
if ndim_array:
331-
s_want = ''.join(s_want.split('...,'))
332-
s_got = ''.join(s_got.split('...,'))
347+
s_want = remove_shape_from_abbrv(s_want)
348+
s_got = remove_shape_from_abbrv(s_got)
333349
return self.check_output(s_want, s_got, optionflags)
334350

335351
# maybe we are dealing with masked arrays?

scipy_doctest/tests/module_cases.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,43 @@ def array_abbreviation():
193193
[0, 0, 0, ..., 0, 0, 1000]])
194194
"""
195195

196+
197+
def array_abbreviation_2():
198+
""" NumPy 2.2 adds shape=(...) to abbreviated arrays.
199+
200+
So the actual numpy==2.2.0 output below is
201+
# array([ 0, 1, 2, ..., 9997, 9998, 9999], shape=(10000,))
202+
203+
This is not a valid argument to `array(...), so it cannot be eval-ed,
204+
and need to be removed for doctesting.
205+
206+
>>> import numpy as np
207+
>>> np.arange(10000)
208+
array([ 0, 1, 2, ..., 9997, 9998, 9999])
209+
210+
>>> np.arange(10000, dtype=np.uint16)
211+
array([ 0, 1, 2, ..., 9997, 9998, 9999], dtype=np.uint16)
212+
213+
>>> np.arange(5000).reshape(50, 100)
214+
array([[ 0, 1, 2, ..., 97, 98, 99],
215+
[ 100, 101, 102, ..., 197, 198, 199],
216+
[ 200, 201, 202, ..., 297, 298, 299],
217+
...,
218+
[4700, 4701, 4702, ..., 4797, 4798, 4799],
219+
[4800, 4801, 4802, ..., 4897, 4898, 4899],
220+
[4900, 4901, 4902, ..., 4997, 4998, 4999]])
221+
222+
>>> np.arange(5000, dtype=np.uint16).reshape(50, 100)
223+
array([[ 0, 1, 2, ..., 97, 98, 99],
224+
[ 100, 101, 102, ..., 197, 198, 199],
225+
[ 200, 201, 202, ..., 297, 298, 299],
226+
...,
227+
[4700, 4701, 4702, ..., 4797, 4798, 4799],
228+
[4800, 4801, 4802, ..., 4897, 4898, 4899],
229+
[4900, 4901, 4902, ..., 4997, 4998, 4999]], dtype=uint16)
230+
"""
231+
232+
196233
def nan_equal():
197234
"""
198235
Test that nans are treated as equal.

0 commit comments

Comments
 (0)