Skip to content

Commit

Permalink
Add deprecation for setting data with non sequence type in Line2D -…
Browse files Browse the repository at this point in the history
… see #22329
  • Loading branch information
ericpre committed Feb 11, 2023
1 parent fc775b1 commit c8a44ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 16 additions & 2 deletions lib/matplotlib/lines.py
Expand Up @@ -1275,7 +1275,14 @@ def set_xdata(self, x):
x : 1D array
"""
if not np.iterable(x):
raise RuntimeError('x must be a sequence')
# When deprecation cycle is completed
# raise RuntimeError('x must be a sequence')
_api.warn_deprecated(
since=3.7,
message="Setting data with a non sequence type "
"is deprecated since %(since)s and will be "
"remove %(removal)s")
x = [x, ]
self._xorig = copy.copy(x)
self._invalidx = True
self.stale = True
Expand All @@ -1289,7 +1296,14 @@ def set_ydata(self, y):
y : 1D array
"""
if not np.iterable(y):
raise RuntimeError('y must be a sequence')
# When deprecation cycle is completed
# raise RuntimeError('y must be a sequence')
_api.warn_deprecated(
since=3.7,
message="Setting data with a non sequence type "
"is deprecated since %(since)s and will be "
"remove %(removal)s")
y = [y, ]
self._yorig = copy.copy(y)
self._invalidy = True
self.stale = True
Expand Down
8 changes: 6 additions & 2 deletions lib/matplotlib/tests/test_lines.py
Expand Up @@ -20,6 +20,7 @@
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib._api.deprecation import MatplotlibDeprecationWarning


def test_segment_hits():
Expand Down Expand Up @@ -91,9 +92,12 @@ def test_invalid_line_data():
mlines.Line2D([], 1)

line = mlines.Line2D([], [])
with pytest.raises(RuntimeError, match='x must be'):
# when deprecation cycle is completed
# with pytest.raises(RuntimeError, match='x must be'):
with pytest.warns(MatplotlibDeprecationWarning):
line.set_xdata(0)
with pytest.raises(RuntimeError, match='y must be'):
# with pytest.raises(RuntimeError, match='y must be'):
with pytest.warns(MatplotlibDeprecationWarning):
line.set_ydata(0)


Expand Down

0 comments on commit c8a44ac

Please sign in to comment.