summaryrefslogtreecommitdiff
path: root/pylint/test/functional/slots_checks.py
blob: 64a439f33819cf2665a2c60084feeaff4ca3c835 (plain)
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
""" Checks that classes uses valid __slots__ """

# pylint: disable=too-few-public-methods, missing-docstring, no-absolute-import, useless-object-inheritance
# pylint: disable=using-constant-test, wrong-import-position, no-else-return, line-too-long
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__ = ('a%s' % i for i in range(10))

class FifthGood(object):
    __slots__ = deque(["a", "b", "c"])

class SixthGood(object):
    __slots__ = {"a": "b", "c": "d"}

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 PotentiallyGood(object):
    __slots__ = func()

class PotentiallySecondGood(object):
    __slots__ = ('a', deque.__name__)


import six


class Metaclass(type):

    def __iter__(cls):
        for value in range(10):
            yield str(value)


@six.add_metaclass(Metaclass)
class IterableClass(object):
    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', )