|
5 | 5 | from typing import List, Optional
|
6 | 6 |
|
7 | 7 | import astroid
|
| 8 | +from astroid import DictComp, GeneratorExp, ListComp, SetComp, Uninferable |
8 | 9 |
|
9 | 10 | from pylint import checkers, interfaces
|
10 | 11 | from pylint.checkers import utils
|
@@ -88,13 +89,13 @@ def visit_call(self, node):
|
88 | 89 | # we're finally out of any nested boolean operations so check if
|
89 | 90 | # this len() call is part of a test condition
|
90 | 91 | if _is_test_condition(node, parent):
|
91 |
| - try: |
92 |
| - instance = next(node.args[0].infer()) |
93 |
| - except astroid.InferenceError: |
94 |
| - # Inference error happens for list comprehension, dict comprehension, |
95 |
| - # set comprehension and generators (like range) |
| 92 | + len_arg = node.args[0] |
| 93 | + generator_or_comprehension = (ListComp, SetComp, DictComp, GeneratorExp) |
| 94 | + if isinstance(len_arg, generator_or_comprehension): |
| 95 | + # The node is a generator or comprehension as in len([x for x in ...]) |
96 | 96 | self.add_message("len-as-condition", node=node)
|
97 | 97 | return
|
| 98 | + instance = next(len_arg.infer()) |
98 | 99 | mother_classes = self.base_classes_of_node(instance)
|
99 | 100 | affected_by_pep8 = any(
|
100 | 101 | t in mother_classes for t in ["str", "tuple", "list", "set"]
|
|
0 commit comments