From cc24bcfda3db5b71bd2edb90b5d71a737d82ccb5 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 27 May 2026 23:49:24 +0200 Subject: [PATCH] Fix invalid format string in set_xticklabels error message. "=" happens to be a valid format string for numeric values, but not for arbitrary objects, so an error (`TypeError: unsupported format string passed to .__format__`) would previously occur *while formatting the error message* for something like `set_xticklabels(object())`. (The actual case where I saw this happen is when I wrongly passed a Formatter to set_xticklabels.) Also remove a fairly pointless comment. --- lib/matplotlib/axis.py | 3 +-- lib/matplotlib/tests/test_axes.py | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 223eb5e7a34f..9a47eb1169ca 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2227,11 +2227,10 @@ def set_ticklabels(self, labels, *, minor=False, fontdict=None, **kwargs): labels = [t.get_text() if hasattr(t, 'get_text') else t for t in labels] except TypeError: - raise TypeError(f"{labels:=} must be a sequence") from None + raise TypeError(f"{labels=!r} must be a sequence") from None locator = (self.get_minor_locator() if minor else self.get_major_locator()) if not labels: - # eg labels=[]: formatter = mticker.NullFormatter() elif (isinstance(locator, mticker.FixedLocator) or (hasattr(locator, 'base') and diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 43babac20897..eedd803a3914 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7032,6 +7032,8 @@ def test_xticks_bad_args(): ax = plt.figure().add_subplot() with pytest.raises(TypeError, match='must be a sequence'): ax.set_xticks([2, 9], 3.1) + with pytest.raises(TypeError, match='must be a sequence'): + ax.set_xticks([2, 9], ax) with pytest.raises(ValueError, match='must be 1D'): plt.xticks(np.arange(4).reshape((-1, 1))) with pytest.raises(ValueError, match='must be 1D'):