From 81a3f0df46c8e115f318b5e1b819de2257118500 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Mon, 8 Jul 2024 19:14:33 -0500 Subject: [PATCH] Testing: Fix failing tests due to Numpy 2.0 --- spyder/widgets/collectionseditor.py | 2 +- spyder/widgets/tests/test_collectioneditor.py | 76 ++++++++++++++----- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/spyder/widgets/collectionseditor.py b/spyder/widgets/collectionseditor.py index dc4970fac1e..e6c80f0774f 100644 --- a/spyder/widgets/collectionseditor.py +++ b/spyder/widgets/collectionseditor.py @@ -1879,7 +1879,7 @@ def __init__(self): 'ddataframe': test_df, 'None': None, 'unsupported1': np.arccos, - 'unsupported2': np.cast, + 'unsupported2': np.asarray, # Test for spyder-ide/spyder#3518. 'big_struct_array': np.zeros(1000, dtype=[('ID', 'f8'), ('param1', 'f8', 5000)]), diff --git a/spyder/widgets/tests/test_collectioneditor.py b/spyder/widgets/tests/test_collectioneditor.py index e5959cf8a09..b87416c23d2 100644 --- a/spyder/widgets/tests/test_collectioneditor.py +++ b/spyder/widgets/tests/test_collectioneditor.py @@ -20,6 +20,7 @@ # Third party imports import numpy +from packaging.version import parse import pandas import pytest from flaky import flaky @@ -333,29 +334,66 @@ def test_shows_dataframeeditor_when_editing_index(monkeypatch): def test_sort_numpy_numeric_collectionsmodel(): + if parse(numpy.__version__) >= parse("2.0.0"): + np20 = True + else: + np20 = False + var_list = [ - numpy.float64(1e16), numpy.float64(10), numpy.float64(1), - numpy.float64(0.1), numpy.float64(1e-6), - numpy.float64(0), numpy.float64(-1e-6), numpy.float64(-1), - numpy.float64(-10), numpy.float64(-1e16) - ] + numpy.float64(1e16), + numpy.float64(10), + numpy.float64(1), + numpy.float64(0.1), + numpy.float64(1e-6), + numpy.float64(0), + numpy.float64(-1e-6), + numpy.float64(-1), + numpy.float64(-10), + numpy.float64(-1e16), + ] cm = CollectionsModel(MockParent(), var_list) assert cm.rowCount() == 10 assert cm.columnCount() == 4 - cm.sort(0) # sort by index - assert data_table(cm, 10, 4) == [list(range(0, 10)), - [u'float64']*10, - [1]*10, - ['1e+16', '10.0', '1.0', '0.1', - '1e-06', '0.0', '-1e-06', - '-1.0', '-10.0', '-1e+16']] - cm.sort(3) # sort by value - assert data_table(cm, 10, 4) == [list(range(9, -1, -1)), - [u'float64']*10, - [1]*10, - ['-1e+16', '-10.0', '-1.0', - '-1e-06', '0.0', '1e-06', - '0.1', '1.0', '10.0', '1e+16']] + + # Sort by index + cm.sort(0) + assert data_table(cm, 10, 4) == [ + list(range(0, 10)), + ["float64"] * 10, + [1] * 10, + [ + "np.float64(1e+16)" if np20 else "1e+16", + "np.float64(10.0)" if np20 else "10.0", + "np.float64(1.0)" if np20 else "1.0", + "np.float64(0.1)" if np20 else "0.1", + "np.float64(1e-06)" if np20 else "1e-06", + "np.float64(0.0)" if np20 else "0.0", + "np.float64(-1e-06)" if np20 else "-1e-06", + "np.float64(-1.0)" if np20 else "-1.0", + "np.float64(-10.0)" if np20 else "-10.0", + "np.float64(-1e+16)" if np20 else "-1e+16", + ], + ] + + # Sort by value + cm.sort(3) + assert data_table(cm, 10, 4) == [ + list(range(9, -1, -1)), + ["float64"] * 10, + [1] * 10, + [ + "np.float64(-1e+16)" if np20 else "-1e+16", + "np.float64(-10.0)" if np20 else "-10.0", + "np.float64(-1.0)" if np20 else "-1.0", + "np.float64(-1e-06)" if np20 else "-1e-06", + "np.float64(0.0)" if np20 else "0.0", + "np.float64(1e-06)" if np20 else "1e-06", + "np.float64(0.1)" if np20 else "0.1", + "np.float64(1.0)" if np20 else "1.0", + "np.float64(10.0)" if np20 else "10.0", + "np.float64(1e+16)" if np20 else "1e+16", + ], + ] def test_sort_float_collectionsmodel():