Skip to content

Commit 5e85d32

Browse files
[fix] 'unnecessary-dict-index-lookup' for in-loop deletetion
Closes #10726
1 parent a0bfd3f commit 5e85d32

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fixed a crash in ``unnecessary-dict-index-lookup`` when the index of an enumerated list
2+
was deleted inside a for loop.
3+
4+
Closes #10726

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,10 +2355,10 @@ def _check_unnecessary_list_index_lookup(
23552355
or iterating_object_name != subscript.value.as_string()
23562356
):
23572357
continue
2358-
23592358
if (
23602359
isinstance(node, nodes.For)
2361-
and index.lookup(index.name)[1][-1].lineno > node.lineno
2360+
and (lookup_results := index.lookup(index.name)[1])
2361+
and lookup_results[-1].lineno > node.lineno
23622362
):
23632363
# Ignore this subscript if it has been redefined after
23642364
# the for loop.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Test for deleted index."""
2+
def test_deleted_index(letters: list[str]) -> None:
3+
for index, letter in enumerate(letters):
4+
del index
5+
print(letters[index])

0 commit comments

Comments
 (0)