Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92d1577

Browse files
committed
Deprecate Text.get_prop_tup.
get_prop_tup was intended as a general caching mechanism for reusing Text layouts, but it ended up only being used by _get_layout (which backends have to call anyways to handle multiline text). Note that in fact, if we really wanted to make backends use that info for caching, whether e.g. the text color needs to be taken into account would likely depend on the backend's own caching mechanism. Replace it by a private `_get_layout_cache_key`, which does not take color into account (color doesn't affect layout), which will later allow removing a color-must-be-hashable check.
1 parent a738d78 commit 92d1577

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``Text.get_prop_tup``
2+
~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated with no replacements (because the `.Text` class cannot know
4+
whether a backend needs to update cache e.g. when the text's color changes).

lib/matplotlib/text.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,28 @@ def update_from(self, other):
284284
self._linespacing = other._linespacing
285285
self.stale = True
286286

287+
def _get_layout_cache_key(self, renderer=None):
288+
"""
289+
Return a hashable tuple of properties that lets `_get_layout` know
290+
whether a previously computed layout can be reused.
291+
"""
292+
x, y = self.get_unitless_position()
293+
renderer = renderer or self._renderer
294+
return (
295+
x, y, self.get_text(), hash(self._fontproperties),
296+
self._verticalalignment, self._horizontalalignment,
297+
self._linespacing,
298+
self._rotation, self._rotation_mode, self._transform_rotates_text,
299+
self.figure.dpi, weakref.ref(renderer),
300+
)
301+
287302
def _get_layout(self, renderer):
288303
"""
289304
Return the extent (bbox) of the text together with
290305
multiple-alignment information. Note that it returns an extent
291306
of a rotated text when necessary.
292307
"""
293-
key = self.get_prop_tup(renderer=renderer)
308+
key = self._get_layout_cache_key(renderer=renderer)
294309
if key in self._cached:
295310
return self._cached[key]
296311

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

849+
# When removing, also remove the hash(color) check in set_color()
850+
@_api.deprecated("3.5")
834851
def get_prop_tup(self, renderer=None):
835852
"""
836853
Return a hashable tuple of properties.
@@ -938,7 +955,8 @@ def set_color(self, color):
938955
# out at draw time for simplicity.
939956
if not cbook._str_equal(color, "auto"):
940957
mpl.colors._check_color_like(color=color)
941-
# Make sure it is hashable, or get_prop_tup will fail.
958+
# Make sure it is hashable, or get_prop_tup will fail (remove this once
959+
# get_prop_tup is removed).
942960
try:
943961
hash(color)
944962
except TypeError:

0 commit comments

Comments
 (0)