Skip to content

Commit 9ffcb45

Browse files
committed
MNT: Deprecate cbook.maxdict
It was used in one place in the library and there is now a standard library lru_cache that we can take advantage of instead.
1 parent 075067c commit 9ffcb45

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``matplotlib.cbook.maxdict`` is deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This class has been deprecated in favor of the standard library
4+
``functools.lru_cache``.

lib/matplotlib/cbook/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ def flatten(seq, scalarp=is_scalar_or_string):
540540
yield from flatten(item, scalarp)
541541

542542

543+
@_api.deprecated("3.6", alternative="functools.lru_cache")
543544
class maxdict(dict):
544545
"""
545546
A dictionary with a maximum size.

lib/matplotlib/text.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Classes for including text in a figure.
33
"""
44

5+
import functools
56
import logging
67
import math
78
import numbers
@@ -108,7 +109,6 @@ class Text(Artist):
108109
"""Handle storing and drawing of text in window or data coordinates."""
109110

110111
zorder = 3
111-
_cached = cbook.maxdict(50)
112112

113113
def __repr__(self):
114114
return "Text(%s, %s, %s)" % (self._x, self._y, repr(self._text))
@@ -294,9 +294,13 @@ def _get_layout(self, renderer):
294294
of a rotated text when necessary.
295295
"""
296296
key = self._get_layout_cache_key(renderer=renderer)
297-
if key in self._cached:
298-
return self._cached[key]
297+
return self._get_layout_cache(key, renderer)
299298

299+
@functools.lru_cache
300+
def _get_layout_cache(self, key, renderer):
301+
"""
302+
Return the layout using a lru_cache decorator
303+
"""
300304
thisx, thisy = 0.0, 0.0
301305
lines = self.get_text().split("\n") # Ensures lines is not empty.
302306

@@ -440,7 +444,6 @@ def _get_layout(self, renderer):
440444
xys = M.transform(offset_layout) - (offsetx, offsety)
441445

442446
ret = bbox, list(zip(lines, zip(ws, hs), *xys.T)), descent
443-
self._cached[key] = ret
444447
return ret
445448

446449
def set_bbox(self, rectprops):

0 commit comments

Comments
 (0)