Skip to content

Commit 9196f8d

Browse files
Backport PR STYLE enable pylint: method-cache-max-size-none (#49784)
Co-authored-by: Natalia Mokeeva <[email protected]>
1 parent 8c4b559 commit 9196f8d

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

doc/source/whatsnew/v1.5.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Fixed regressions
2828
Bug fixes
2929
~~~~~~~~~
3030
- Bug in the Copy-on-Write implementation losing track of views in certain chained indexing cases (:issue:`48996`)
31-
-
31+
- Fixed memory leak in :meth:`.Styler.to_excel` (:issue:`49751`)
3232

3333
.. ---------------------------------------------------------------------------
3434
.. _whatsnew_152.other:

pandas/io/formats/excel.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ def __init__(self, inherited: str | None = None) -> None:
170170
self.inherited = self.compute_css(inherited)
171171
else:
172172
self.inherited = None
173+
# We should avoid lru_cache on the __call__ method.
174+
# Otherwise once the method __call__ has been called
175+
# garbage collection no longer deletes the instance.
176+
self._call_cached = lru_cache(maxsize=None)(self._call_uncached)
173177

174178
compute_css = CSSResolver()
175179

176-
@lru_cache(maxsize=None)
177180
def __call__(
178181
self, declarations: str | frozenset[tuple[str, str]]
179182
) -> dict[str, dict[str, str]]:
@@ -193,6 +196,11 @@ def __call__(
193196
A style as interpreted by ExcelWriter when found in
194197
ExcelCell.style.
195198
"""
199+
return self._call_cached(declarations)
200+
201+
def _call_uncached(
202+
self, declarations: str | frozenset[tuple[str, str]]
203+
) -> dict[str, dict[str, str]]:
196204
properties = self.compute_css(declarations, self.inherited)
197205
return self.build_xlstyle(properties)
198206

pandas/tests/io/formats/test_to_excel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def test_css_excel_cell_precedence(styles, expected):
357357
"""It applies favors latter declarations over former declarations"""
358358
# See GH 47371
359359
converter = CSSToExcelConverter()
360-
converter.__call__.cache_clear()
360+
converter._call_cached.cache_clear()
361361
css_styles = {(0, 0): styles}
362362
cell = CssExcelCell(
363363
row=0,
@@ -369,7 +369,7 @@ def test_css_excel_cell_precedence(styles, expected):
369369
css_col=0,
370370
css_converter=converter,
371371
)
372-
converter.__call__.cache_clear()
372+
converter._call_cached.cache_clear()
373373

374374
assert cell.style == converter(expected)
375375

@@ -410,7 +410,7 @@ def test_css_excel_cell_cache(styles, cache_hits, cache_misses):
410410
"""It caches unique cell styles"""
411411
# See GH 47371
412412
converter = CSSToExcelConverter()
413-
converter.__call__.cache_clear()
413+
converter._call_cached.cache_clear()
414414

415415
css_styles = {(0, i): _style for i, _style in enumerate(styles)}
416416
for css_row, css_col in css_styles:
@@ -424,8 +424,8 @@ def test_css_excel_cell_cache(styles, cache_hits, cache_misses):
424424
css_col=css_col,
425425
css_converter=converter,
426426
)
427-
cache_info = converter.__call__.cache_info()
428-
converter.__call__.cache_clear()
427+
cache_info = converter._call_cached.cache_info()
428+
converter._call_cached.cache_clear()
429429

430430
assert cache_info.hits == cache_hits
431431
assert cache_info.misses == cache_misses

0 commit comments

Comments
 (0)