Skip to content

Commit

Permalink
Treat generator and comprehension more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Sep 14, 2020
1 parent c0e0be5 commit d69f819
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pylint/checkers/refactoring/len_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import List, Optional

import astroid
from astroid import Uninferable
from astroid import DictComp, GeneratorExp, ListComp, SetComp, Uninferable

from pylint import checkers, interfaces
from pylint.checkers import utils
Expand Down Expand Up @@ -89,13 +89,13 @@ def visit_call(self, node):
# we're finally out of any nested boolean operations so check if
# this len() call is part of a test condition
if _is_test_condition(node, parent):
try:
instance = next(node.args[0].infer())
except astroid.InferenceError:
# Inference error happens for list comprehension, dict comprehension,
# set comprehension and generators (like range)
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"]
Expand Down

0 comments on commit d69f819

Please sign in to comment.