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

Skip to content

Commit 9fa9efb

Browse files
authored
Merge pull request #18124 from QuLogic/fix-test_draw_frame
FIX: FuncAnimation._draw_frame exception and testing
2 parents 8a78bf2 + 8a3f1dd commit 9fa9efb

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

lib/matplotlib/animation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,13 +1723,13 @@ def _draw_frame(self, framedata):
17231723

17241724
if self._blit:
17251725

1726-
err = RuntimeError('The animation function must return a '
1727-
'sequence of Artist objects.')
1726+
err = RuntimeError('The animation function must return a sequence '
1727+
'of Artist objects.')
17281728
try:
17291729
# check if a sequence
17301730
iter(self._drawn_artists)
17311731
except TypeError:
1732-
raise err
1732+
raise err from None
17331733

17341734
# check each item if is artist
17351735
for i in self._drawn_artists:

lib/matplotlib/tests/test_animation.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import matplotlib as mpl
1111
from matplotlib import pyplot as plt
1212
from matplotlib import animation
13-
from matplotlib import artist
1413

1514

1615
class NullMovieWriter(animation.AbstractMovieWriter):
@@ -275,34 +274,32 @@ def frames_generator():
275274
assert (f() is None) != cache_frame_data
276275

277276

278-
def test_draw_frame():
277+
@pytest.mark.parametrize('return_value', [
278+
# User forgot to return (returns None).
279+
None,
280+
# User returned a string.
281+
'string',
282+
# User returned an int.
283+
1,
284+
# User returns a sequence of other objects, e.g., string instead of Artist.
285+
('string', ),
286+
# User forgot to return a sequence (handled in `animate` below.)
287+
'artist',
288+
])
289+
def test_draw_frame(return_value):
279290
# test _draw_frame method
280291

281292
fig, ax = plt.subplots()
282293
line, = ax.plot([])
283294

284-
def animate(i, arg):
295+
def animate(i):
285296
# general update func
286297
line.set_data([0, 1], [0, i])
287-
if arg:
288-
return arg
298+
if return_value == 'artist':
299+
# *not* a sequence
300+
return line
301+
else:
302+
return return_value
289303

290304
with pytest.raises(RuntimeError):
291-
292-
# user forgot to return (returns None)
293-
animation.FuncAnimation(fig, animate, blit=True, fargs=(None,))
294-
295-
# user (for some reason) returned a string...AttributeError is raised
296-
animation.FuncAnimation(fig, animate, blit=True, fargs=('string', ))
297-
298-
# user (for some reason) returned a string...AttributeError is raised
299-
animation.FuncAnimation(fig, animate, blit=True, fargs=(1, ))
300-
301-
# user returns a sequence of other objects
302-
# e.g. a string instead of Artist
303-
animation.FuncAnimation(fig, animate, blit=True, fargs=(('string',), ))
304-
305-
# user forgot to put comma or return a sequence
306-
# TypeError will be raised (same with returning a number or bool)
307-
artist_obj = artist.Artist()
308-
animation.FuncAnimation(fig, animate, blit=True, fargs=(artist_obj, ))
305+
animation.FuncAnimation(fig, animate, blit=True)

0 commit comments

Comments
 (0)