Skip to content

Commit

Permalink
Use premature return to simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Nov 29, 2020
1 parent 496bf1c commit e3ee0e6
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions pylint/checkers/refactoring/len_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,32 @@ def visit_call(self, node):
# a len(S) call is used inside a test condition
# could be if, while, assert or if expression statement
# e.g. `if len(S):`
if utils.is_call_of_name(node, "len"):
# the len() call could also be nested together with other
# boolean operations, e.g. `if z or len(x):`
parent = node.parent
while isinstance(parent, astroid.BoolOp):
parent = parent.parent
# we're finally out of any nested boolean operations so check if
# this len() call is part of a test condition
if utils.is_test_condition(node, parent):
len_arg = node.args[0]
generator_or_comprehension = (ListComp, SetComp, DictComp, GeneratorExp)
if isinstance(len_arg, generator_or_comprehension):
# The node is a generator or comprehension as in len([x for x in ...])
self.add_message("len-as-condition", node=node)
return
instance = next(len_arg.infer())
mother_classes = self.base_classes_of_node(instance)
affected_by_pep8 = any(
t in mother_classes for t in ["str", "tuple", "list", "set"]
)
if "range" in mother_classes or (
affected_by_pep8 and not self.instance_has_bool(instance)
):
self.add_message("len-as-condition", node=node)
if not utils.is_call_of_name(node, "len"):
return
# the len() call could also be nested together with other
# boolean operations, e.g. `if z or len(x):`
parent = node.parent
while isinstance(parent, astroid.BoolOp):
parent = parent.parent
# we're finally out of any nested boolean operations so check if
# this len() call is part of a test condition
if not utils.is_test_condition(node, parent):
return
len_arg = node.args[0]
generator_or_comprehension = (ListComp, SetComp, DictComp, GeneratorExp)
if isinstance(len_arg, generator_or_comprehension):
# The node is a generator or comprehension as in len([x for x in ...])
self.add_message("len-as-condition", node=node)
return
instance = next(len_arg.infer())
mother_classes = self.base_classes_of_node(instance)
affected_by_pep8 = any(
t in mother_classes for t in ["str", "tuple", "list", "set"]
)
if "range" in mother_classes or (
affected_by_pep8 and not self.instance_has_bool(instance)
):
self.add_message("len-as-condition", node=node)

@staticmethod
def instance_has_bool(class_def: astroid.ClassDef) -> bool:
Expand Down

0 comments on commit e3ee0e6

Please sign in to comment.