@@ -228,6 +228,38 @@ def test_copy(comprehensive, render, deepcopy, mi_styler, mi_styler_comp):
228
228
assert id (getattr (s2 , attr )) != id (getattr (styler , attr ))
229
229
230
230
231
+ def test_clear (mi_styler_comp ):
232
+ # NOTE: if this test fails for new features then 'mi_styler_comp' should be updated
233
+ # to ensure proper testing of the 'copy', 'clear', 'export' methods with new feature
234
+ # GH 40675
235
+ styler = mi_styler_comp
236
+ styler .to_html () # new attrs maybe created on render
237
+
238
+ clean_copy = Styler (styler .data , uuid = styler .uuid )
239
+
240
+ excl = [
241
+ "data" ,
242
+ "index" ,
243
+ "columns" ,
244
+ "uuid" ,
245
+ "uuid_len" ,
246
+ "cell_ids" ,
247
+ "cellstyle_map" , # execution time only
248
+ "precision" , # deprecated
249
+ "na_rep" , # deprecated
250
+ ]
251
+ # tests vars are not same vals on obj and clean copy before clear (except for excl)
252
+ for attr in [a for a in styler .__dict__ if not (callable (a ) or a in excl )]:
253
+ res = getattr (styler , attr ) == getattr (clean_copy , attr )
254
+ assert not (all (res ) if (hasattr (res , "__iter__" ) and len (res ) > 0 ) else res )
255
+
256
+ # test vars have same vales on obj and clean copy after clearing
257
+ styler .clear ()
258
+ for attr in [a for a in styler .__dict__ if not (callable (a ))]:
259
+ res = getattr (styler , attr ) == getattr (clean_copy , attr )
260
+ assert all (res ) if hasattr (res , "__iter__" ) else res
261
+
262
+
231
263
class TestStyler :
232
264
def setup_method (self , method ):
233
265
np .random .seed (24 )
@@ -283,33 +315,6 @@ def test_update_ctx_flatten_multi_and_trailing_semi(self):
283
315
}
284
316
assert self .styler .ctx == expected
285
317
286
- def test_clear (self ):
287
- # updated in GH 39396
288
- tt = DataFrame ({"A" : [None , "tt" ]})
289
- css = DataFrame ({"A" : [None , "cls-a" ]})
290
- s = self .df .style .highlight_max ().set_tooltips (tt ).set_td_classes (css )
291
- s = s .hide_index ().hide_columns ("A" )
292
- # _todo, tooltips and cell_context items added to..
293
- assert len (s ._todo ) > 0
294
- assert s .tooltips
295
- assert len (s .cell_context ) > 0
296
- assert s .hide_index_ is True
297
- assert len (s .hidden_columns ) > 0
298
-
299
- s = s ._compute ()
300
- # ctx item affected when a render takes place. _todo is maintained
301
- assert len (s .ctx ) > 0
302
- assert len (s ._todo ) > 0
303
-
304
- s .clear ()
305
- # ctx, _todo, tooltips and cell_context items all revert to null state.
306
- assert len (s .ctx ) == 0
307
- assert len (s ._todo ) == 0
308
- assert not s .tooltips
309
- assert len (s .cell_context ) == 0
310
- assert s .hide_index_ is False
311
- assert len (s .hidden_columns ) == 0
312
-
313
318
def test_render (self ):
314
319
df = DataFrame ({"A" : [0 , 1 ]})
315
320
style = lambda x : pd .Series (["color: red" , "color: blue" ], name = x .name )
0 commit comments