From 5639edf90e265b003d4221c225cc1776a396e87c Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Tue, 22 Nov 2022 15:54:12 -0300 Subject: [PATCH 1/3] handle Attribute --- doc/whatsnew/fragments/7821.bugfix | 3 +++ .../checkers/refactoring/refactoring_checker.py | 16 +++++++--------- .../unnecessary/unnecessary_list_index_lookup.py | 6 ++++++ 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 doc/whatsnew/fragments/7821.bugfix diff --git a/doc/whatsnew/fragments/7821.bugfix b/doc/whatsnew/fragments/7821.bugfix new file mode 100644 index 0000000000..30ed305e1e --- /dev/null +++ b/doc/whatsnew/fragments/7821.bugfix @@ -0,0 +1,3 @@ +Fixes bug that resulted in crash during ``unnecessary_list_index_lookup`` check. + +Closes #7821 diff --git a/pylint/checkers/refactoring/refactoring_checker.py b/pylint/checkers/refactoring/refactoring_checker.py index ec68099a6b..095a215a9d 100644 --- a/pylint/checkers/refactoring/refactoring_checker.py +++ b/pylint/checkers/refactoring/refactoring_checker.py @@ -2301,15 +2301,13 @@ def _enumerate_with_start( return False, confidence def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]: - confidence = HIGH - - if isinstance(node, (nodes.Name, nodes.Call)): + if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)): inferred = utils.safe_infer(node) start_val = inferred.value if inferred else None - confidence = INFERENCE - elif isinstance(node, nodes.UnaryOp): - start_val = node.operand.value - else: - start_val = node.value + return start_val, INFERENCE + if isinstance(node, nodes.UnaryOp): + return node.operand.value, HIGH + if isinstance(node, nodes.Const): + return node.value, HIGH - return start_val, confidence + return None, HIGH diff --git a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py index eb23938bcb..71dcff0b83 100644 --- a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py +++ b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py @@ -138,3 +138,9 @@ def return_start(start): for idx, val in enumerate(): print(my_list[idx]) + +class Command: + def _get_extra_attrs(self, extra_columns): + self.extra_rows_start = 8 # pylint: disable=attribute-defined-outside-init + for index, column in enumerate(extra_columns, start=self.extra_rows_start): + pass From 58a3c9191fb7f7014f4d8db44f32ffcd0532a8e5 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Thu, 24 Nov 2022 16:59:28 -0300 Subject: [PATCH 2/3] add another test case --- .../u/unnecessary/unnecessary_list_index_lookup.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py index 71dcff0b83..ec5ee22c20 100644 --- a/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py +++ b/tests/functional/u/unnecessary/unnecessary_list_index_lookup.py @@ -144,3 +144,8 @@ def _get_extra_attrs(self, extra_columns): self.extra_rows_start = 8 # pylint: disable=attribute-defined-outside-init for index, column in enumerate(extra_columns, start=self.extra_rows_start): pass + +Y_START = 2 +nums = list(range(20)) +for y, x in enumerate(nums, start=Y_START + 1): + pass From f3e5eabb48ca3fdc4b7aafba6facc2722ff3abab Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Thu, 24 Nov 2022 23:37:37 +0100 Subject: [PATCH 3/3] Update doc/whatsnew/fragments/7821.bugfix --- doc/whatsnew/fragments/7821.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/7821.bugfix b/doc/whatsnew/fragments/7821.bugfix index 30ed305e1e..af48814db7 100644 --- a/doc/whatsnew/fragments/7821.bugfix +++ b/doc/whatsnew/fragments/7821.bugfix @@ -1,3 +1,3 @@ -Fixes bug that resulted in crash during ``unnecessary_list_index_lookup`` check. +Fixes a crash in the ``unnecessary_list_index_lookup`` check when using ``enumerate`` with ``start`` and a class attribute. Closes #7821