summaryrefslogtreecommitdiff
path: root/tests/functional/ext/code_style/consider_using_assignment_expr.py
blob: 4f9a8370031e13b8019c8ba47cbccaf82ce94226 (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
# pylint: disable=missing-docstring,invalid-name,undefined-variable,too-few-public-methods

a1 = 2
if a1:  # [consider-using-assignment-expr]
    ...

# Do not suggest assignment expressions if assignment spans multiple lines
a2 = (
    1,
)
if a2:
    ...

# Only first name should be replaced
a3 = 2
if a3 == a3_a:  # [consider-using-assignment-expr]
    ...

# Above black line length
a4 = some_loooooooonnnnnngggg_object_name.with_some_really_long_function_name(arg)
if a4:
    ...

def func_a():
    a5 = some___object.function_name_is_just_long_enough_to_fit_in_line()  # some comment
    if a5 is None:  # [consider-using-assignment-expr]
        ...

    # Using assignment expression would result in line being 89 chars long
    a6 = some_long_object.function_name_is_too_long_enough_to_fit___line()
    if a6 is None:
        ...

# Previous unrelate note should not match
print("")
if a7:
    ...


b1: int = 2
if b1:  # [consider-using-assignment-expr]
    ...

b2 = some_function(2, 3)
if b2:  # [consider-using-assignment-expr]
    ...

b3 = some_object.variable
if b3:  # [consider-using-assignment-expr]
    ...


# UnaryOp
c1 = 2
if not c1:  # [consider-using-assignment-expr]
    ...


# Compare
d1 = 2
if d1 is True:  # [consider-using-assignment-expr]
    ...

d2 = 2
if d2 is not None:  # [consider-using-assignment-expr]
    ...

d3 = 2
if d3 == 2:  # [consider-using-assignment-expr]
    ...


# -----
# Don't emit warning if match statement would be a better fit
o1 = 2
if o1 == 1:
    ...
elif o1 == 2:
    ...
elif o1 == 3:
    ...

o2 = 2
if o2 == 1:
    ...
elif o2:
    ...

o3 = 2
if o3 == 1:  # [consider-using-assignment-expr]
    ...
else:
    ...

o4 = 2
if o4 == 1:  # [consider-using-assignment-expr]
    ...
elif o4 and o4_other:
    ...

o5 = 2
if o5 == 1:  # [consider-using-assignment-expr]
    ...
elif o5_other == 1:
    ...

o6 = 2
if o6 == 1:  # [consider-using-assignment-expr]
    ...
elif o6_other:
    ...

def func_p():
    p1 = 2
    if p1 == 1:
        return
    if p1 == 2:
        return

    p2 = 2
    if p2 == 1:
        return
    if p2:
        return

    p3 = 2
    if p3 == 1:  # [consider-using-assignment-expr]
        ...
    else:
        ...

    p4 = 2
    if p4 == 1:  # [consider-using-assignment-expr]
        ...
    elif p4 and p4_other:
        ...

    p5 = 2
    if p5 == 1:  # [consider-using-assignment-expr]
        ...
    elif p5_other == 1:
        ...

    p6 = 2
    if p6 == 1:  # [consider-using-assignment-expr]
        ...
    elif p6_other:
        ...


# -----
# Assignment expression does NOT work for attribute access
# Make sure not to emit message!
class A:
    var = 1

A.var = 2
if A.var:
    ...