@@ -104,18 +104,30 @@ def test(self):
104104def areinstance (x , y , tuple_of_classes ):
105105 return isinstance (x , tuple_of_classes ) and isinstance (y , tuple_of_classes )
106106
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 )
107112
108113# For equality of ndarrays, list, dicts, pd Series and pd DataFrames:
109114# First try to the faster equality functions. If these don't pass,
110115# Run the assertions that are typically slower.
111116def 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
115117 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
116122 if areinstance (x , y , (Exception ,)):
117123 # Types of errors don't matter (this is debatable)
118124 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+
119131 if areinstance (x , y , (np .ndarray , dict , list , tuple )):
120132 np .testing .assert_equal (x , y )
121133 return True
0 commit comments