Skip to content

MNT: Deprecate cbook.maxdict #22299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/22299-GL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``matplotlib.cbook.maxdict`` is deprecated
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This class has been deprecated in favor of the standard library
``functools.lru_cache``.
1 change: 1 addition & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ def flatten(seq, scalarp=is_scalar_or_string):
yield from flatten(item, scalarp)


@_api.deprecated("3.6", alternative="functools.lru_cache")
class maxdict(dict):
"""
A dictionary with a maximum size.
Expand Down
11 changes: 7 additions & 4 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Classes for including text in a figure.
"""

import functools
import logging
import math
import numbers
Expand Down Expand Up @@ -108,7 +109,6 @@ class Text(Artist):
"""Handle storing and drawing of text in window or data coordinates."""

zorder = 3
_cached = cbook.maxdict(50)

def __repr__(self):
return "Text(%s, %s, %s)" % (self._x, self._y, repr(self._text))
Expand Down Expand Up @@ -294,9 +294,13 @@ def _get_layout(self, renderer):
of a rotated text when necessary.
"""
key = self._get_layout_cache_key(renderer=renderer)
if key in self._cached:
return self._cached[key]
return self._get_layout_cache(key, renderer)

@functools.lru_cache(maxsize=50)
def _get_layout_cache(self, key, renderer):
Comment on lines +299 to +300
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is broken from what @anntzer mentioned, because it's caching on self.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐑 Doh! Should have read that closer. I'll move to draft for now and wait until #22271 goes in to have another look at this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI #22271 is in.

"""
Return the layout using a lru_cache decorator
"""
thisx, thisy = 0.0, 0.0
lines = self.get_text().split("\n") # Ensures lines is not empty.

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

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

def set_bbox(self, rectprops):
Expand Down