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

Skip to content

Commit 4aa99c3

Browse files
committed
Deprecate passing incorrect types to Axes.add_*.
1 parent 0135146 commit 4aa99c3

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
@@ -2150,6 +2150,20 @@ def has_data(self):
21502150
mlines.Line2D, mpatches.Patch))
21512151
for a in self._children)
21522152

2153+
def _deprecate_noninstance(self, _name, _types, **kwargs):
2154+
"""
2155+
For each *key, value* pair in *kwargs*, check that *value* is an
2156+
instance of one of *_types*; if not, raise an appropriate deprecation.
2157+
"""
2158+
for key, value in kwargs.items():
2159+
if not isinstance(value, _types):
2160+
_api.warn_deprecated(
2161+
'3.5', name=_name,
2162+
message=f'Passing argument *{key}* of unexpected type '
2163+
f'{type(value).__qualname__} to %(name)s which only '
2164+
f'accepts {_types} is deprecated since %(since)s and will '
2165+
'become an error %(removal)s.')
2166+
21532167
def add_artist(self, a):
21542168
"""
21552169
Add an `~.Artist` to the Axes; return the artist.
@@ -2193,6 +2207,8 @@ def add_collection(self, collection, autolim=True):
21932207
"""
21942208
Add a `~.Collection` to the Axes; return the collection.
21952209
"""
2210+
self._deprecate_noninstance('add_collection', mcoll.Collection,
2211+
collection=collection)
21962212
label = collection.get_label()
21972213
if not label:
21982214
collection.set_label(f'_child{len(self._children)}')
@@ -2225,6 +2241,7 @@ def add_image(self, image):
22252241
"""
22262242
Add an `~.AxesImage` to the Axes; return the image.
22272243
"""
2244+
self._deprecate_noninstance('add_image', mimage.AxesImage, image=image)
22282245
self._set_artist_props(image)
22292246
if not image.get_label():
22302247
image.set_label(f'_child{len(self._children)}')
@@ -2241,6 +2258,7 @@ def add_line(self, line):
22412258
"""
22422259
Add a `.Line2D` to the Axes; return the line.
22432260
"""
2261+
self._deprecate_noninstance('add_line', mlines.Line2D, line=line)
22442262
self._set_artist_props(line)
22452263
if line.get_clip_path() is None:
22462264
line.set_clip_path(self.patch)
@@ -2257,6 +2275,7 @@ def _add_text(self, txt):
22572275
"""
22582276
Add a `~.Text` to the Axes; return the text.
22592277
"""
2278+
self._deprecate_noninstance('_add_text', mtext.Text, txt=txt)
22602279
self._set_artist_props(txt)
22612280
self._children.append(txt)
22622281
txt._remove_method = self._children.remove
@@ -2311,6 +2330,7 @@ def add_patch(self, p):
23112330
"""
23122331
Add a `~.Patch` to the Axes; return the patch.
23132332
"""
2333+
self._deprecate_noninstance('add_patch', mpatches.Patch, p=p)
23142334
self._set_artist_props(p)
23152335
if p.get_clip_path() is None:
23162336
p.set_clip_path(self.patch)
@@ -2351,6 +2371,7 @@ def add_table(self, tab):
23512371
"""
23522372
Add a `~.Table` to the Axes; return the table.
23532373
"""
2374+
self._deprecate_noninstance('add_table', mtable.Table, tab=tab)
23542375
self._set_artist_props(tab)
23552376
self._children.append(tab)
23562377
tab.set_clip_path(self.patch)

0 commit comments

Comments
 (0)