1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
""" Checks that classes uses valid __slots__ """
# pylint: disable=too-few-public-methods, missing-docstring, useless-object-inheritance
# pylint: disable=using-constant-test, wrong-import-position, no-else-return, line-too-long, unused-private-member
from collections import deque
def func():
if True:
return ("a", "b", "c")
else:
return [str(var) for var in range(3)]
class NotIterable(object):
def __iter_(self):
""" do nothing """
class Good(object):
__slots__ = ()
class SecondGood(object):
__slots__ = []
class ThirdGood(object):
__slots__ = ['a']
class FourthGood(object):
__slots__ = (f'a{i}' for i in range(10))
class FifthGood(object):
__slots__ = deque(["a", "b", "c"])
class SixthGood(object):
__slots__ = {"a": "b", "c": "d"}
class SeventhGood:
"""type-annotated __slots__ with no value"""
__slots__: str
class EigthGood:
"""Multiple __slots__ declared in the class"""
x = 1
if x:
__slots__: str
else:
__slots__ = ("y",)
class Bad(object): # [invalid-slots]
__slots__ = list
class SecondBad(object): # [invalid-slots]
__slots__ = 1
class ThirdBad(object):
__slots__ = ('a', 2) # [invalid-slots-object]
class FourthBad(object): # [invalid-slots]
__slots__ = NotIterable()
class FifthBad(object):
__slots__ = ("a", "b", "") # [invalid-slots-object]
class SixthBad(object): # [single-string-used-for-slots]
__slots__ = "a"
class SeventhBad(object): # [single-string-used-for-slots]
__slots__ = ('foo')
class EighthBad(object): # [single-string-used-for-slots]
__slots__ = deque.__name__
class NinthBad(object):
__slots__ = [str] # [invalid-slots-object]
class TenthBad(object):
__slots__ = [1 + 2 + 3] # [invalid-slots-object]
class EleventhBad: # [invalid-slots]
__slots__ = None
class TwelfthBad: # [invalid-slots]
"""One valid & one invalid __slots__ value"""
x = 1
if x:
__slots__ = ("y",)
else:
__slots__ = None
class PotentiallyGood(object):
__slots__ = func()
class PotentiallySecondGood(object):
__slots__ = ('a', deque.__name__)
class Metaclass(type):
def __iter__(cls):
for value in range(10):
yield str(value)
class IterableClass(object, metaclass=Metaclass):
pass
class PotentiallyThirdGood(object):
__slots__ = IterableClass
class PotentiallyFourthGood(object):
__slots__ = Good.__slots__
class ValueInSlotConflict(object):
__slots__ = ('first', 'second', 'third', 'fourth') # [class-variable-slots-conflict, class-variable-slots-conflict, class-variable-slots-conflict]
first = None
@property
def third(self):
return 42
def fourth(self):
return self.third
class Parent(object):
first = 42
class ChildNotAffectedByValueInSlot(Parent):
__slots__ = ('first', )
|