Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 186432d

Browse files
authored
Replace x[:] with x.copy() (#14949)
This PR fixes the [`FURB145`](https://github.com/dosisod/refurb/blob/master/docs/checks.md#furb145-no-slice-copy) error code from Refurb. In my (limited) testing, using `x.copy()` is faster then both the interpreted and compiled versions of `x[:]` (see #14887 (comment)). See #14887 for more info.
1 parent 7be0d4b commit 186432d

23 files changed

Lines changed: 39 additions & 39 deletions

mypy/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ def __init__(
19111911
self.caller_state = caller_state
19121912
self.caller_line = caller_line
19131913
if caller_state:
1914-
self.import_context = caller_state.import_context[:]
1914+
self.import_context = caller_state.import_context.copy()
19151915
self.import_context.append((caller_state.xpath, caller_line))
19161916
else:
19171917
self.import_context = []

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4472,7 +4472,7 @@ def infer_lambda_type_using_context(
44724472
is_ellipsis_args=False,
44734473
arg_types=[AnyType(TypeOfAny.special_form)] * len(arg_kinds),
44744474
arg_kinds=arg_kinds,
4475-
arg_names=e.arg_names[:],
4475+
arg_names=e.arg_names.copy(),
44764476
)
44774477

44784478
if ARG_STAR in arg_kinds or ARG_STAR2 in arg_kinds:

mypy/constraints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ def find_matching_overload_items(
11581158
if not res:
11591159
# Falling back to all items if we can't find a match is pretty arbitrary, but
11601160
# it maintains backward compatibility.
1161-
res = items[:]
1161+
res = items.copy()
11621162
return res
11631163

11641164

mypy/dmypy_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def fine_grained_increment_follow_imports(self, sources: list[BuildSource]) -> l
599599
messages = fine_grained_manager.update(changed, [], followed=True)
600600

601601
# Follow deps from changed modules (still within graph).
602-
worklist = changed[:]
602+
worklist = changed.copy()
603603
while worklist:
604604
module = worklist.pop()
605605
if module[0] not in graph:
@@ -706,7 +706,7 @@ def find_reachable_changed_modules(
706706
"""
707707
changed = []
708708
new_files = []
709-
worklist = roots[:]
709+
worklist = roots.copy()
710710
seen.update(source.module for source in worklist)
711711
while worklist:
712712
nxt = worklist.pop()

mypy/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ def current_module(self) -> str | None:
338338

339339
def import_context(self) -> list[tuple[str, int]]:
340340
"""Return a copy of the import context."""
341-
return self.import_ctx[:]
341+
return self.import_ctx.copy()
342342

343343
def set_import_context(self, ctx: list[tuple[str, int]]) -> None:
344344
"""Replace the entire import context with a new value."""
345-
self.import_ctx = ctx[:]
345+
self.import_ctx = ctx.copy()
346346

347347
def report(
348348
self,

mypy/memprofile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def visit(o: object) -> None:
103103
objs.append(o)
104104
seen.add(id(o))
105105

106-
for obj in objs[:]:
106+
for obj in objs.copy():
107107
if type(obj) is FakeInfo:
108108
# Processing these would cause a crash.
109109
continue

mypy/mro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def linearize_hierarchy(
4444

4545

4646
def merge(seqs: list[list[TypeInfo]]) -> list[TypeInfo]:
47-
seqs = [s[:] for s in seqs]
47+
seqs = [s.copy() for s in seqs]
4848
result: list[TypeInfo] = []
4949
while True:
5050
seqs = [s for s in seqs if s]

mypy/semanal_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def process_top_levels(graph: Graph, scc: list[str], patches: Patches) -> None:
190190
# Initially all namespaces in the SCC are incomplete (well they are empty).
191191
state.manager.incomplete_namespaces.update(scc)
192192

193-
worklist = scc[:]
193+
worklist = scc.copy()
194194
# HACK: process core stuff first. This is mostly needed to support defining
195195
# named tuples in builtin SCC.
196196
if all(m in worklist for m in core_modules):

mypy/server/deps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ def visit_tuple_type(self, typ: TupleType) -> list[str]:
10271027
def visit_type_type(self, typ: TypeType) -> list[str]:
10281028
triggers = self.get_type_triggers(typ.item)
10291029
if not self.use_logical_deps:
1030-
old_triggers = triggers[:]
1030+
old_triggers = triggers.copy()
10311031
for trigger in old_triggers:
10321032
triggers.append(trigger.rstrip(">") + ".__init__>")
10331033
triggers.append(trigger.rstrip(">") + ".__new__>")

mypy/server/update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __init__(self, result: BuildResult) -> None:
187187
# Merge in any root dependencies that may not have been loaded
188188
merge_dependencies(manager.load_fine_grained_deps(FAKE_ROOT_MODULE), self.deps)
189189
self.previous_targets_with_errors = manager.errors.targets()
190-
self.previous_messages: list[str] = result.errors[:]
190+
self.previous_messages: list[str] = result.errors.copy()
191191
# Module, if any, that had blocking errors in the last run as (id, path) tuple.
192192
self.blocking_error: tuple[str, str] | None = None
193193
# Module that we haven't processed yet but that are known to be stale.
@@ -302,7 +302,7 @@ def update(
302302
break
303303

304304
messages = sort_messages_preserving_file_order(messages, self.previous_messages)
305-
self.previous_messages = messages[:]
305+
self.previous_messages = messages.copy()
306306
return messages
307307

308308
def trigger(self, target: str) -> list[str]:
@@ -322,7 +322,7 @@ def trigger(self, target: str) -> list[str]:
322322
)
323323
# Preserve state needed for the next update.
324324
self.previous_targets_with_errors = self.manager.errors.targets()
325-
self.previous_messages = self.manager.errors.new_messages()[:]
325+
self.previous_messages = self.manager.errors.new_messages().copy()
326326
return self.update(changed_modules, [])
327327

328328
def flush_cache(self) -> None:

0 commit comments

Comments
 (0)