From 6edcbc53f71e9dfe58d4b601539d6c52ddc8a560 Mon Sep 17 00:00:00 2001 From: James Lin Date: Sat, 18 Jul 2026 11:59:48 -0700 Subject: [PATCH] Raise NotImplementedError in MovieWriter._args instead of returning it MovieWriter._args() is a placeholder meant to be overridden by concrete subclasses. It used `return NotImplementedError(...)` instead of `raise`, so a MovieWriter subclass that forgot to override it would not fail loudly: _run() calls `command = self._args()`, which would silently bind an exception instance and pass it on as if it were the command-line argument list, producing a confusing downstream error instead of the intended 'args needs to be implemented by subclass' message. Add a regression test. --- lib/matplotlib/animation.py | 2 +- lib/matplotlib/tests/test_animation.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 7146dc28fcc9..00bb47ebec93 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -356,7 +356,7 @@ def grab_frame(self, **savefig_kwargs): def _args(self): """Assemble list of encoder-specific command-line arguments.""" - return NotImplementedError("args needs to be implemented by subclass.") + raise NotImplementedError("args needs to be implemented by subclass.") @classmethod def bin_path(cls): diff --git a/lib/matplotlib/tests/test_animation.py b/lib/matplotlib/tests/test_animation.py index bfddddf6d665..0fef17e6c44b 100644 --- a/lib/matplotlib/tests/test_animation.py +++ b/lib/matplotlib/tests/test_animation.py @@ -135,6 +135,17 @@ def _run(self): assert writer.dpi == fig.dpi +def test_movie_writer_args_not_implemented(): + # MovieWriter._args is a placeholder that subclasses must override; it + # must *raise* NotImplementedError rather than merely returning it, so a + # subclass that forgets to override it fails loudly instead of passing an + # exception instance on as if it were the command-line arguments. + writer = animation.MovieWriter.__new__(animation.MovieWriter) + with pytest.raises(NotImplementedError, + match="args needs to be implemented by subclass"): + writer._args() + + @animation.writers.register('null') class RegisteredNullMovieWriter(NullMovieWriter):