scipy / scipy_doctest

Floating-point aware doctesting
BSD 3-Clause "New" or "Revised" License
6 stars 4 forks source link

Handle array abbreviation for n-dimensional arrays with n > 1 #64

Closed Sheila-nk closed 1 year ago

Sheila-nk commented 1 year ago

The current implementation works only for 1-dim numpy arrays:

In [1]: s = "array([     0,      1,      2, ..., 999997, 999998, 999999])"
In [2]: eval(s, {'array': np.array})
Out[2]: array([0, 1, 2, Ellipsis, 999997, 999998, 999999], dtype=object)

but produces a ValueError for multi-dim arrays as ellipses abbreviate the rows:

In [19]: s = """array([[0.        , 0.        , 0.        , ..., 0.        , 0.        ,
    ...:         0.        ],
    ...:        [0.        , 0.03333333, 0.        , ..., 0.        , 0.        ,
    ...:         0.        ],
    ...:        [0.        , 0.        , 0.06666667, ..., 0.        , 0.        ,
    ...:         0.        ],
    ...:        ...,
    ...:        [0.        , 0.        , 0.        , ..., 1.        , 0.        ,
    ...:         0.        ],
    ...:        [0.        , 0.        , 0.        , ..., 0.        , 1.03333333,
    ...:         0.        ],
    ...:        [0.        , 0.        , 0.        , ..., 0.        , 0.        ,
    ...:         1.06666667]])"""
In [20]: eval(s, {'array': np.array})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[20], line 1
----> 1 eval(s, {'array': np.array})
File <string>:1
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (7,) + inhomogeneous part.