Skip to content

Deprecate Text.get_prop_tup. #19575

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

Merged
merged 1 commit into from
Apr 1, 2021
Merged
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/19575-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``Text.get_prop_tup``
~~~~~~~~~~~~~~~~~~~~~
... is deprecated with no replacements (because the `.Text` class cannot know
whether a backend needs to update cache e.g. when the text's color changes).
22 changes: 20 additions & 2 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,28 @@ def update_from(self, other):
self._linespacing = other._linespacing
self.stale = True

def _get_layout_cache_key(self, renderer=None):
"""
Return a hashable tuple of properties that lets `_get_layout` know
whether a previously computed layout can be reused.
"""
x, y = self.get_unitless_position()
renderer = renderer or self._renderer
return (
x, y, self.get_text(), hash(self._fontproperties),
self._verticalalignment, self._horizontalalignment,
self._linespacing,
self._rotation, self._rotation_mode, self._transform_rotates_text,
self.figure.dpi, weakref.ref(renderer),
)

def _get_layout(self, renderer):
"""
Return the extent (bbox) of the text together with
multiple-alignment information. Note that it returns an extent
of a rotated text when necessary.
"""
key = self.get_prop_tup(renderer=renderer)
key = self._get_layout_cache_key(renderer=renderer)
if key in self._cached:
return self._cached[key]

Expand Down Expand Up @@ -831,6 +846,8 @@ def get_position(self):
# specified with 'set_x' and 'set_y'.
return self._x, self._y

# When removing, also remove the hash(color) check in set_color()
@_api.deprecated("3.5")
def get_prop_tup(self, renderer=None):
"""
Return a hashable tuple of properties.
Expand Down Expand Up @@ -938,7 +955,8 @@ def set_color(self, color):
# out at draw time for simplicity.
if not cbook._str_equal(color, "auto"):
mpl.colors._check_color_like(color=color)
# Make sure it is hashable, or get_prop_tup will fail.
# Make sure it is hashable, or get_prop_tup will fail (remove this once
# get_prop_tup is removed).
try:
hash(color)
except TypeError:
Expand Down