Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / test / functional / assigning_non_slot.py @ 745

History | View | Annotate | Download (3.03 KB)

1
""" Checks assigning attributes not found in class slots
2
will trigger assigning-non-slot warning.
3
"""
4
# pylint: disable=too-few-public-methods, no-init, missing-docstring, no-absolute-import, import-error
5
from collections import deque
6

    
7
from missing import Unknown
8

    
9
class Empty(object):
10
    """ empty """
11

    
12
class Bad(object):
13
    """ missing not in slots. """
14

    
15
    __slots__ = ['member']
16

    
17
    def __init__(self):
18
        self.missing = 42 # [assigning-non-slot]
19

    
20
class Bad2(object):
21
    """ missing not in slots """
22
    __slots__ = [deque.__name__, 'member']
23

    
24
    def __init__(self):
25
        self.deque = 42
26
        self.missing = 42 # [assigning-non-slot]
27

    
28
class Bad3(Bad):
29
    """ missing not found in slots """
30

    
31
    __slots__ = ['component']
32

    
33
    def __init__(self):
34
        self.component = 42
35
        self.member = 24
36
        self.missing = 42 # [assigning-non-slot]
37
        super(Bad3, self).__init__()
38

    
39
class Good(Empty):
40
    """ missing not in slots, but Empty doesn't
41
    specify __slots__.
42
    """
43
    __slots__ = ['a']
44

    
45
    def __init__(self):
46
        self.missing = 42
47

    
48
class Good2(object):
49
    """ Using __dict__ in slots will be safe. """
50

    
51
    __slots__ = ['__dict__', 'comp']
52

    
53
    def __init__(self):
54
        self.comp = 4
55
        self.missing = 5
56

    
57
class PropertyGood(object):
58
    """ Using properties is safe. """
59

    
60
    __slots__ = ['tmp', '_value']
61

    
62
    @property
63
    def test(self):
64
        return self._value
65

    
66
    @test.setter
67
    def test(self, value):
68
        # pylint: disable=attribute-defined-outside-init
69
        self._value = value
70

    
71
    def __init__(self):
72
        self.test = 42
73

    
74
class PropertyGood2(object):
75
    """ Using properties in the body of the class is safe. """
76
    __slots__ = ['_value']
77

    
78
    def _getter(self):
79
        return self._value
80

    
81
    def _setter(self, value):
82
        # pylint: disable=attribute-defined-outside-init
83
        self._value = value
84

    
85
    test = property(_getter, _setter)
86

    
87
    def __init__(self):
88
        self.test = 24
89

    
90
class UnicodeSlots(object):
91
    """Using unicode objects in __slots__ is okay.
92

93
    On Python 3.3 onward, u'' is equivalent to '',
94
    so this test should be safe for both versions.
95
    """
96
    __slots__ = (u'first', u'second')
97

    
98
    def __init__(self):
99
        self.first = 42
100
        self.second = 24
101

    
102

    
103
class DataDescriptor(object):
104
    def __init__(self, name, default=''):
105
        self.__name = name
106
        self.__default = default
107

    
108
    def __get__(self, inst, cls):
109
        return getattr(inst, self.__name, self.__default)
110

    
111
    def __set__(self, inst, value):
112
        setattr(inst, self.__name, value)
113

    
114

    
115
class NonDataDescriptor(object):
116
    def __get__(self, inst, cls):
117
        return 42
118

    
119

    
120
class SlotsWithDescriptor(object):
121
    __slots__ = ['_err']
122
    data_descriptor = DataDescriptor('_err')
123
    non_data_descriptor = NonDataDescriptor()
124
    missing_descriptor = Unknown()
125

    
126

    
127
def dont_emit_for_descriptors():
128
    inst = SlotsWithDescriptor()
129
    # This should not emit, because attr is
130
    # a data descriptor
131
    inst.data_descriptor = 'foo'
132
    inst.non_data_descriptor = 'lala' # [assigning-non-slot]
133
    return