Skip to content

Commit 0599016

Browse files
redefined-slots-in-subclass crash when slot type is neither a string or constant node (#6112)
* Suppress `invalid-slots-object` in the function tests for `redefined-slots-in-subclass` Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent c48c45a commit 0599016

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Release date: TBA
2424
* functions & classes which contain both a docstring and an ellipsis.
2525
* A body which contains an ellipsis ``nodes.Expr`` node & at least one other statement.
2626

27+
* Fix crash for ``redefined-slots-in-subclass`` when the type of the slot is not a const or a string.
28+
29+
Closes #6100
30+
2731
* Only raise ``not-callable`` when all the inferred values of a property are not callable.
2832

2933
Closes #5931

pylint/checkers/classes/class_checker.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,9 @@ def _check_redefined_slots(
13811381
slots_names.append(slot.value)
13821382
else:
13831383
inferred_slot = safe_infer(slot)
1384-
if inferred_slot:
1385-
slots_names.append(inferred_slot.value)
1384+
inferred_slot_value = getattr(inferred_slot, "value", None)
1385+
if isinstance(inferred_slot_value, str):
1386+
slots_names.append(inferred_slot_value)
13861387

13871388
# Slots of all parent classes
13881389
ancestors_slots_names = {

tests/functional/r/redefined/redefined_slots.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Checks that a subclass does not redefine a slot which has been defined in a parent class."""
22

3-
# pylint: disable=too-few-public-methods
3+
# pylint: disable=too-few-public-methods, invalid-slots-object
44

55
from collections import deque
66

@@ -31,3 +31,9 @@ class Subclass3(Base, Base2):
3131
Redefining the `i`, `j`, `k` slot already defined in `Base2`
3232
"""
3333
__slots__ = ("a", "b", "c", "i", "j", "k", "l", "m", "n") # [redefined-slots-in-subclass]
34+
35+
36+
# https://github.com/PyCQA/pylint/issues/6100
37+
class MyClass:
38+
"""No crash when the type of the slot is not a Const or a str"""
39+
__slots__ = [str]

0 commit comments

Comments
 (0)