aboutsummaryrefslogtreecommitdiff
path: root/tests/functional/m/member/member_checks.py
blob: c1b6a22fbaebd0212203a586a547bbb603f807e5 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# pylint: disable=missing-docstring,no-self-use,too-few-public-methods,bare-except,broad-except, useless-object-inheritance, unused-private-member
# pylint: disable=using-constant-test,expression-not-assigned, assigning-non-slot, unused-variable,pointless-statement, wrong-import-order, wrong-import-position,import-outside-toplevel
from __future__ import print_function
class Provider(object):
    """provide some attributes and method"""
    cattr = 4
    def __init__(self):
        self.attr = 4
    def method(self, val):
        """impressive method"""
        return self.attr * val
    def hophop(self):
        """hop method"""
        print('hop hop hop', self)


class Client(object):
    """use provider class"""

    def __init__(self):
        self._prov = Provider()
        self._prov_attr = Provider.cattr
        self._prov_attr2 = Provider.cattribute  # [no-member]
        self.set_later = 0

    def set_set_later(self, value):
        """set set_later attribute (introduce an inference ambiguity)"""
        self.set_later = value

    def use_method(self):
        """use provider's method"""
        self._prov.hophop()
        self._prov.hophophop()  # [no-member]

    def use_attr(self):
        """use provider's attr"""
        print(self._prov.attr)
        print(self._prov.attribute)  # [no-member]

    def debug(self):
        """print debug information"""
        print(self.__class__.__name__)
        print(self.__doc__)
        print(self.__dict__)
        print(self.__module__)

    def test_bt_types(self):
        """test access to unexistant member of builtin types"""
        lis = []
        lis.apppend(self)  # [no-member]
        dic = {}
        dic.set(self)  # [no-member]
        tup = ()
        tup.append(self)  # [no-member]
        string = 'toto'
        print(string.loower())  # [no-member]
        integer = 1
        print(integer.whatever)  # [no-member]

    def test_no_false_positives(self):
        none = None
        print(none.whatever)
        # No misssing in the parents.
        super().misssing() # [no-member]


class Mixin(object):
    """No no-member should be emitted for mixins."""

class Getattr(object):
    """no-member shouldn't be emitted for classes with dunder getattr."""

    def __getattr__(self, attr):
        return self.__dict__[attr]


class Getattribute(object):
    """no-member shouldn't be emitted for classes with dunder getattribute."""

    def __getattribute__(self, attr):
        return 42

print(object.__init__)
print(property.__init__)
print(Client().set_later.lower())
print(Mixin().nanana())
print(Getattr().nananan())
print(Getattribute().batman())

try:
    Client().missing_method()
except AttributeError:
    pass

try:
    Client().indeed() # [no-member]
except ImportError:
    pass

try:
    Client.missing()
except AttributeError:
    Client.missing() # [no-member]

try:
    Client.missing()
except AttributeError:
    try:
        Client.missing() # [no-member]
    except ValueError:
        pass

try:
    if Client:
        Client().missing()
except AttributeError:
    pass

try:
    Client().indeed()
except AttributeError:
    try:
        Client.missing() # [no-member]
    except Exception:
        pass


class SuperChecks(str, str): # pylint: disable=duplicate-bases
    """Don't fail when the MRO is invalid."""
    def test(self):
        super().lalala()

type(Client()).ala # [no-member]
type({}).bala # [no-member]
type('').portocala # [no-member]


def socket_false_positive():
    """Test a regression
    Version used:

    - Pylint 0.10.0
    - Logilab common 0.15.0
    - Logilab astroid 0.15.1

    False E1101 positive, line 23:
    Instance of '_socketobject' has no 'connect' member
    """

    import socket
    sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sckt.connect(('127.0.0.1', 80))
    sckt.close()


def no_conjugate_member(magic_flag):
    """should not raise E1101 on something.conjugate"""
    if magic_flag:
        something = 1.0
    else:
        something = 1.0j
    if isinstance(something, float):
        return something
    return something.conjugate()


class NoDunderNameInInstance(object):
    """Emit a warning when accessing __name__ from an instance."""
    def __init__(self):
        self.var = self.__name__ # [no-member]


class InvalidAccessBySlots(object):
    __slots__ = ('a', )
    def __init__(self):
        var = self.teta # [no-member]
        self.teta = 24


class MetaWithDynamicGetattr(type):

    def __getattr__(cls, attr):
        return attr


class SomeClass(object, metaclass=MetaWithDynamicGetattr):
    pass


SomeClass.does_not_exist

class ClassWithMangledAttribute(object):
    def __init__(self):
        self.name = 'Bug1643'
    def __bar(self):
        print(self.name + "xD")

ClassWithMangledAttribute()._ClassWithMangledAttribute__bar()  # pylint: disable=protected-access


import enum


class Cls(enum.IntEnum):
    BAR = 0


SOME_VALUE = Cls.BAZ  # [no-member]



# Does not crash when inferring the `append` attribute on the slice object
class SomeClassUsingSlice:
    def __init__(self, flag):
        if flag:
            self.attribute = slice(None)
        else:
            self.attribute = []
            self.attribute.append(1)

from enum import Enum
class Animal(Enum):
    ANT = 1
    BEE = 2
    CAT = 3
    DOG = 4
# To test false positive no-member on Enum.__members__.items()
for itm in Animal.__members__.items():
    print(itm)
for keyy in Animal.__members__.keys():  # pylint: disable=consider-iterating-dictionary
    print(keyy)
for vall in Animal.__members__.values():
    print(vall)