Skip to content

Commit

Permalink
Merge pull request #49 from tonybaloney/no_tuple_all
Browse files Browse the repository at this point in the history
Don't recommend `__all__` be a tuple
  • Loading branch information
tonybaloney authored Jan 10, 2024
2 parents 136408e + 83e7826 commit f1716c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions perflint/for_loop_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.",
),
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions perflint/list_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_list_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(): #@
Expand Down

0 comments on commit f1716c9

Please sign in to comment.