summaryrefslogtreecommitdiff
path: root/tests/functional/ext/code_style/cs_consider_using_augmented_assign.py
blob: 417bc5c0be88924ad8e45198b86f42978920438c (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
"""Tests for consider-using-augmented-assign."""

# pylint: disable=invalid-name,too-few-public-methods,import-error,consider-using-f-string,missing-docstring

from unknown import Unknown

x = 1

# summation is commutative (for integer and float, but not for string)
x = x + 3  # [consider-using-augmented-assign]
x = 3 + x  # [consider-using-augmented-assign]
x = x + "3"  # [consider-using-augmented-assign]
x = "3" + x

# We don't warn on intricate expressions as we lack knowledge of simplifying such
# expressions which is necessary to see if they can become augmented
x, y = 1 + x, 2 + x
x = 1 + x - 2
x = 1 + x + 2

# For anything other than a float or an int we only want to warn on
# assignments where the 'itself' is on the left side of the assignment
my_list = [2, 3, 4]
my_list = [1] + my_list


class MyClass:
    """Simple base class."""

    def __init__(self) -> None:
        self.x = 1
        self.x = self.x + 1  # [consider-using-augmented-assign]
        self.x = 1 + self.x  # [consider-using-augmented-assign]

        x = 1  # [redefined-outer-name]
        self.x = x


instance = MyClass()

x = instance.x + 1

my_str = ""
my_str = my_str + "foo"  # [consider-using-augmented-assign]
my_str = "foo" + my_str

my_bytes = b""
my_bytes = my_bytes + b"foo"  # [consider-using-augmented-assign]
my_bytes = b"foo" + my_bytes


def return_str() -> str:
    """Return a string."""
    return ""


# Currently we disregard all calls
my_str = return_str() + my_str
my_str = my_str % return_str()
my_str = my_str % 1  # [consider-using-augmented-assign]
my_str = my_str % (1, 2)  # [consider-using-augmented-assign]
my_str = "%s" % my_str
my_str = return_str() % my_str
my_str = Unknown % my_str
my_str = my_str % Unknown  # [consider-using-augmented-assign]

# subtraction is anti-commutative
x = x - 3  # [consider-using-augmented-assign]
x = 3 - x

# multiplication is commutative
x = x * 3  # [consider-using-augmented-assign]
x = 3 * x  # [consider-using-augmented-assign]

# division is not commutative
x = x / 3  # [consider-using-augmented-assign]
x = 3 / x

# integer division is not commutative
x = x // 3  # [consider-using-augmented-assign]
x = 3 // x

# Left shift operator is not commutative
x = x << 3  # [consider-using-augmented-assign]
x = 3 << x

# Right shift operator is not commutative
x = x >> 3  # [consider-using-augmented-assign]
x = 3 >> x

# modulo is not commutative
x = x % 3  # [consider-using-augmented-assign]
x = 3 % x

# exponential is not commutative
x = x**3  # [consider-using-augmented-assign]
x = 3**x

# XOR is commutative
x = x ^ 3  # [consider-using-augmented-assign]
x = 3 ^ x  # [consider-using-augmented-assign]

# Bitwise AND operator is commutative
x = x & 3  # [consider-using-augmented-assign]
x = 3 & x  # [consider-using-augmented-assign]

# Bitwise OR operator is commutative
x = x | 3  # [consider-using-augmented-assign]
x = 3 | x  # [consider-using-augmented-assign]

x = x > 3
x = 3 > x

x = x < 3
x = 3 < x

x = x >= 3
x = 3 >= x

x = x <= 3
x = 3 <= x


# https://github.com/PyCQA/pylint/issues/8086
# consider-using-augmented-assign should only be flagged
# if names attribute names match exactly.

class A:
    def __init__(self) -> None:
        self.a = 1
        self.b = A()

    def test(self) -> None:
        self.a = self.a + 1  # [consider-using-augmented-assign]
        self.b.a = self.a + 1  # Names don't match!