A community-maintained Python framework for creating mathematical animations.
[pre-commit.ci] pre-commit autoupdate (#3929)
pre-commit-ci[bot]39382e6ba8b4
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yamlindex a4c90fccb..68895ec5e 100644--- a/.pre-commit-config.yaml+++ b/.pre-commit-config.yaml@@ -1,9 +1,9 @@-default_stages: [commit, push]+default_stages: [pre-commit, pre-push]fail_fast: falseexclude: ^(manim/grpc/gen/|docs/i18n/)repos:- repo: https://github.com/pre-commit/pre-commit-hooks- rev: v4.6.0+ rev: v5.0.0hooks:- id: check-astname: Validate Python@@ -13,7 +13,7 @@ repos:- id: check-tomlname: Validate Poetry- repo: https://github.com/astral-sh/ruff-pre-commit- rev: v0.6.2+ rev: v0.7.0hooks:- id: ruffname: ruff lint@@ -22,7 +22,7 @@ repos:- id: ruff-formattypes: [python]- repo: https://github.com/pre-commit/mirrors-mypy- rev: v1.11.2+ rev: v1.12.1hooks:- id: mypyadditional_dependencies:diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.pyindex ef12c517b..4293de710 100644--- a/manim/scene/scene_file_writer.py+++ b/manim/scene/scene_file_writer.py@@ -365,11 +365,10 @@ class SceneFileWriter:if file_path.suffix not in (".wav", ".raw"):# we need to pass delete=False to work on Windows# TODO: figure out a way to cache the wav file generated (benchmark needed)- wav_file_path = NamedTemporaryFile(suffix=".wav", delete=False)- convert_audio(file_path, wav_file_path, "pcm_s16le")- new_segment = AudioSegment.from_file(wav_file_path.name)- logger.info(f"Automatically converted {file_path} to .wav")- wav_file_path.close()+ with NamedTemporaryFile(suffix=".wav", delete=False) as wav_file_path:+ convert_audio(file_path, wav_file_path, "pcm_s16le")+ new_segment = AudioSegment.from_file(wav_file_path.name)+ logger.info(f"Automatically converted {file_path} to .wav")Path(wav_file_path.name).unlink()else:new_segment = AudioSegment.from_file(file_path)diff --git a/tests/module/scene/test_sound.py b/tests/module/scene/test_sound.pyindex cfd67ed2d..cfa9a4da4 100644--- a/tests/module/scene/test_sound.py+++ b/tests/module/scene/test_sound.py@@ -10,14 +10,12 @@ from manim import Scenedef test_add_sound(tmpdir):# create sound filesound_loc = Path(tmpdir, "noise.wav")- f = wave.open(str(sound_loc), "w")- f.setparams((2, 2, 44100, 0, "NONE", "not compressed"))- for _ in range(22050): # half a second of sound- packed_value = struct.pack("h", 14242)- f.writeframes(packed_value)- f.writeframes(packed_value)-- f.close()+ with wave.open(str(sound_loc), "w") as f:+ f.setparams((2, 2, 44100, 0, "NONE", "not compressed"))+ for _ in range(22050): # half a second of sound+ packed_value = struct.pack("h", 14242)+ f.writeframes(packed_value)+ f.writeframes(packed_value)scene = Scene()scene.add_sound(sound_loc)diff --git a/tests/opengl/test_config_opengl.py b/tests/opengl/test_config_opengl.pyindex e0dea461f..d0ca0e5a8 100644--- a/tests/opengl/test_config_opengl.py+++ b/tests/opengl/test_config_opengl.py@@ -50,16 +50,15 @@ def test_background_color(config, using_opengl_renderer, dry_run):def test_digest_file(config, using_opengl_renderer, tmp_path):"""Test that a config file can be digested programmatically."""- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- """- [CLI]- media_dir = this_is_my_favorite_path- video_dir = {media_dir}/videos- frame_height = 10- """,- )- tmp_cfg.close()+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ """+ [CLI]+ media_dir = this_is_my_favorite_path+ video_dir = {media_dir}/videos+ frame_height = 10+ """,+ )config.digest_file(tmp_cfg.name)assert config.get_dir("media_dir") == Path("this_is_my_favorite_path")@@ -74,15 +73,14 @@ def test_frame_size(config, using_opengl_renderer, tmp_path):np.testing.assert_allclose(config.frame_height, 8.0)with tempconfig({}):- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- """- [CLI]- pixel_height = 10- pixel_width = 10- """,- )- tmp_cfg.close()+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ """+ [CLI]+ pixel_height = 10+ pixel_width = 10+ """,+ )config.digest_file(tmp_cfg.name)# aspect ratio is set using pixel measurements@@ -93,16 +91,16 @@ def test_frame_size(config, using_opengl_renderer, tmp_path):def test_frame_size_if_frame_width(config, using_opengl_renderer, tmp_path):- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- """- [CLI]- pixel_height = 10- pixel_width = 10- frame_height = 10- frame_width = 10- """,- )+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ """+ [CLI]+ pixel_height = 10+ pixel_width = 10+ frame_height = 10+ frame_width = 10+ """,+ )tmp_cfg.close()config.digest_file(tmp_cfg.name)diff --git a/tests/opengl/test_sound_opengl.py b/tests/opengl/test_sound_opengl.pyindex 40ec914de..cc4ccceae 100644--- a/tests/opengl/test_sound_opengl.py+++ b/tests/opengl/test_sound_opengl.py@@ -13,14 +13,12 @@ from manim import Scenedef test_add_sound(using_opengl_renderer, tmpdir):# create sound filesound_loc = Path(tmpdir, "noise.wav")- f = wave.open(str(sound_loc), "w")- f.setparams((2, 2, 44100, 0, "NONE", "not compressed"))- for _ in range(22050): # half a second of sound- packed_value = struct.pack("h", 14242)- f.writeframes(packed_value)- f.writeframes(packed_value)-- f.close()+ with wave.open(str(sound_loc), "w") as f:+ f.setparams((2, 2, 44100, 0, "NONE", "not compressed"))+ for _ in range(22050): # half a second of sound+ packed_value = struct.pack("h", 14242)+ f.writeframes(packed_value)+ f.writeframes(packed_value)scene = Scene()scene.add_sound(sound_loc)diff --git a/tests/test_config.py b/tests/test_config.pyindex 1cff88549..a245f4027 100644--- a/tests/test_config.py+++ b/tests/test_config.py@@ -100,17 +100,16 @@ def test_background_color(config):def test_digest_file(tmp_path, config):"""Test that a config file can be digested programmatically."""- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- """- [CLI]- media_dir = this_is_my_favorite_path- video_dir = {media_dir}/videos- sections_dir = {media_dir}/{scene_name}/prepare_for_unforeseen_consequences- frame_height = 10- """,- )- tmp_cfg.close()+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ """+ [CLI]+ media_dir = this_is_my_favorite_path+ video_dir = {media_dir}/videos+ sections_dir = {media_dir}/{scene_name}/prepare_for_unforeseen_consequences+ frame_height = 10+ """,+ )config.digest_file(tmp_cfg.name)assert config.get_dir("media_dir") == Path("this_is_my_favorite_path")@@ -156,15 +155,14 @@ def test_custom_dirs(tmp_path, config):def test_pixel_dimensions(tmp_path, config):- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- """- [CLI]- pixel_height = 10- pixel_width = 10- """,- )- tmp_cfg.close()+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ """+ [CLI]+ pixel_height = 10+ pixel_width = 10+ """,+ )config.digest_file(tmp_cfg.name)# aspect ratio is set using pixel measurements@@ -181,17 +179,16 @@ def test_frame_size(tmp_path, config):)np.testing.assert_allclose(config.frame_height, 8.0)- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- """- [CLI]- pixel_height = 10- pixel_width = 10- frame_height = 10- frame_width = 10- """,- )- tmp_cfg.close()+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ """+ [CLI]+ pixel_height = 10+ pixel_width = 10+ frame_height = 10+ frame_width = 10+ """,+ )config.digest_file(tmp_cfg.name)np.testing.assert_allclose(config.aspect_ratio, 1.0)@@ -235,14 +232,13 @@ def test_tex_template_file(tmp_path):"""Test that a custom tex template file can be set from a config file."""tex_file = Path(tmp_path / "my_template.tex")tex_file.write_text("Hello World!")- tmp_cfg = tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False)- tmp_cfg.write(- f"""- [CLI]- tex_template_file = {tex_file}- """,- )- tmp_cfg.close()+ with tempfile.NamedTemporaryFile("w", dir=tmp_path, delete=False) as tmp_cfg:+ tmp_cfg.write(+ f"""+ [CLI]+ tex_template_file = {tex_file}+ """,+ )custom_config = ManimConfig().digest_file(tmp_cfg.name)