From db0ec14905b8e416e013ec740d811eb9a858ed60 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Tue, 29 Apr 2025 00:21:12 +0200 Subject: [PATCH] FIX: Typing of FuncAnimation `func` and `init_func` may return None (which is ok if `blit=False`). Since gating the allowed signature on the state of `blit` is not feasible, we err on the side on being too permissive in the type definition: Rather not flag a type error and only raise on runtime than complain on an actually working signature. Closes #29960. --- lib/matplotlib/animation.py | 8 ++++---- lib/matplotlib/animation.pyi | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 97e2cbc64ede..c6ff7702d992 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1769,8 +1769,8 @@ def _init_draw(self): self._drawn_artists = self._init_func() if self._blit: if self._drawn_artists is None: - raise RuntimeError('The init_func must return a ' - 'sequence of Artist objects.') + raise RuntimeError('When blit=True, the init_func must ' + 'return a sequence of Artist objects.') for a in self._drawn_artists: a.set_animated(self._blit) self._save_seq = [] @@ -1787,8 +1787,8 @@ def _draw_frame(self, framedata): if self._blit: - err = RuntimeError('The animation function must return a sequence ' - 'of Artist objects.') + err = RuntimeError('When blit=True, the animation function must ' + 'return a sequence of Artist objects.') try: # check if a sequence iter(self._drawn_artists) diff --git a/lib/matplotlib/animation.pyi b/lib/matplotlib/animation.pyi index 345e3c6dbe61..f725df8ebb22 100644 --- a/lib/matplotlib/animation.pyi +++ b/lib/matplotlib/animation.pyi @@ -206,9 +206,9 @@ class FuncAnimation(TimedAnimation): def __init__( self, fig: Figure, - func: Callable[..., Iterable[Artist]], + func: Callable[..., Iterable[Artist] | None], frames: Iterable | int | Callable[[], Generator] | None = ..., - init_func: Callable[[], Iterable[Artist]] | None = ..., + init_func: Callable[[], Iterable[Artist] | None] | None = ..., fargs: tuple[Any, ...] | None = ..., save_count: int | None = ..., *,