Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cd47a2c

Browse filesBrowse files
committed
Deprecate passing incorrect types to Axes.add_*.
1 parent 32adf18 commit cd47a2c
Copy full SHA for cd47a2c

File tree

Expand file treeCollapse file tree

1 file changed

+21
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-0
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,20 @@ def has_data(self):
20292029
mlines.Line2D, mpatches.Patch))
20302030
for a in self._children)
20312031

2032+
def _deprecate_noninstance(self, _name, _types, **kwargs):
2033+
"""
2034+
For each *key, value* pair in *kwargs*, check that *value* is an
2035+
instance of one of *_types*; if not, raise an appropriate deprecation.
2036+
"""
2037+
for key, value in kwargs.items():
2038+
if not isinstance(value, _types):
2039+
cbook.warn_deprecated(
2040+
'3.4', name=_name,
2041+
message=f'Passing argument *{key}* of unexpected type '
2042+
f'{type(value).__qualname__} to %(name)s which only '
2043+
f'accepts {_types} is deprecated since %(since)s and will '
2044+
'become an error %(removal)s.')
2045+
20322046
def add_artist(self, a):
20332047
"""
20342048
Add an `~.Artist` to the Axes; return the artist.
@@ -2072,6 +2086,8 @@ def add_collection(self, collection, autolim=True):
20722086
"""
20732087
Add a `~.Collection` to the Axes; return the collection.
20742088
"""
2089+
self._deprecate_noninstance('add_collection', mcoll.Collection,
2090+
collection=collection)
20752091
label = collection.get_label()
20762092
if not label:
20772093
collection.set_label(f'_collection{len(self._children)}')
@@ -2095,6 +2111,7 @@ def add_image(self, image):
20952111
"""
20962112
Add an `~.AxesImage` to the Axes; return the image.
20972113
"""
2114+
self._deprecate_noninstance('add_image', mimage.AxesImage, image=image)
20982115
self._set_artist_props(image)
20992116
if not image.get_label():
21002117
image.set_label(f'_image{len(self._children)}')
@@ -2111,6 +2128,7 @@ def add_line(self, line):
21112128
"""
21122129
Add a `.Line2D` to the Axes; return the line.
21132130
"""
2131+
self._deprecate_noninstance('add_line', mlines.Line2D, line=line)
21142132
self._set_artist_props(line)
21152133
if line.get_clip_path() is None:
21162134
line.set_clip_path(self.patch)
@@ -2127,6 +2145,7 @@ def _add_text(self, txt):
21272145
"""
21282146
Add a `~.Text` to the Axes; return the text.
21292147
"""
2148+
self._deprecate_noninstance('_add_text', mtext.Text, txt=txt)
21302149
self._set_artist_props(txt)
21312150
self._children.append(txt)
21322151
txt._remove_method = self._children.remove
@@ -2181,6 +2200,7 @@ def add_patch(self, p):
21812200
"""
21822201
Add a `~.Patch` to the Axes; return the patch.
21832202
"""
2203+
self._deprecate_noninstance('add_patch', mpatches.Patch, p=p)
21842204
self._set_artist_props(p)
21852205
if p.get_clip_path() is None:
21862206
p.set_clip_path(self.patch)
@@ -2219,6 +2239,7 @@ def add_table(self, tab):
22192239
"""
22202240
Add a `~.Table` to the Axes; return the table.
22212241
"""
2242+
self._deprecate_noninstance('add_table', mtable.Table, tab=tab)
22222243
self._set_artist_props(tab)
22232244
self._children.append(tab)
22242245
tab.set_clip_path(self.patch)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.