Skip to content

Commit 1214a74

Browse files
authoredApr 3, 2025··
Enable FURB187 - avoid reverse list copy (#18716)
Enable a ruff rule that avoid copies in list reversal. Use builtin reverse method. The builtin does an efficient stride reversal and avoids accidental copies. This is extra helpful since the changed reverses are for perf optimization.
1 parent 6b68661 commit 1214a74

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed
 

‎mypy/semanal_main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def process_top_levels(graph: Graph, scc: list[str], patches: Patches) -> None:
181181

182182
# Reverse order of the scc so the first modules in the original list will be
183183
# be processed first. This helps with performance.
184-
scc = list(reversed(scc))
184+
scc = list(reversed(scc)) # noqa: FURB187 intentional copy
185185

186186
# Initialize ASTs and symbol tables.
187187
for id in scc:

‎mypyc/analysis/dataflow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def run_analysis(
542542
# Set up initial state for worklist algorithm.
543543
worklist = list(blocks)
544544
if not backward:
545-
worklist = worklist[::-1] # Reverse for a small performance improvement
545+
worklist.reverse() # Reverse for a small performance improvement
546546
workset = set(worklist)
547547
before: dict[BasicBlock, set[T]] = {}
548548
after: dict[BasicBlock, set[T]] = {}

‎pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ select = [
148148
"SIM201", "SIM202", "SIM222", "SIM223", # flake8-simplify
149149
"FURB168", # Prefer is operator over isinstance for None checks
150150
"FURB169", # Do not use is comparison with type(None). Use None
151+
"FURB187", # avoid list reverse copy
151152
"FURB188", # use str.remove(pre|suf)fix
152153
"ISC001", # implicitly concatenated string
153154
"RET501", "RET502", # better return None handling

0 commit comments

Comments
 (0)
Please sign in to comment.