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

Skip to content

Commit 6df1210

Browse files
committed
MNT: make sure we do not mutate input in Text.update
We take in a dictionary (called kwargs rather than collecting kwargs) which we mutate in the method. We pop bbox and fontproperties keys out of the user supplied dictionary (to make sure the precedence and default behaviors are correct) which is propagated back out to user code. This makes an internal copy of the dictionary before we mutate it. closes #18838
1 parent 74d6145 commit 6df1210

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import matplotlib.pyplot as plt
1313
import matplotlib.transforms as mtransforms
1414
from matplotlib.testing.decorators import check_figures_equal, image_comparison
15-
15+
from matplotlib.font_manager import FontProperties
16+
from matplotlib.text import Text
1617

1718
needs_usetex = pytest.mark.skipif(
1819
not mpl.checkdep_usetex(True),
@@ -697,3 +698,13 @@ def test_transform_rotates_text():
697698
transform_rotates_text=True)
698699
result = text.get_rotation()
699700
assert_almost_equal(result, 30)
701+
702+
703+
def test_update_mutate_input():
704+
inp = dict(fontproperties=FontProperties(weight="bold"),
705+
bbox=None)
706+
cache = dict(inp)
707+
t = Text()
708+
t.update(inp)
709+
assert inp['fontproperties'] == cache['fontproperties']
710+
assert inp['bbox'] == cache['bbox']

lib/matplotlib/text.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def __init__(self,
171171
self.update(kwargs)
172172

173173
def update(self, kwargs):
174+
# make a copy so we do not mutate user input!
175+
kwargs = dict(kwargs)
174176
# docstring inherited
175177
sentinel = object() # bbox can be None, so use another sentinel.
176178
# Update fontproperties first, as it has lowest priority.

0 commit comments

Comments
 (0)