From cff6d39b0ce57299d51f2321c0b6e6e761d9feeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:22:19 +0200 Subject: Fix crash for ``protected-access`` on (outer)-class traversal (#5305) * Fix crash for ``protected-access`` on (outer)-class traversal Co-authored-by: Pierre Sassoulas --- ChangeLog | 2 ++ doc/whatsnew/2.12.rst | 2 ++ pylint/checkers/classes.py | 2 +- .../r/regression_02/regression_protected_access.py | 19 +++++++++++++++++++ .../r/regression_02/regression_protected_access.txt | 1 + 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/functional/r/regression_02/regression_protected_access.py create mode 100644 tests/functional/r/regression_02/regression_protected_access.txt diff --git a/ChangeLog b/ChangeLog index a4998baf8..4a8b2eb81 100644 --- a/ChangeLog +++ b/ChangeLog @@ -175,6 +175,8 @@ Release date: TBA Follow-up in #5259 +* Fix crash for ``protected-access`` on (outer) class traversal + * Added new checker ``useless-with-lock`` to find incorrect usage of with statement and threading module locks. Emitted when ``with threading.Lock():`` is used instead of ``with lock_instance:``. diff --git a/doc/whatsnew/2.12.rst b/doc/whatsnew/2.12.rst index b69af3289..97957d7c5 100644 --- a/doc/whatsnew/2.12.rst +++ b/doc/whatsnew/2.12.rst @@ -179,6 +179,8 @@ Other Changes Closes #4580 +* Fix crash for ``protected-access`` on (outer) class traversal + * Make yn validator case insensitive, to allow for ``True`` and ``False`` in config files. * The last version compatible with python '3.6.0' and '3.6.1' is pylint '2.9.3'. We did not diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index cd3e1178c..319a37f73 100644 --- a/pylint/checkers/classes.py +++ b/pylint/checkers/classes.py @@ -1624,7 +1624,7 @@ a metaclass class method.", parents_callee = callee.split(".") parents_callee.reverse() for callee in parents_callee: - if callee != outer_klass.name: + if not outer_klass or callee != outer_klass.name: inside_klass = False break diff --git a/tests/functional/r/regression_02/regression_protected_access.py b/tests/functional/r/regression_02/regression_protected_access.py new file mode 100644 index 000000000..df36f6795 --- /dev/null +++ b/tests/functional/r/regression_02/regression_protected_access.py @@ -0,0 +1,19 @@ +"""Test for the regression on (outer)-class traversal for private methods +MyClass does not have an outerclass which previously crashed the protected-access check +""" +# pylint: disable=too-few-public-methods + + +class MyClass: + """Test class""" + + @staticmethod + def _a_private_method(): + """Private method that references the class itself""" + return MySecondClass.MyClass._a_private_method() # [protected-access] + + +class MySecondClass: + """Class that uses MyClass""" + + MyClass = MyClass diff --git a/tests/functional/r/regression_02/regression_protected_access.txt b/tests/functional/r/regression_02/regression_protected_access.txt new file mode 100644 index 000000000..e3577b4b1 --- /dev/null +++ b/tests/functional/r/regression_02/regression_protected_access.txt @@ -0,0 +1 @@ +protected-access:13:15:MyClass._a_private_method:Access to a protected member _a_private_method of a client class:HIGH -- cgit v1.2.1