summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-12-29 10:20:00 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2020-12-29 10:27:39 +0100
commit5a9ea7ce8884c3040a21b91b45ab0a7951439f0f (patch)
tree86349411f845c508605ba231352a73241952c32d
parent17325f2f7c8cf43f7ae61ba6726b5117cc11066c (diff)
downloadpylint-git-fix-crash-in-attribute-check.tar.gz
Fix a crash in `undefined-variable` caused by chained attributes in metaclassfix-crash-in-attribute-check
Close #3742
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/variables.py5
-rw-r--r--tests/functional/u/undefined_variable_crash_on_attribute.py6
3 files changed, 14 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 97ebf5f55..9dfd94060 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -28,6 +28,10 @@ What's New in Pylint 2.6.1?
===========================
Release date: TBA
+* Fix a crash in `undefined-variable` caused by chained attributes in metaclass
+
+ Close #3742
+
* Fix false positive for `not-async-context-manager` when `contextlib.asynccontextmanager` is used
Close #3862
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index b6a2978a5..986f76fd2 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -2037,7 +2037,10 @@ class VariablesChecker(BaseChecker):
if isinstance(klass._metaclass, astroid.Name):
name = klass._metaclass.name
elif isinstance(klass._metaclass, astroid.Attribute) and klass._metaclass.expr:
- name = klass._metaclass.expr.name
+ attr = klass._metaclass.expr
+ while not isinstance(attr, astroid.Name):
+ attr = attr.expr
+ name = attr.name
elif metaclass:
name = metaclass.root().name
diff --git a/tests/functional/u/undefined_variable_crash_on_attribute.py b/tests/functional/u/undefined_variable_crash_on_attribute.py
new file mode 100644
index 000000000..571efab1c
--- /dev/null
+++ b/tests/functional/u/undefined_variable_crash_on_attribute.py
@@ -0,0 +1,6 @@
+# pylint: disable=import-error,missing-module-docstring,missing-class-docstring,too-few-public-methods
+import unknown
+
+
+class Cls(metaclass=unknown.path.MetaFoo):
+ pass