From 5f19cd5a12c25c6f0e02566ccfcde19c6fee3674 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 20 Jul 2024 11:56:45 -0400 Subject: [PATCH] Fix a crash when a subclass extends `__slots__` (#9817) (#9822) (cherry picked from commit 8e18fc012a5fcbd6cfe2b719e35ecd995da9f4bf) Co-authored-by: Pierre Sassoulas --- doc/whatsnew/fragments/9814.bugfix | 3 +++ pylint/checkers/classes/class_checker.py | 6 +++++- tests/functional/s/slots_checks.py | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 doc/whatsnew/fragments/9814.bugfix diff --git a/doc/whatsnew/fragments/9814.bugfix b/doc/whatsnew/fragments/9814.bugfix new file mode 100644 index 0000000000..0d635e6bfd --- /dev/null +++ b/doc/whatsnew/fragments/9814.bugfix @@ -0,0 +1,3 @@ +Fix a crash when a subclass extends ``__slots__``. + +Closes #9814 diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py index ffe47ab156..c3e1a081c8 100644 --- a/pylint/checkers/classes/class_checker.py +++ b/pylint/checkers/classes/class_checker.py @@ -1486,7 +1486,11 @@ def _check_slots(self, node: nodes.ClassDef) -> None: if "__slots__" not in node.locals: return - for slots in node.ilookup("__slots__"): + try: + inferred_slots = tuple(node.ilookup("__slots__")) + except astroid.InferenceError: + return + for slots in inferred_slots: # check if __slots__ is a valid type if isinstance(slots, util.UninferableBase): continue diff --git a/tests/functional/s/slots_checks.py b/tests/functional/s/slots_checks.py index 2c22e968ed..ddef0fb94c 100644 --- a/tests/functional/s/slots_checks.py +++ b/tests/functional/s/slots_checks.py @@ -128,3 +128,17 @@ class Parent: class ChildNotAffectedByValueInSlot(Parent): __slots__ = ('first', ) + + +# https://github.com/pylint-dev/pylint/issues/9814 +class SlotsManipulationTest: + __slots__ = ["a", "b", "c"] + + +class TestChild(SlotsManipulationTest): + __slots__ += ["d", "e", "f"] # pylint: disable=undefined-variable + + +t = TestChild() + +print(t.__slots__)