summaryrefslogtreecommitdiff
path: root/tests/functional/l/len_checks.py
blob: ceeedb5493743e0fb5322a443ddaae9d7e13a805 (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
# pylint: disable=too-few-public-methods,import-error, missing-docstring, misplaced-comparison-constant
# pylint: disable=useless-super-delegation,wrong-import-position,invalid-name, wrong-import-order, condition-evals-to-constant

if len('TEST'):  # [len-as-condition]
    pass

if not len('TEST'):  # [len-as-condition]
    pass

z = []
if z and len(['T', 'E', 'S', 'T']):  # [len-as-condition]
    pass

if True or len('TEST'):  # [len-as-condition]
    pass

if len('TEST') == 0:  # Should be fine
    pass

if len('TEST') < 1:  # Should be fine
    pass

if len('TEST') <= 0:  # Should be fine
    pass

if 1 > len('TEST'):  # Should be fine
    pass

if 0 >= len('TEST'):  # Should be fine
    pass

if z and len('TEST') == 0:  # Should be fine
    pass

if 0 == len('TEST') < 10:  # Should be fine
    pass

if 0 < 1 <= len('TEST') < 10:  # Should be fine
    pass

if 10 > len('TEST') != 0:  # Should be fine
    pass

if 10 > len('TEST') > 1 > 0:  # Should be fine
    pass

if 0 <= len('TEST') < 100:  # Should be fine
    pass

if z or 10 > len('TEST') != 0:  # Should be fine
    pass

if z:
    pass
elif len('TEST'):  # [len-as-condition]
    pass

if z:
    pass
elif not len('TEST'):  # [len-as-condition]
    pass

while len('TEST'):  # [len-as-condition]
    pass

while not len('TEST'):  # [len-as-condition]
    pass

while z and len('TEST'):  # [len-as-condition]
    pass

while not len('TEST') and z:  # [len-as-condition]
    pass

assert len('TEST') > 0  # Should be fine

x = 1 if len('TEST') != 0 else 2  # Should be fine

f_o_o = len('TEST') or 42  # Should be fine

a = x and len(x)  # Should be fine

def some_func():
    return len('TEST') > 0  # Should be fine

def github_issue_1325():
    l = [1, 2, 3]
    length = len(l) if l else 0  # Should be fine
    return length

def github_issue_1331(*args):
    assert False, len(args)  # Should be fine

def github_issue_1331_v2(*args):
    assert len(args), args  # [len-as-condition]

def github_issue_1331_v3(*args):
    assert len(args) or z, args  # [len-as-condition]

def github_issue_1331_v4(*args):
    assert z and len(args), args  # [len-as-condition]

b = bool(len(z)) # [len-as-condition]
c = bool(len('TEST') or 42) # [len-as-condition]


def github_issue_1879():

    class ClassWithBool(list):
        def __bool__(self):
            return True

    class ClassWithoutBool(list):
        pass

    class ChildClassWithBool(ClassWithBool):
        pass

    class ChildClassWithoutBool(ClassWithoutBool):
        pass

    assert len(ClassWithBool())
    assert len(ChildClassWithBool())
    assert len(ClassWithoutBool())  # [len-as-condition]
    assert len(ChildClassWithoutBool())  # [len-as-condition]
    assert len(range(0))  # [len-as-condition]
    assert len([t + 1 for t in []])  # [len-as-condition]
    assert len(u + 1 for u in [])  # [len-as-condition]
    assert len({"1":(v + 1) for v in {}})  # [len-as-condition]
    assert len(set((w + 1) for w in set()))  # [len-as-condition]

    # pylint: disable=import-outside-toplevel
    import numpy
    numpy_array = numpy.array([0])
    if len(numpy_array) > 0:
        print('numpy_array')
    if len(numpy_array):
        print('numpy_array')
    if numpy_array:
        print('b')

    import pandas as pd
    pandas_df = pd.DataFrame()
    if len(pandas_df):
        print("this works, but pylint tells me not to use len() without comparison")
    if len(pandas_df) > 0:
        print("this works and pylint likes it, but it's not the solution intended by PEP-8")
    if pandas_df:
        print("this does not work (truth value of dataframe is ambiguous)")

    def function_returning_list(r):
        if r==1:
            return [1]
        return [2]

    def function_returning_int(r):
        if r==1:
            return 1
        return 2

    # def function_returning_generator(r):
    #     for i in [r, 1, 2, 3]:
    #         yield i

    # def function_returning_comprehension(r):
    #     return [x+1 for x in [r, 1, 2, 3]]

    # def function_returning_function(r):
    #     return function_returning_generator(r)

    assert len(function_returning_list(z))  # [len-as-condition]
    assert len(function_returning_int(z))
    # This should raise a len-as-conditions once astroid can infer it
    # See https://github.com/PyCQA/pylint/pull/3821#issuecomment-743771514
    # assert len(function_returning_generator(z))
    # assert len(function_returning_comprehension(z))
    # assert len(function_returning_function(z))


def github_issue_4215():
    # Test undefined variables
    # https://github.com/PyCQA/pylint/issues/4215
    if len(undefined_var):  # [undefined-variable]
        pass
    if len(undefined_var2[0]):  # [undefined-variable]
        pass