summaryrefslogtreecommitdiff
path: root/tests/functional/u/use/use_implicit_booleaness_not_comparison.py
blob: 681ee0607aad78b8bc875b1062a7fbc6f0794817 (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
234
235
236
237
238
239
# pylint: disable=missing-docstring, missing-module-docstring, invalid-name
# pylint: disable=too-few-public-methods, line-too-long, dangerous-default-value
# pylint: disable=wrong-import-order
# https://github.com/PyCQA/pylint/issues/4774

def github_issue_4774():
    # Test literals
    # https://github.com/PyCQA/pylint/issues/4774
    good_list = []
    if not good_list:
        pass

    bad_list = []
    if bad_list == []: # [use-implicit-booleaness-not-comparison]
        pass

# Testing for empty literals
empty_tuple = ()
empty_list = []
empty_dict = {}

if empty_tuple == (): # [use-implicit-booleaness-not-comparison]
    pass

if empty_list == []: # [use-implicit-booleaness-not-comparison]
    pass

if empty_dict == {}: # [use-implicit-booleaness-not-comparison]
    pass

if () == empty_tuple: # [use-implicit-booleaness-not-comparison]
    pass

if [] == empty_list: # [use-implicit-booleaness-not-comparison]
    pass

if {} == empty_dict: # [use-implicit-booleaness-not-comparison]
    pass

def bad_tuple_return():
    t = (1, )
    return t == () # [use-implicit-booleaness-not-comparison]

def bad_list_return():
    b = [1]
    return b == [] # [use-implicit-booleaness-not-comparison]

def bad_dict_return():
    c = {1: 1}
    return c == {} # [use-implicit-booleaness-not-comparison]

assert () == empty_tuple # [use-implicit-booleaness-not-comparison]
assert [] == empty_list # [use-implicit-booleaness-not-comparison]
assert {} != empty_dict # [use-implicit-booleaness-not-comparison]
assert () < empty_tuple # [use-implicit-booleaness-not-comparison]
assert [] <= empty_list # [use-implicit-booleaness-not-comparison]
assert () > empty_tuple # [use-implicit-booleaness-not-comparison]
assert [] >= empty_list # [use-implicit-booleaness-not-comparison]

assert [] == []
assert {} != {}
assert () == ()

d = {}

if d in {}:
    pass

class NoBool:
    def __init__(self):
        self.a = 2

class YesBool:
    def __init__(self):
        self.a = True

    def __bool__(self):
        return self.a


# Should be triggered
a = NoBool()
if [] == a: # [use-implicit-booleaness-not-comparison]
    pass

a = YesBool()
if a == []:
    pass

# compound test cases

e = []
f = {}

if e == [] and f == {}: # [use-implicit-booleaness-not-comparison, use-implicit-booleaness-not-comparison]
    pass


named_fields = [0, "", "42", "forty two"]
empty = any(field == "" for field in named_fields)

something_else = NoBool()
empty_literals = [[], {}, ()]
is_empty = any(field == something_else for field in empty_literals)

h, i, j = 1, None, [1,2,3]

def test(k):
    print(k == {})

def test_with_default(k={}):
    print(k == {})
    print(k == 1)

test(h)
test(i)
test(j)

test_with_default(h)
test_with_default(i)
test_with_default(j)


class A:
    lst = []

    @staticmethod
    def test(b=1):
        print(b)
        return []


if A.lst == []:  # [use-implicit-booleaness-not-comparison]
    pass


if [] == A.lst:  # [use-implicit-booleaness-not-comparison]
    pass


if A.test("b") == []:  # [use-implicit-booleaness-not-comparison]
    pass


def test_function():
    return []


if test_function() == []:  # [use-implicit-booleaness-not-comparison]
    pass

# pylint: disable=import-outside-toplevel, wrong-import-position, import-error
# Numpy has its own implementation of __bool__, but base class has list, that's why the comparison check is happening
import numpy
numpy_array = numpy.array([0])
if numpy_array == []: # [use-implicit-booleaness-not-comparison]
    print('numpy_array')
if numpy_array != []: # [use-implicit-booleaness-not-comparison]
    print('numpy_array')
if numpy_array >= (): # [use-implicit-booleaness-not-comparison]
    print('b')

# pandas has its own implementations of __bool__ and is not subclass of list, dict, or tuple; that's why comparison check is not happening
import pandas as pd
pandas_df = pd.DataFrame()
if pandas_df == []:
    pass
if pandas_df != ():
    pass
if pandas_df <= []:
    print("don't emit warning if variable can't safely be inferred")

from typing import Union
from random import random

var: Union[dict, bool, None] = {}
if random() > 0.5:
    var = True

if var == {}:
    pass

data = {}

if data == {}: # [use-implicit-booleaness-not-comparison]
    print("This will be printed")
if data != {}: # [use-implicit-booleaness-not-comparison]
    print("This will also be printed")

if data or not data:
    print("This however won't be")

# literal string check
long_test = {}
if long_test == {        }: # [use-implicit-booleaness-not-comparison]
    pass


# Check for properties and uninferable class methods
# See https://github.com/PyCQA/pylint/issues/5646
from xyz import AnotherClassWithProperty


class ParentWithProperty:

    @classmethod
    @property
    def parent_function(cls):
        return {}


class MyClassWithProxy(ParentWithProperty):

    attribute = True

    @property
    @classmethod
    def my_property(cls):
        return {}

    @property
    @classmethod
    def my_difficult_property(cls):
        if cls.attribute:
            return {}
        return MyClassWithProxy()



def test_func():
    """Some assertions against empty dicts."""
    my_class = MyClassWithProxy()
    assert my_class.parent_function == {}  # [use-implicit-booleaness-not-comparison]
    assert my_class.my_property == {}  # [use-implicit-booleaness-not-comparison]

    # If the return value is not always implicit boolean, don't raise
    assert my_class.my_difficult_property == {}
    # Uninferable does not raise
    assert AnotherClassWithProperty().my_property == {}