From 858bb81fba7e09f1f562283ed6d1394db883b6c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Mon, 10 Oct 2022 17:42:36 +0200 Subject: [PATCH] Raise `ValueError` if invalid arguments passed to `TextClip` (#1842) * Raise error if invalid arguments passed to TextClip * Add CHANGELOG entry --- CHANGELOG.md | 1 + moviepy/video/VideoClip.py | 6 +++++- tests/test_TextClip.py | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 423c2ec66..5caac733b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `accel_decel` FX raises `ValueError` if `sooness` parameter value is lower than zero [\#1546](https://github.com/Zulko/moviepy/pull/1546) - `Clip.subclip` raise `ValueError` if `start_time >= clip.duration` (previously printing a message to stdout only if `start_time > clip.duration`) [\#1589](https://github.com/Zulko/moviepy/pull/1589) - Allow to pass times in `HH:MM:SS` format to `t` argument of `clip.show` method [\#1594](https://github.com/Zulko/moviepy/pull/1594) +- `TextClip` now raises `ValueError` if none of the `text` or `filename` arguments are specified [\#1842](https://github.com/Zulko/moviepy/pull/1842) ### Deprecated - `moviepy.video.fx.all` and `moviepy.audio.fx.all`. Use the fx method directly from the clip instance or import the fx function from `moviepy.video.fx` and `moviepy.audio.fx`. [\#1105](https://github.com/Zulko/moviepy/pull/1105) diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py index 07b048704..76e19e47a 100644 --- a/moviepy/video/VideoClip.py +++ b/moviepy/video/VideoClip.py @@ -1254,9 +1254,13 @@ def __init__( os.write(temptxt_fd, text) os.close(temptxt_fd) text = "@" + temptxt - else: + elif filename is not None: # use a file instead of a text. text = "@" + filename + else: + raise ValueError( + "You must provide either 'text' or 'filename' arguments to TextClip" + ) if size is not None: size = ( diff --git a/tests/test_TextClip.py b/tests/test_TextClip.py index e30aca082..9745fc9f4 100644 --- a/tests/test_TextClip.py +++ b/tests/test_TextClip.py @@ -77,5 +77,22 @@ def test_text_filename_arguments_consistence(util): assert np.equal(frames_from_text[0], frames_from_file[0]).all() +@pytest.mark.parametrize( + "method", ("caption", "label"), ids=("method=caption", "method=label") +) +def test_no_text_nor_filename_arguments(method, util): + expected_error_msg = ( + "^You must provide either 'text' or 'filename' arguments to TextClip$" + ) + with pytest.raises(ValueError, match=expected_error_msg): + TextClip( + size=(20, 20), + color="#000", + bg_color="#FFF", + font=util.FONT, + method=method, + ) + + if __name__ == "__main__": pytest.main()