Skip to content

Commit 204b54b

Browse files
committed
class attrs should not emit assigning-non-slot msg
1 parent 2ae0e98 commit 204b54b

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false positive ``assigning-non-slot`` when a class attribute is re-assigned.
2+
3+
Closes #6001

pylint/checkers/classes/class_checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,6 @@ def _check_in_slots(self, node: nodes.AssignAttr) -> None:
16221622
for ancestor in klass.ancestors()
16231623
):
16241624
return
1625-
16261625
if not any(slot.value == node.attrname for slot in slots):
16271626
# If we have a '__dict__' in slots, then
16281627
# assigning any name is valid.
@@ -1631,6 +1630,8 @@ def _check_in_slots(self, node: nodes.AssignAttr) -> None:
16311630
# Properties circumvent the slots mechanism,
16321631
# so we should not emit a warning for them.
16331632
return
1633+
if self._is_class_attribute(node.attrname, klass):
1634+
return
16341635
if node.attrname in klass.locals:
16351636
for local_name in klass.locals.get(node.attrname):
16361637
statement = local_name.statement(future=True)

tests/functional/a/assigning/assigning_non_slot.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def dont_emit_for_descriptors():
129129
# This should not emit, because attr is
130130
# a data descriptor
131131
inst.data_descriptor = 'foo'
132-
inst.non_data_descriptor = 'lala' # [assigning-non-slot]
132+
inst.non_data_descriptor = 'lala'
133133

134134

135135
class ClassWithSlots:
@@ -147,7 +147,7 @@ class ClassReassingingInvalidLayoutClass:
147147
__slots__ = []
148148

149149
def release(self):
150-
self.__class__ = ClassWithSlots # [assigning-non-slot]
150+
self.__class__ = ClassWithSlots
151151

152152

153153
# pylint: disable=attribute-defined-outside-init
@@ -200,3 +200,15 @@ def dont_emit_for_defined_setattr():
200200

201201
child = ClassWithParentDefiningSetattr()
202202
child.non_existent = "non-existent"
203+
204+
class ColorCls:
205+
__slots__ = ()
206+
COLOR = "red"
207+
208+
209+
class Repro(ColorCls):
210+
__slots__ = ()
211+
212+
213+
repro = Repro()
214+
Repro.COLOR = "blue"
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
assigning-non-slot:18:8:18:20:Bad.__init__:Assigning to attribute 'missing' not defined in class slots:UNDEFINED
22
assigning-non-slot:26:8:26:20:Bad2.__init__:Assigning to attribute 'missing' not defined in class slots:UNDEFINED
33
assigning-non-slot:36:8:36:20:Bad3.__init__:Assigning to attribute 'missing' not defined in class slots:UNDEFINED
4-
assigning-non-slot:132:4:132:28:dont_emit_for_descriptors:Assigning to attribute 'non_data_descriptor' not defined in class slots:UNDEFINED
5-
assigning-non-slot:150:8:150:22:ClassReassingingInvalidLayoutClass.release:Assigning to attribute '__class__' not defined in class slots:UNDEFINED

0 commit comments

Comments
 (0)