Skip to content

Commit

Permalink
Use ruff for pytest style (ManimCommunity#3872)
Browse files Browse the repository at this point in the history
* Add ruff config for pytest

* Fixes for pre-commit

* Changes to make pre-commit happy (Unrelated to PR)

---------

Co-authored-by: Tristan Schulz <[email protected]>
  • Loading branch information
JasonGrace2282 and MrDiver authored Jul 21, 2024
1 parent fa65b48 commit c8fe4d9
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 34 deletions.
3 changes: 2 additions & 1 deletion manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,8 @@ def restore(self) -> Self:

def reduce_across_dimension(self, reduce_func: Callable, dim: int):
"""Find the min or max value from a dimension across all points in this and submobjects."""
assert dim >= 0 and dim <= 2
assert dim >= 0
assert dim <= 2
if len(self.submobjects) == 0 and len(self.points) == 0:
# If we have no points and no submobjects, return 0 (e.g. center)
return 0
Expand Down
11 changes: 9 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ flake8-bugbear = "^23.11.28"
flake8-builtins = "^2.2.0"
flake8-comprehensions = "^3.7.0"
flake8-docstrings = "^1.7.0"
flake8-pytest-style = "^1.7.2"
flake8-simplify = "^0.14.1"
flake8-rst-docstrings = "^0.3.0"
furo = "^2023.09.10"
gitpython = "^3"
isort = "^5.12.0"
Expand Down Expand Up @@ -139,13 +137,18 @@ select = [
"E",
"F",
"I",
"PT",
"UP",
]

ignore = [
# due to the import * used in manim
"F403",
"F405",
# fixtures not returning anything should have leading underscore
"PT004",
# Exception too broad (this would require lots of changes + re.escape) for little benefit
"PT011",
# as recommended by https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"E111",
"E114",
Expand Down Expand Up @@ -176,6 +179,10 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
[tool.ruff.lint.isort]
required-imports = ["from __future__ import annotations"]

[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
mark-parentheses = false

[tool.ruff.format]
docstring-code-format = true

Expand Down
12 changes: 4 additions & 8 deletions tests/module/animation/test_animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ def test_chained_animate():
scale_factor = 2
direction = np.array((1, 1, 0))
anim = s.animate.scale(scale_factor).shift(direction).build()
assert (
anim.mobject.target.width == scale_factor * s.width
and (anim.mobject.target.get_center() == direction).all()
)
assert anim.mobject.target.width == scale_factor * s.width
assert (anim.mobject.target.get_center() == direction).all()


def test_overridden_animate():
Expand Down Expand Up @@ -103,10 +101,8 @@ def test_chained_animate_with_args():
run_time = 2

anim = s.animate(run_time=run_time).scale(scale_factor).shift(direction).build()
assert (
anim.mobject.target.width == scale_factor * s.width
and (anim.mobject.target.get_center() == direction).all()
)
assert anim.mobject.target.width == scale_factor * s.width
assert (anim.mobject.target.get_center() == direction).all()
assert anim.run_time == run_time


Expand Down
2 changes: 1 addition & 1 deletion tests/module/animation/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_animationgroup_with_wait():


@pytest.mark.parametrize(
"animation_remover, animation_group_remover",
("animation_remover", "animation_group_remover"),
[(False, True), (True, False)],
)
def test_animationgroup_is_passing_remover_to_animations(
Expand Down
2 changes: 1 addition & 1 deletion tests/module/mobject/graphing/test_coordinate_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_dimension():

def test_abstract_base_class():
"""Check that CoordinateSystem has some abstract methods."""
with pytest.raises(Exception):
with pytest.raises(NotImplementedError):
CS().get_axes()


Expand Down
2 changes: 1 addition & 1 deletion tests/module/mobject/test_boolean_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@pytest.mark.parametrize(
"test_input,expected",
("test_input", "expected"),
[
(
[(1.0, 2.0), (3.0, 4.0)],
Expand Down
2 changes: 1 addition & 1 deletion tests/module/mobject/text/test_texmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_SingleStringMathTex(config):


@pytest.mark.parametrize( # : PT006
"text_input,length_sub",
("text_input", "length_sub"),
[("{{ a }} + {{ b }} = {{ c }}", 5), (r"\frac{1}{a+b\sqrt{2}}", 1)],
)
def test_double_braces_testing(text_input, length_sub):
Expand Down
3 changes: 2 additions & 1 deletion tests/module/scene/test_auto_zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_zoom():

assert scene.camera.frame_width == abs(
s1.get_left()[0] - s2.get_right()[0],
) and scene.camera.frame.get_center()[0] == (
)
assert scene.camera.frame.get_center()[0] == (
abs(s1.get_center()[0] + s2.get_center()[0]) / 2
)
12 changes: 4 additions & 8 deletions tests/opengl/test_animate_opengl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ def test_chained_animate(using_opengl_renderer):
scale_factor = 2
direction = np.array((1, 1, 0))
anim = s.animate.scale(scale_factor).shift(direction).build()
assert (
anim.mobject.target.width == scale_factor * s.width
and (anim.mobject.target.get_center() == direction).all()
)
assert anim.mobject.target.width == scale_factor * s.width
assert (anim.mobject.target.get_center() == direction).all()


def test_overridden_animate(using_opengl_renderer):
Expand Down Expand Up @@ -103,10 +101,8 @@ def test_chained_animate_with_args(using_opengl_renderer):
run_time = 2

anim = s.animate(run_time=run_time).scale(scale_factor).shift(direction).build()
assert (
anim.mobject.target.width == scale_factor * s.width
and (anim.mobject.target.get_center() == direction).all()
)
assert anim.mobject.target.width == scale_factor * s.width
assert (anim.mobject.target.get_center() == direction).all()
assert anim.run_time == run_time


Expand Down
2 changes: 1 addition & 1 deletion tests/opengl/test_coordinate_system_opengl.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_dimension(using_opengl_renderer):

def test_abstract_base_class(using_opengl_renderer):
"""Check that CoordinateSystem has some abstract methods."""
with pytest.raises(Exception):
with pytest.raises(NotImplementedError):
CS().get_axes()


Expand Down
2 changes: 1 addition & 1 deletion tests/opengl/test_texmobject_opengl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_SingleStringMathTex(config, using_opengl_renderer):


@pytest.mark.parametrize( # : PT006
"text_input,length_sub",
("text_input", "length_sub"),
[("{{ a }} + {{ b }} = {{ c }}", 5), (r"\frac{1}{a+b\sqrt{2}}", 1)],
)
def test_double_braces_testing(using_opengl_renderer, text_input, length_sub):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_graphical_units/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def show_diff(request):

@pytest.fixture(params=[True, False])
def use_vectorized(request):
yield request.param
return request.param
3 changes: 2 additions & 1 deletion tests/test_graphical_units/test_transform_matching_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def test_TransformMatchingLeavesOneObject(scene):
circle = Circle().shift(RIGHT)
scene.add(square)
scene.play(TransformMatchingShapes(square, circle))
assert len(scene.mobjects) == 1 and isinstance(scene.mobjects[0], Circle)
assert len(scene.mobjects) == 1
assert isinstance(scene.mobjects[0], Circle)


@frames_comparison(last_frame=False)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_linear_transformation_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ def test_ghost_vectors_len_and_types():
assert len(ghosts[0]) == 2

# check types of ghost vectors
assert isinstance(ghosts, VGroup) and isinstance(ghosts[0], VGroup)
assert isinstance(ghosts, VGroup)
assert isinstance(ghosts[0], VGroup)
assert all(isinstance(x, Vector) for x in ghosts[0])
6 changes: 4 additions & 2 deletions tests/test_logging/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_error_logging(tmp_path, python_version):
out, err, exitcode = capture(command)
if err is None:
err = out
assert exitcode != 0 and "Traceback (most recent call last)" in err
assert exitcode != 0
assert "Traceback (most recent call last)" in err


@logs_comparison(
Expand All @@ -69,4 +70,5 @@ def test_tex_error_logs(tmp_path, python_version):
"BadTex",
]
_, err, exitcode = capture(command)
assert exitcode != 0 and len(err) > 0
assert exitcode != 0
assert len(err) > 0
4 changes: 2 additions & 2 deletions tests/test_plugins/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def {function_name}():

@pytest.fixture
def simple_scenes_path():
yield Path(__file__).parent / "simple_scenes.py"
return Path(__file__).parent / "simple_scenes.py"


def cfg_file_create(cfg_file_contents, path):
Expand All @@ -73,7 +73,7 @@ def random_string():
all_letters = string.ascii_lowercase
a = random.Random()
final_letters = [a.choice(all_letters) for _ in range(8)]
yield "".join(final_letters)
return "".join(final_letters)


def test_plugin_warning(tmp_path, python_version, simple_scenes_path):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scene_rendering/test_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_gif_writing(tmp_path, transparent):

@pytest.mark.slow
@pytest.mark.parametrize(
"format, transparent, codec, pixel_format",
("format", "transparent", "codec", "pixel_format"),
[
("mp4", False, "h264", "yuv420p"),
("mov", False, "h264", "yuv420p"),
Expand Down

0 comments on commit c8fe4d9

Please sign in to comment.