@@ -58,30 +58,32 @@ def visit_call(self, node):
58
58
# a len(S) call is used inside a test condition
59
59
# could be if, while, assert or if expression statement
60
60
# e.g. `if len(S):`
61
- if utils .is_call_of_name (node , "len" ):
62
- # the len() call could also be nested together with other
63
- # boolean operations, e.g. `if z or len(x):`
64
- parent = node .parent
65
- while isinstance (parent , astroid .BoolOp ):
66
- parent = parent .parent
67
- # we're finally out of any nested boolean operations so check if
68
- # this len() call is part of a test condition
69
- if utils .is_test_condition (node , parent ):
70
- len_arg = node .args [0 ]
71
- generator_or_comprehension = (ListComp , SetComp , DictComp , GeneratorExp )
72
- if isinstance (len_arg , generator_or_comprehension ):
73
- # The node is a generator or comprehension as in len([x for x in ...])
74
- self .add_message ("len-as-condition" , node = node )
75
- return
76
- instance = next (len_arg .infer ())
77
- mother_classes = self .base_classes_of_node (instance )
78
- affected_by_pep8 = any (
79
- t in mother_classes for t in ["str" , "tuple" , "list" , "set" ]
80
- )
81
- if "range" in mother_classes or (
82
- affected_by_pep8 and not self .instance_has_bool (instance )
83
- ):
84
- self .add_message ("len-as-condition" , node = node )
61
+ if not utils .is_call_of_name (node , "len" ):
62
+ return
63
+ # the len() call could also be nested together with other
64
+ # boolean operations, e.g. `if z or len(x):`
65
+ parent = node .parent
66
+ while isinstance (parent , astroid .BoolOp ):
67
+ parent = parent .parent
68
+ # we're finally out of any nested boolean operations so check if
69
+ # this len() call is part of a test condition
70
+ if not utils .is_test_condition (node , parent ):
71
+ return
72
+ len_arg = node .args [0 ]
73
+ generator_or_comprehension = (ListComp , SetComp , DictComp , GeneratorExp )
74
+ if isinstance (len_arg , generator_or_comprehension ):
75
+ # The node is a generator or comprehension as in len([x for x in ...])
76
+ self .add_message ("len-as-condition" , node = node )
77
+ return
78
+ instance = next (len_arg .infer ())
79
+ mother_classes = self .base_classes_of_node (instance )
80
+ affected_by_pep8 = any (
81
+ t in mother_classes for t in ["str" , "tuple" , "list" , "set" ]
82
+ )
83
+ if "range" in mother_classes or (
84
+ affected_by_pep8 and not self .instance_has_bool (instance )
85
+ ):
86
+ self .add_message ("len-as-condition" , node = node )
85
87
86
88
@staticmethod
87
89
def instance_has_bool (class_def : astroid .ClassDef ) -> bool :
0 commit comments