Skip to content

Commit

Permalink
redefined-slots-in-subclass crash when slot type is neither a str…
Browse files Browse the repository at this point in the history
…ing or constant node (#6112)

* Suppress `invalid-slots-object` in the function tests for `redefined-slots-in-subclass`

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
mbyrnepr2 and Pierre-Sassoulas committed Apr 4, 2022
1 parent c48c45a commit 0599016
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Release date: TBA
* functions & classes which contain both a docstring and an ellipsis.
* A body which contains an ellipsis ``nodes.Expr`` node & at least one other statement.

* Fix crash for ``redefined-slots-in-subclass`` when the type of the slot is not a const or a string.

Closes #6100

* Only raise ``not-callable`` when all the inferred values of a property are not callable.

Closes #5931
Expand Down
5 changes: 3 additions & 2 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,8 +1381,9 @@ def _check_redefined_slots(
slots_names.append(slot.value)
else:
inferred_slot = safe_infer(slot)
if inferred_slot:
slots_names.append(inferred_slot.value)
inferred_slot_value = getattr(inferred_slot, "value", None)
if isinstance(inferred_slot_value, str):
slots_names.append(inferred_slot_value)

# Slots of all parent classes
ancestors_slots_names = {
Expand Down
8 changes: 7 additions & 1 deletion tests/functional/r/redefined/redefined_slots.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Checks that a subclass does not redefine a slot which has been defined in a parent class."""

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

from collections import deque

Expand Down Expand Up @@ -31,3 +31,9 @@ class Subclass3(Base, Base2):
Redefining the `i`, `j`, `k` slot already defined in `Base2`
"""
__slots__ = ("a", "b", "c", "i", "j", "k", "l", "m", "n") # [redefined-slots-in-subclass]


# https://github.com/PyCQA/pylint/issues/6100
class MyClass:
"""No crash when the type of the slot is not a Const or a str"""
__slots__ = [str]

0 comments on commit 0599016

Please sign in to comment.