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

Skip to content

Commit d2ee3d5

Browse files
authored
Merge pull request #21741 from jakelishman/fix/do_3d_projection-deprecation
Reduce do_3d_projection deprecation warnings in external artists
2 parents f06543e + 53e124b commit d2ee3d5

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from collections import defaultdict
1414
import functools
15+
import inspect
1516
import itertools
1617
import math
1718
from numbers import Integral
@@ -412,24 +413,27 @@ def do_3d_projection(artist):
412413
Call `do_3d_projection` on an *artist*, and warn if passing
413414
*renderer*.
414415
415-
For our Artists, never pass *renderer*. For external Artists,
416-
in lieu of more complicated signature parsing, always pass
417-
*renderer* and raise a warning.
416+
Attempt to bind the empty signature first, so external Artists
417+
can avoid the deprecation warning if they support the new
418+
calling convention.
418419
"""
419-
420-
if artist.__module__ == 'mpl_toolkits.mplot3d.art3d':
421-
# Our 3D Artists have deprecated the renderer parameter, so
422-
# avoid passing it to them; call this directly once the
423-
# deprecation has expired.
420+
try:
421+
signature = inspect.signature(artist.do_3d_projection)
422+
signature.bind()
423+
# ValueError if `inspect.signature` cannot provide a signature
424+
# and TypeError if the binding fails or the object does not
425+
# appear to be callable - the next call will then re-raise.
426+
except (ValueError, TypeError):
427+
_api.warn_deprecated(
428+
"3.4",
429+
message="The 'renderer' parameter of "
430+
"do_3d_projection() was deprecated in Matplotlib "
431+
"%(since)s and will be removed %(removal)s.")
432+
return artist.do_3d_projection(renderer)
433+
else:
434+
# Call this directly once the deprecation period expires.
424435
return artist.do_3d_projection()
425436

426-
_api.warn_deprecated(
427-
"3.4",
428-
message="The 'renderer' parameter of "
429-
"do_3d_projection() was deprecated in Matplotlib "
430-
"%(since)s and will be removed %(removal)s.")
431-
return artist.do_3d_projection(renderer)
432-
433437
collections_and_patches = (
434438
artist for artist in self._children
435439
if isinstance(artist, (mcoll.Collection, mpatches.Patch))

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,3 +1711,63 @@ def test_view_init_vertical_axis(
17111711
tickdir_expected = tickdirs_expected[i]
17121712
tickdir_actual = axis._get_tickdir()
17131713
np.testing.assert_array_equal(tickdir_expected, tickdir_actual)
1714+
1715+
1716+
def test_do_3d_projection_renderer_deprecation_warn_on_argument():
1717+
"""
1718+
Test that an external artist with an old-style calling convention raises
1719+
a suitable deprecation warning.
1720+
"""
1721+
class DummyPatch(art3d.Patch3D):
1722+
def do_3d_projection(self, renderer):
1723+
return 0
1724+
1725+
def draw(self, renderer):
1726+
pass
1727+
1728+
fig = plt.figure()
1729+
ax = fig.add_subplot(111, projection='3d')
1730+
artist = DummyPatch()
1731+
ax.add_artist(artist)
1732+
1733+
match = r"The 'renderer' parameter of do_3d_projection\(\) was deprecated"
1734+
with pytest.warns(MatplotlibDeprecationWarning, match=match):
1735+
fig.canvas.draw()
1736+
1737+
1738+
def test_do_3d_projection_renderer_deprecation_nowarn_on_optional_argument():
1739+
"""
1740+
Test that an external artist with a calling convention compatible with
1741+
both v3.3 and v3.4 does not raise a deprecation warning.
1742+
"""
1743+
class DummyPatch(art3d.Patch3D):
1744+
def do_3d_projection(self, renderer=None):
1745+
return 0
1746+
1747+
def draw(self, renderer):
1748+
pass
1749+
1750+
fig = plt.figure()
1751+
ax = fig.add_subplot(111, projection='3d')
1752+
artist = DummyPatch()
1753+
ax.add_artist(artist)
1754+
fig.canvas.draw()
1755+
1756+
1757+
def test_do_3d_projection_renderer_deprecation_nowarn_on_no_argument():
1758+
"""
1759+
Test that an external artist with a calling convention compatible with
1760+
only v3.4 does not raise a deprecation warning.
1761+
"""
1762+
class DummyPatch(art3d.Patch3D):
1763+
def do_3d_projection(self):
1764+
return 0
1765+
1766+
def draw(self, renderer):
1767+
pass
1768+
1769+
fig = plt.figure()
1770+
ax = fig.add_subplot(111, projection='3d')
1771+
artist = DummyPatch()
1772+
ax.add_artist(artist)
1773+
fig.canvas.draw()

0 commit comments

Comments
 (0)