Skip to content

MNT: make sure we do not mutate input in Text.update #18839

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
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
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

import matplotlib as mpl
from matplotlib.backend_bases import MouseEvent
from matplotlib.font_manager import FontProperties
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
from matplotlib.testing.decorators import check_figures_equal, image_comparison
from matplotlib.text import Text


needs_usetex = pytest.mark.skipif(
Expand Down Expand Up @@ -697,3 +699,13 @@ def test_transform_rotates_text():
transform_rotates_text=True)
result = text.get_rotation()
assert_almost_equal(result, 30)


def test_update_mutate_input():
inp = dict(fontproperties=FontProperties(weight="bold"),
bbox=None)
cache = dict(inp)
t = Text()
t.update(inp)
assert inp['fontproperties'] == cache['fontproperties']
assert inp['bbox'] == cache['bbox']
2 changes: 2 additions & 0 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def __init__(self,

def update(self, kwargs):
# docstring inherited
# make a copy so we do not mutate user input!
kwargs = dict(kwargs)
sentinel = object() # bbox can be None, so use another sentinel.
# Update fontproperties first, as it has lowest priority.
fontproperties = kwargs.pop("fontproperties", sentinel)
Expand Down