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

Skip to content

Commit 073be45

Browse files
committed
Deprecate passing incorrect types to Axes.add_*.
1 parent 53e6f94 commit 073be45

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
@@ -2064,6 +2064,20 @@ def has_data(self):
20642064
mlines.Line2D, mpatches.Patch))
20652065
for a in self._children)
20662066

2067+
def _deprecate_noninstance(self, _name, _types, **kwargs):
2068+
"""
2069+
For each *key, value* pair in *kwargs*, check that *value* is an
2070+
instance of one of *_types*; if not, raise an appropriate deprecation.
2071+
"""
2072+
for key, value in kwargs.items():
2073+
if not isinstance(value, _types):
2074+
cbook.warn_deprecated(
2075+
'3.4', name=_name,
2076+
message=f'Passing argument *{key}* of unexpected type '
2077+
f'{type(value).__qualname__} to %(name)s which only '
2078+
f'accepts {_types} is deprecated since %(since)s and will '
2079+
'become an error %(removal)s.')
2080+
20672081
def add_artist(self, a):
20682082
"""
20692083
Add an `~.Artist` to the Axes; return the artist.
@@ -2107,6 +2121,8 @@ def add_collection(self, collection, autolim=True):
21072121
"""
21082122
Add a `~.Collection` to the Axes; return the collection.
21092123
"""
2124+
self._deprecate_noninstance('add_collection', mcoll.Collection,
2125+
collection=collection)
21102126
label = collection.get_label()
21112127
if not label:
21122128
collection.set_label(f'_child{len(self._children)}')
@@ -2130,6 +2146,7 @@ def add_image(self, image):
21302146
"""
21312147
Add an `~.AxesImage` to the Axes; return the image.
21322148
"""
2149+
self._deprecate_noninstance('add_image', mimage.AxesImage, image=image)
21332150
self._set_artist_props(image)
21342151
if not image.get_label():
21352152
image.set_label(f'_child{len(self._children)}')
@@ -2146,6 +2163,7 @@ def add_line(self, line):
21462163
"""
21472164
Add a `.Line2D` to the Axes; return the line.
21482165
"""
2166+
self._deprecate_noninstance('add_line', mlines.Line2D, line=line)
21492167
self._set_artist_props(line)
21502168
if line.get_clip_path() is None:
21512169
line.set_clip_path(self.patch)
@@ -2162,6 +2180,7 @@ def _add_text(self, txt):
21622180
"""
21632181
Add a `~.Text` to the Axes; return the text.
21642182
"""
2183+
self._deprecate_noninstance('_add_text', mtext.Text, txt=txt)
21652184
self._set_artist_props(txt)
21662185
self._children.append(txt)
21672186
txt._remove_method = self._children.remove
@@ -2216,6 +2235,7 @@ def add_patch(self, p):
22162235
"""
22172236
Add a `~.Patch` to the Axes; return the patch.
22182237
"""
2238+
self._deprecate_noninstance('add_patch', mpatches.Patch, p=p)
22192239
self._set_artist_props(p)
22202240
if p.get_clip_path() is None:
22212241
p.set_clip_path(self.patch)
@@ -2254,6 +2274,7 @@ def add_table(self, tab):
22542274
"""
22552275
Add a `~.Table` to the Axes; return the table.
22562276
"""
2277+
self._deprecate_noninstance('add_table', mtable.Table, tab=tab)
22572278
self._set_artist_props(tab)
22582279
self._children.append(tab)
22592280
tab.set_clip_path(self.patch)

0 commit comments

Comments
 (0)