Skip to content

Commit 3258cda

Browse files
committed
Add ruff rules PERF for performance
1 parent e85bfca commit 3258cda

File tree

11 files changed

+55
-64
lines changed

11 files changed

+55
-64
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@ repos:
1212
- id: end-of-file-fixer
1313
- id: check-toml
1414
name: Validate pyproject.toml
15+
16+
- repo: https://github.com/codespell-project/codespell
17+
rev: v2.4.1
18+
hooks:
19+
- id: codespell
20+
files: ^.*\.(py|md|rst)$
21+
args: ["-L", "medias,nam"]
22+
1523
- repo: https://github.com/astral-sh/ruff-pre-commit
16-
rev: v0.14.6
24+
rev: v0.14.7
1725
hooks:
1826
- id: ruff
1927
name: ruff lint
2028
types: [python]
2129
args: [--exit-non-zero-on-fix]
2230
- id: ruff-format
2331
types: [python]
32+
2433
- repo: https://github.com/pre-commit/mirrors-mypy
25-
rev: v1.18.2
34+
rev: v1.19.0
2635
hooks:
2736
- id: mypy
2837
additional_dependencies:
@@ -34,10 +43,3 @@ repos:
3443
types-setuptools,
3544
]
3645
files: ^manim/
37-
38-
- repo: https://github.com/codespell-project/codespell
39-
rev: v2.4.1
40-
hooks:
41-
- id: codespell
42-
files: ^.*\.(py|md|rst)$
43-
args: ["-L", "medias,nam"]

manim/cli/init/commands.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ def select_resolution() -> tuple[int, int]:
4343
tuple[int, int]
4444
Tuple containing height and width.
4545
"""
46-
resolution_options: list[tuple[int, int]] = []
47-
for quality in QUALITIES.items():
48-
resolution_options.append(
49-
(quality[1]["pixel_height"], quality[1]["pixel_width"]),
50-
)
46+
resolution_options: list[tuple[int, int]] = [
47+
(quality[1]["pixel_height"], quality[1]["pixel_width"]) for quality in QUALITIES.items()
48+
]
5149
resolution_options.pop()
5250
choice = click.prompt(
5351
"\nSelect resolution:\n",

manim/mobject/geometry/boolean_ops.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,7 @@ def __init__(self, *vmobjects: VMobject, **kwargs: Any) -> None:
182182
if len(vmobjects) < 2:
183183
raise ValueError("At least 2 mobjects needed for Union.")
184184
super().__init__(**kwargs)
185-
paths = []
186-
for vmobject in vmobjects:
187-
paths.append(self._convert_vmobject_to_skia_path(vmobject))
185+
paths = [self._convert_vmobject_to_skia_path(vmobject) for vmobject in vmobjects]
188186
outpen = SkiaPath()
189187
union(paths, outpen.getPen())
190188
self._convert_skia_path_to_vmobject(outpen)

manim/mobject/graph.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,10 +1021,7 @@ def _add_edge(
10211021
"""
10221022
if edge_config is None:
10231023
edge_config = self.default_edge_config.copy()
1024-
added_mobjects = []
1025-
for v in edge:
1026-
if v not in self.vertices:
1027-
added_mobjects.append(self._add_vertex(v))
1024+
added_mobjects = [self._add_vertex(v) for v in edge if v not in self.vertices]
10281025
u, v = edge
10291026

10301027
self._graph.add_edge(u, v)

manim/mobject/mobject.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,8 +2959,7 @@ def add_n_more_submobjects(self, n: int) -> Self | None:
29592959
new_submobs = []
29602960
for submob, sf in zip(self.submobjects, split_factors):
29612961
new_submobs.append(submob)
2962-
for _ in range(1, sf):
2963-
new_submobs.append(submob.copy().fade(1))
2962+
new_submobs.extend(submob.copy().fade(1) for _ in range(1, sf))
29642963
self.submobjects = new_submobs
29652964
return self
29662965

manim/mobject/three_d/polyhedra.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ def extract_face_coords(self) -> Point3DLike_Array:
154154
"""Extracts the coordinates of the vertices in the graph.
155155
Used for updating faces.
156156
"""
157-
new_vertex_coords = []
158-
for v in self.graph.vertices:
159-
new_vertex_coords.append(self.graph[v].get_center())
157+
new_vertex_coords = [self.graph[v].get_center() for v in self.graph.vertices]
160158
layout = dict(enumerate(new_vertex_coords))
161159
return [[layout[j] for j in i] for i in self.faces_list]
162160

manim/renderer/shader_wrapper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ def get_program_code(self):
141141

142142
def replace_code(self, old: str, new: str) -> None:
143143
code_map = self.program_code
144-
for name, _code in code_map.items():
145-
if code_map[name] is None:
146-
continue
147-
code_map[name] = re.sub(old, new, code_map[name])
144+
for name, code in code_map.items():
145+
if code:
146+
code_map[name] = re.sub(old, new, code)
148147
self.refresh_id()
149148

150149
def combine_with(self, *shader_wrappers: "ShaderWrapper") -> Self: # noqa: UP037

manim/utils/deprecation.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,7 @@ def deprecate_params(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
521521
arguments.
522522
523523
"""
524-
used = []
525-
for param in params:
526-
if param in kwargs:
527-
used.append(param)
524+
used = [param for param in params if param in kwargs]
528525

529526
if len(used) > 0:
530527
logger.warning(warning_msg(func, used))

pyproject.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ dev = [
8787
"pytest>=8.3.4",
8888
"pytest-cov>=6.0.0",
8989
"pytest-xdist>=2.2,<3.0",
90-
"ruff>=0.9.3",
90+
"ruff>=0.14.7",
9191
"sphinx>=7.4.7",
9292
"sphinx-copybutton>=0.5.2",
9393
"sphinx-design>=0.6.1",
@@ -149,13 +149,14 @@ select = [
149149
"C4",
150150
"D",
151151
"E",
152-
"W",
153152
"F",
154153
"I",
154+
"PERF",
155155
"PGH",
156156
"PT",
157157
"SIM",
158158
"UP",
159+
"W",
159160
]
160161

161162
ignore = [
@@ -172,18 +173,19 @@ ignore = [
172173
"D205",
173174
"D212",
174175
"D4",
176+
"E111",
177+
"E114",
178+
"E117",
179+
"E501",
175180
# due to the import * used in manim
176181
"F403",
177182
"F405",
183+
"PERF203", # try-except-in-loop
178184
# Exception too broad (this would require lots of changes + re.escape) for little benefit
179185
"PT011",
180186
# as recommended by https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
181187
"D206",
182188
"D300",
183-
"E111",
184-
"E114",
185-
"E117",
186-
"E501",
187189
]
188190

189191
# Allow unused variables when underscore-prefixed.

tests/module/mobject/test_graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ def test_graph_accepts_labeledline_as_edge_type():
105105
vertices, edges, edge_type=LabeledLine, edge_config=edge_config
106106
)
107107

108-
for _edge_key, edge_obj in G_manual.edges.items():
108+
for edge_obj in G_manual.edges.values():
109109
assert isinstance(edge_obj, LabeledLine)
110110
assert hasattr(edge_obj, "label")
111111

112-
for _edge_key, edge_obj in G_directed.edges.items():
112+
for edge_obj in G_directed.edges.values():
113113
assert isinstance(edge_obj, LabeledLine)
114114
assert hasattr(edge_obj, "label")
115115

0 commit comments

Comments
 (0)