Skip to content

Commit ee229ea

Browse files
committed
Deprecate passing incorrect types to Axes.add_*.
1 parent 2d1b1e1 commit ee229ea

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/matplotlib/axes/_base.py

+21
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,20 @@ def has_data(self):
20272027
mlines.Line2D, mpatches.Patch))
20282028
for a in self._children)
20292029

2030+
def _deprecate_noninstance(self, _name, _types, **kwargs):
2031+
"""
2032+
For each *key, value* pair in *kwargs*, check that *value* is an
2033+
instance of one of *_types*; if not, raise an appropriate deprecation.
2034+
"""
2035+
for key, value in kwargs.items():
2036+
if not isinstance(value, _types):
2037+
cbook.warn_deprecated(
2038+
'3.4', name=_name,
2039+
message=f'Passing argument *{key}* of unexpected type '
2040+
f'{type(value).__qualname__} to %(name)s which only '
2041+
f'accepts {_types} is deprecated since %(since)s and will '
2042+
'become an error %(removal)s.')
2043+
20302044
def add_artist(self, a):
20312045
"""
20322046
Add an `~.Artist` to the Axes; return the artist.
@@ -2070,6 +2084,8 @@ def add_collection(self, collection, autolim=True):
20702084
"""
20712085
Add a `~.Collection` to the Axes; return the collection.
20722086
"""
2087+
self._deprecate_noninstance('add_collection', mcoll.Collection,
2088+
collection=collection)
20732089
label = collection.get_label()
20742090
if not label:
20752091
collection.set_label(f'_collection{len(self._children)}')
@@ -2093,6 +2109,7 @@ def add_image(self, image):
20932109
"""
20942110
Add an `~.AxesImage` to the Axes; return the image.
20952111
"""
2112+
self._deprecate_noninstance('add_image', mimage.AxesImage, image=image)
20962113
self._set_artist_props(image)
20972114
if not image.get_label():
20982115
image.set_label(f'_image{len(self._children)}')
@@ -2109,6 +2126,7 @@ def add_line(self, line):
21092126
"""
21102127
Add a `.Line2D` to the Axes; return the line.
21112128
"""
2129+
self._deprecate_noninstance('add_line', mlines.Line2D, line=line)
21122130
self._set_artist_props(line)
21132131
if line.get_clip_path() is None:
21142132
line.set_clip_path(self.patch)
@@ -2125,6 +2143,7 @@ def _add_text(self, txt):
21252143
"""
21262144
Add a `~.Text` to the Axes; return the text.
21272145
"""
2146+
self._deprecate_noninstance('_add_text', mtext.Text, txt=txt)
21282147
self._set_artist_props(txt)
21292148
self._children.append(txt)
21302149
txt._remove_method = self._children.remove
@@ -2179,6 +2198,7 @@ def add_patch(self, p):
21792198
"""
21802199
Add a `~.Patch` to the Axes; return the patch.
21812200
"""
2201+
self._deprecate_noninstance('add_patch', mpatches.Patch, p=p)
21822202
self._set_artist_props(p)
21832203
if p.get_clip_path() is None:
21842204
p.set_clip_path(self.patch)
@@ -2217,6 +2237,7 @@ def add_table(self, tab):
22172237
"""
22182238
Add a `~.Table` to the Axes; return the table.
22192239
"""
2240+
self._deprecate_noninstance('add_table', mtable.Table, tab=tab)
22202241
self._set_artist_props(tab)
22212242
self._children.append(tab)
22222243
tab.set_clip_path(self.patch)

0 commit comments

Comments
 (0)