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

Skip to content

Commit a8f3955

Browse files
committed
Deprecate passing incorrect types to Axes.add_*.
1 parent f280c4e commit a8f3955

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,20 @@ def has_data(self):
20862086
mlines.Line2D, mpatches.Patch))
20872087
for a in self._children)
20882088

2089+
def _deprecate_noninstance(self, _name, _types, **kwargs):
2090+
"""
2091+
For each *key, value* pair in *kwargs*, check that *value* is an
2092+
instance of one of *_types*; if not, raise an appropriate deprecation.
2093+
"""
2094+
for key, value in kwargs.items():
2095+
if not isinstance(value, _types):
2096+
cbook.warn_deprecated(
2097+
'3.4', name=_name,
2098+
message=f'Passing argument *{key}* of unexpected type '
2099+
f'{type(value).__qualname__} to %(name)s which only '
2100+
f'accepts {_types} is deprecated since %(since)s and will '
2101+
'become an error %(removal)s.')
2102+
20892103
def add_artist(self, a):
20902104
"""
20912105
Add an `~.Artist` to the Axes; return the artist.
@@ -2129,6 +2143,8 @@ def add_collection(self, collection, autolim=True):
21292143
"""
21302144
Add a `~.Collection` to the Axes; return the collection.
21312145
"""
2146+
self._deprecate_noninstance('add_collection', mcoll.Collection,
2147+
collection=collection)
21322148
label = collection.get_label()
21332149
if not label:
21342150
collection.set_label(f'_child{len(self._children)}')
@@ -2152,6 +2168,7 @@ def add_image(self, image):
21522168
"""
21532169
Add an `~.AxesImage` to the Axes; return the image.
21542170
"""
2171+
self._deprecate_noninstance('add_image', mimage.AxesImage, image=image)
21552172
self._set_artist_props(image)
21562173
if not image.get_label():
21572174
image.set_label(f'_child{len(self._children)}')
@@ -2168,6 +2185,7 @@ def add_line(self, line):
21682185
"""
21692186
Add a `.Line2D` to the Axes; return the line.
21702187
"""
2188+
self._deprecate_noninstance('add_line', mlines.Line2D, line=line)
21712189
self._set_artist_props(line)
21722190
if line.get_clip_path() is None:
21732191
line.set_clip_path(self.patch)
@@ -2184,6 +2202,7 @@ def _add_text(self, txt):
21842202
"""
21852203
Add a `~.Text` to the Axes; return the text.
21862204
"""
2205+
self._deprecate_noninstance('_add_text', mtext.Text, txt=txt)
21872206
self._set_artist_props(txt)
21882207
self._children.append(txt)
21892208
txt._remove_method = self._children.remove
@@ -2238,6 +2257,7 @@ def add_patch(self, p):
22382257
"""
22392258
Add a `~.Patch` to the Axes; return the patch.
22402259
"""
2260+
self._deprecate_noninstance('add_patch', mpatches.Patch, p=p)
22412261
self._set_artist_props(p)
22422262
if p.get_clip_path() is None:
22432263
p.set_clip_path(self.patch)
@@ -2276,6 +2296,7 @@ def add_table(self, tab):
22762296
"""
22772297
Add a `~.Table` to the Axes; return the table.
22782298
"""
2299+
self._deprecate_noninstance('add_table', mtable.Table, tab=tab)
22792300
self._set_artist_props(tab)
22802301
self._children.append(tab)
22812302
tab.set_clip_path(self.patch)

0 commit comments

Comments
 (0)