@@ -104,18 +104,30 @@ def test(self):
104
104
def areinstance (x , y , tuple_of_classes ):
105
105
return isinstance (x , tuple_of_classes ) and isinstance (y , tuple_of_classes )
106
106
107
+ def is_primitive (x ):
108
+ return isinstance (x , (str , int , float , bool , type (None )))
109
+
110
+ def is_collection_of_primitives (x ):
111
+ return isinstance (x , (list , tuple , set )) and all (is_primitive (element ) for element in x )
107
112
108
113
# For equality of ndarrays, list, dicts, pd Series and pd DataFrames:
109
114
# First try to the faster equality functions. If these don't pass,
110
115
# Run the assertions that are typically slower.
111
116
def is_equal (x , y ):
112
- import pandas as pd
113
- from pandas .testing import assert_frame_equal , assert_series_equal
114
- import numpy as np
115
117
try :
118
+ if areinstance (x , y , (str , int , float , bool , type (None ))):
119
+ return x == y
120
+ if is_collection_of_primitives (x ):
121
+ return x == y
116
122
if areinstance (x , y , (Exception ,)):
117
123
# Types of errors don't matter (this is debatable)
118
124
return str (x ) == str (y )
125
+
126
+ # Delay importing pandas / numpy until absolutely necessary. This is important for performance in Pyodide.
127
+ import pandas as pd
128
+ from pandas .testing import assert_frame_equal , assert_series_equal
129
+ import numpy as np
130
+
119
131
if areinstance (x , y , (np .ndarray , dict , list , tuple )):
120
132
np .testing .assert_equal (x , y )
121
133
return True
0 commit comments