summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-03-08 13:12:36 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-03-08 17:55:44 +0100
commit0f1245c2959f16dd68a2f7cf191c3cee0fcc08c2 (patch)
treef03cbe4ad3241c677665e539d3befa280fa92ef8
parentba4941b96c12f32d1eb91868c1ce040f3f7f48e2 (diff)
downloadpylint-git-0f1245c2959f16dd68a2f7cf191c3cee0fcc08c2.tar.gz
Fix astroid.Inference error for undefined-variables with len()
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/refactoring/len_checker.py6
-rw-r--r--tests/functional/l/len_checks.py9
-rw-r--r--tests/functional/l/len_checks.txt2
4 files changed, 20 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 030078157..e6a1a8e74 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -48,6 +48,10 @@ Release date: TBA
Closes #4181
+* Fix astroid.Inference error for undefined-variables with ``len()```
+
+ Closes #4215
+
What's New in Pylint 2.7.2?
===========================
diff --git a/pylint/checkers/refactoring/len_checker.py b/pylint/checkers/refactoring/len_checker.py
index 35157dffd..c11d74b30 100644
--- a/pylint/checkers/refactoring/len_checker.py
+++ b/pylint/checkers/refactoring/len_checker.py
@@ -73,7 +73,11 @@ class LenChecker(checkers.BaseChecker):
# 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())
+ try:
+ instance = next(len_arg.infer())
+ except astroid.InferenceError:
+ # Probably undefined-varible, abort check
+ return
mother_classes = self.base_classes_of_node(instance)
affected_by_pep8 = any(
t in mother_classes for t in ["str", "tuple", "list", "set"]
diff --git a/tests/functional/l/len_checks.py b/tests/functional/l/len_checks.py
index eb10b6a3b..2cbefcbe7 100644
--- a/tests/functional/l/len_checks.py
+++ b/tests/functional/l/len_checks.py
@@ -175,3 +175,12 @@ def github_issue_1879():
# assert len(function_returning_generator(z))
# assert len(function_returning_comprehension(z))
# assert len(function_returning_function(z))
+
+
+def github_issue_4215():
+ # Test undefined variables
+ # https://github.com/PyCQA/pylint/issues/4215
+ if len(undefined_var): # [undefined-variable]
+ pass
+ if len(undefined_var2[0]): # [undefined-variable]
+ pass
diff --git a/tests/functional/l/len_checks.txt b/tests/functional/l/len_checks.txt
index 135e886c9..f1b96e477 100644
--- a/tests/functional/l/len_checks.txt
+++ b/tests/functional/l/len_checks.txt
@@ -21,3 +21,5 @@ len-as-condition:128:11:github_issue_1879:Do not use `len(SEQUENCE)` without com
len-as-condition:129:11:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:130:11:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
len-as-condition:171:11:github_issue_1879:Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty
+undefined-variable:183:11:github_issue_4215:Undefined variable 'undefined_var'
+undefined-variable:185:11:github_issue_4215:Undefined variable 'undefined_var2'