|
|
301142d |
From 61b5a758a2a03d3e22cdcb7b9420e71c3f0a2d77 Mon Sep 17 00:00:00 2001
|
|
|
301142d |
From: Caio Carrara <ccarrara@redhat.com>
|
|
|
301142d |
Date: Tue, 21 Aug 2018 13:06:17 -0300
|
|
|
301142d |
Subject: [PATCH] Fix is_subclass_of when one param is not ClassDef
|
|
|
301142d |
|
|
|
301142d |
Currently the function `checkers.utils.is_subclass_of()` is raising a
|
|
|
301142d |
`NoneType object has no attribute name` exception when only one of the
|
|
|
301142d |
arguments (nodes) is `None`.
|
|
|
301142d |
|
|
|
301142d |
This change fix that updating the function to make sure all params are
|
|
|
301142d |
an instance of `astroid.ClassDef` before the proper check
|
|
|
301142d |
---
|
|
|
301142d |
CONTRIBUTORS.txt | 2 ++
|
|
|
301142d |
ChangeLog | 3 +++
|
|
|
301142d |
doc/whatsnew/2.2.rst | 2 +-
|
|
|
301142d |
pylint/checkers/utils.py | 3 ++-
|
|
|
301142d |
pylint/test/unittest_checkers_utils.py | 36 ++++++++++++++++++++++++++
|
|
|
301142d |
5 files changed, 44 insertions(+), 2 deletions(-)
|
|
|
301142d |
|
|
|
301142d |
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
|
|
|
301142d |
index 1e4c47745..7cd2de4a7 100644
|
|
|
301142d |
--- a/CONTRIBUTORS.txt
|
|
|
301142d |
+++ b/CONTRIBUTORS.txt
|
|
|
301142d |
@@ -219,3 +219,5 @@ contributors:
|
|
|
301142d |
* Marcus Näslund (naslundx): contributor
|
|
|
301142d |
|
|
|
301142d |
* Natalie Serebryakova: contributor
|
|
|
301142d |
+
|
|
|
301142d |
+* Caio Carrara: contributor
|
|
|
301142d |
diff --git a/ChangeLog b/ChangeLog
|
|
|
301142d |
index 8bb81581c..8439abf6a 100644
|
|
|
301142d |
--- a/ChangeLog
|
|
|
301142d |
+++ b/ChangeLog
|
|
|
301142d |
@@ -44,6 +44,9 @@ Release date: TBA
|
|
|
301142d |
* Consider tuples in exception handler for ``try-except-raise``.
|
|
|
301142d |
Close #2389
|
|
|
301142d |
|
|
|
301142d |
+ * Fix astroid.ClassDef check in checkers.utils.is_subclass_of
|
|
|
301142d |
+
|
|
|
301142d |
+
|
|
|
301142d |
What's New in Pylint 2.1.1?
|
|
|
301142d |
===========================
|
|
|
301142d |
|
|
|
301142d |
diff --git a/doc/whatsnew/2.2.rst b/doc/whatsnew/2.2.rst
|
|
|
301142d |
index bd388ae4c..a2d5facc6 100644
|
|
|
301142d |
--- a/doc/whatsnew/2.2.rst
|
|
|
301142d |
+++ b/doc/whatsnew/2.2.rst
|
|
|
301142d |
@@ -23,4 +23,4 @@ Other Changes
|
|
|
301142d |
|
|
|
301142d |
* Fix false positive ``undefined-variable`` and ``used-before-assignment`` with nonlocal keyword usage.
|
|
|
301142d |
|
|
|
301142d |
-
|
|
|
301142d |
+* Fix exceptions being raised when one of the params is not a ClassDef in checkers.utils.is_subclass_of.
|
|
|
301142d |
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
|
|
|
301142d |
index 2add31f67..8ac9b1cce 100644
|
|
|
301142d |
--- a/pylint/checkers/utils.py
|
|
|
301142d |
+++ b/pylint/checkers/utils.py
|
|
|
301142d |
@@ -22,6 +22,7 @@
|
|
|
301142d |
# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
|
|
|
301142d |
# Copyright (c) 2018 Ville Skyttä <ville.skytta@upcloud.com>
|
|
|
301142d |
# Copyright (c) 2018 Brian Shaginaw <brian.shaginaw@warbyparker.com>
|
|
|
301142d |
+# Copyright (c) 2018 Caio Carrara <ccarrara@redhat.com>
|
|
|
301142d |
|
|
|
301142d |
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
301142d |
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
|
|
|
301142d |
@@ -1045,7 +1046,7 @@ def is_subclass_of(node_a: astroid.ClassDef,
|
|
|
301142d |
:returns: True if node_a is derived from node_b. False otherwise.
|
|
|
301142d |
:rtype: bool
|
|
|
301142d |
"""
|
|
|
301142d |
- if not any(isinstance(node, astroid.ClassDef) for node in (node_a, node_b)):
|
|
|
301142d |
+ if not all(isinstance(node, astroid.ClassDef) for node in (node_a, node_b)):
|
|
|
301142d |
return False
|
|
|
301142d |
|
|
|
301142d |
return node_b.name in {base.name for base in node_a.bases}
|
|
|
301142d |
diff --git a/pylint/test/unittest_checkers_utils.py b/pylint/test/unittest_checkers_utils.py
|
|
|
301142d |
index 4d3f46a3e..b9af5ba16 100644
|
|
|
301142d |
--- a/pylint/test/unittest_checkers_utils.py
|
|
|
301142d |
+++ b/pylint/test/unittest_checkers_utils.py
|
|
|
301142d |
@@ -4,6 +4,7 @@
|
|
|
301142d |
# Copyright (c) 2014 Arun Persaud <arun@nubati.net>
|
|
|
301142d |
# Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
|
|
|
301142d |
# Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
|
|
|
301142d |
+# Copyright (c) 2018 Caio Carrara <ccarrara@redhat.com>
|
|
|
301142d |
|
|
|
301142d |
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
301142d |
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
|
|
|
301142d |
@@ -102,3 +103,38 @@ def test_node_ignores_exception():
|
|
|
301142d |
assert not utils.node_ignores_exception(nodes[1], ZeroDivisionError)
|
|
|
301142d |
assert utils.node_ignores_exception(nodes[2], ZeroDivisionError)
|
|
|
301142d |
assert not utils.node_ignores_exception(nodes[3], ZeroDivisionError)
|
|
|
301142d |
+
|
|
|
301142d |
+
|
|
|
301142d |
+def test_is_sublcass_of_node_b_derived_from_node_a():
|
|
|
301142d |
+ nodes = astroid.extract_node("""
|
|
|
301142d |
+ class Superclass: #@
|
|
|
301142d |
+ pass
|
|
|
301142d |
+
|
|
|
301142d |
+ class Subclass(Superclass): #@
|
|
|
301142d |
+ pass
|
|
|
301142d |
+ """)
|
|
|
301142d |
+ assert utils.is_subclass_of(nodes[1], nodes[0])
|
|
|
301142d |
+
|
|
|
301142d |
+
|
|
|
301142d |
+def test_is_sublcass_of_node_b_not_derived_from_node_a():
|
|
|
301142d |
+ nodes = astroid.extract_node("""
|
|
|
301142d |
+ class OneClass: #@
|
|
|
301142d |
+ pass
|
|
|
301142d |
+
|
|
|
301142d |
+ class AnotherClass: #@
|
|
|
301142d |
+ pass
|
|
|
301142d |
+ """)
|
|
|
301142d |
+ assert not utils.is_subclass_of(nodes[1], nodes[0])
|
|
|
301142d |
+
|
|
|
301142d |
+
|
|
|
301142d |
+def test_is_sublcass_of_one_param_is_not_classdef():
|
|
|
301142d |
+ node = astroid.extract_node("""
|
|
|
301142d |
+ class OneClass: #@
|
|
|
301142d |
+ pass
|
|
|
301142d |
+ """)
|
|
|
301142d |
+ assert not utils.is_subclass_of(None, node)
|
|
|
301142d |
+ assert not utils.is_subclass_of(node, None)
|
|
|
301142d |
+
|
|
|
301142d |
+
|
|
|
301142d |
+def test_is_sublcass_of_both_params_are_not_classdef():
|
|
|
301142d |
+ assert not utils.is_subclass_of(None, None)
|