From 57feb0bfcbecf50671b982aebc826d5142868862 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Wed, 10 Jan 2024 17:04:37 +1100 Subject: [PATCH 1/2] Add clarity to the try in a loop rule --- perflint/for_loop_checker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/perflint/for_loop_checker.py b/perflint/for_loop_checker.py index f1f97f8..0b9cbb9 100644 --- a/perflint/for_loop_checker.py +++ b/perflint/for_loop_checker.py @@ -3,6 +3,8 @@ from astroid.helpers import safe_infer from pylint.checkers import BaseChecker from pylint.checkers import utils as checker_utils +from pylint.interfaces import INFERENCE + iterable_types = ( nodes.Tuple, @@ -141,7 +143,7 @@ class LoopInvariantChecker(BaseChecker): "Global name lookups in Python are slower than local names.", ), "R8203": ( - "Try..except blocks have an overhead. Avoid using them inside a loop unless you're using them for control-flow.", + "Try..except blocks have an overhead. Avoid using them inside a loop unless you're using them for control-flow. Rule only applies to Python < 3.11.", "loop-try-except-usage", "Avoid using try..except within a loop.", ), @@ -336,7 +338,7 @@ def visit_call(self, node: nodes.Call) -> None: @checker_utils.only_required_for_messages("loop-try-except-usage") def visit_tryexcept(self, node: nodes.Try) -> None: if self._loop_level > 0: - self.add_message("loop-try-except-usage", node=node) + self.add_message("loop-try-except-usage", node=node, confidence=INFERENCE) @checker_utils.only_required_for_messages("memoryview-over-bytes") def visit_subscript(self, node: nodes.Subscript) -> None: From 83e78262dcda0ed6c26a46c6b77a7551dcf03ea7 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Wed, 10 Jan 2024 17:33:08 +1100 Subject: [PATCH 2/2] Don't recommend all be a tuple --- perflint/list_checker.py | 2 ++ tests/test_list_checker.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/perflint/list_checker.py b/perflint/list_checker.py index 9ded539..068f225 100644 --- a/perflint/list_checker.py +++ b/perflint/list_checker.py @@ -29,6 +29,8 @@ def visit_assign(self, node: nodes.Assign): if len(node.targets) > 1: return if isinstance(node.targets[0], nodes.AssignName): + if node.targets[0].name == "__all__": + return self._lists_to_watch[-1][node.targets[0].name] = node.targets[0] def visit_module(self, node: nodes.Module): diff --git a/tests/test_list_checker.py b/tests/test_list_checker.py index 5c00383..0835f46 100644 --- a/tests/test_list_checker.py +++ b/tests/test_list_checker.py @@ -16,6 +16,15 @@ def test(): #@ with self.assertAddedMessage("use-tuple-over-list"): self.walk(test_func) + def test_const_all_be_ignored(self): + test_func = astroid.extract_node(""" + def test(): #@ + __all__ = [1,2,3,4] + """) + + with self.assertNoMessages(): + self.walk(test_func) + def test_mutated_list_by_method(self): test_func = astroid.extract_node(""" def test(): #@