Repository: Zulko/moviepy
Pull request #1842 (2022-10-10 15:29:09)

Raise `ValueError` if invalid arguments passed to `TextClip`
At least one of `filename` or `text` arguments of `TextClip` are required. Now raises `ValueError` if no `filename` and `text` arguments are provided.


coveralls: 
[![Coverage Status](https://coveralls.io/builds/53142575/badge)](https://coveralls.io/builds/53142575)

Coverage decreased (-2.2%) to 81.756% when pulling **f6ef574698ba534849540a380d8bfd65b72566da on filename-or-text-required-TextClip** into **97870c976d24d540460b6e863f48cd92f0cb35e6 on master**.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 423c2ec..5caac73 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 <!-- for soon-to-be removed features -->
 - `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 07b0487..76e19e4 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -1254,9 +1254,13 @@ class TextClip(ImageClip):
                     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 = (


Test patch:
diff --git a/tests/test_TextClip.py b/tests/test_TextClip.py
index e30aca0..9745fc9 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()