File: bug_equivalent_nodes.py

package info (click to toggle)
python-refurb 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,700 kB
  • sloc: python: 9,468; makefile: 40; sh: 6
file content (152 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (2)
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
# These tests ensure that when comparing nodes to make sure that they are
# similar, extraneous info such as line numbers don't interfere. In short, if
# two nodes are semanticaly similar, but only differ in line number, they
# should still be considered equivalent.

# See issue #97

class Person:
    name: str
    age: int

    def __init__(self, name: str) -> None:
        self.name = name

bob = Person("bob")

# The following examples should all emit errors

# member expr
_ = (
    bob.name
    if bob.name
    else "alice"
)

nums = [1, 2, 3]

# index expr
_ = (
    nums[0]
    if nums[0]
    else 123
)


def f(x: int) -> int:
    return x


# func call expr
_ = (
    f(1)
    if f(1)
    else 2
)

# list expr
_ = (
    [1, 2, 3]
    if [1, 2, 3]
    else []
)

# star "*" expr
_ = (
    [*nums, 4, 5, 6]
    if [*nums, 4, 5, 6]
    else []
)

# unary oper
_ = (
    not False
    if not False
    else False
)

# binary oper
_ = (
    1 + 2
    if 1 + 2
    else 3
)

# comparison expr
_ = (
    1 < 2
    if 1 < 2
    else 3
)

# slice expr
_ = (
    nums[1:]
    if nums[1:]
    else nums
)

# dict expr
_ = (
    {"k": "v"}
    if {"k": "v"}
    else {}
)

# tuple expr
_ = (
    (1, 2, 3)
    if (1, 2, 3)
    else ()
)

# set expr
_ = (
    {1, 2, 3}
    if {1, 2, 3}
    else set()
)


# These should not

_ = bob.age if bob.name else 123

_ = f(1) if f(2) else 2
_ = f(1) if f(x=1) else 2

_ = [1, 2, 3] if [1, 2, 3, 4] else []
_ = [1, 2, 3] if [1, 2, 4] else []

_ = [*nums, 1, 2, 3] if [*nums, 4, 5, 6] else []
_ = [*nums, 1, 2, 3] if [*nums, 1, 2, 3, 4] else []

nums2 = []

_ = [*nums, 1, 2, 3] if [*nums2, 1, 2, 3] else []

_ = - 1 if - 2 else 3
_ = - 1 if + 1 else 2

_ = 1 + 2 if 1 - 2 else 3
_ = 1 + 2 if 1 + 3 else 3
_ = 1 + 2 if 2 + 2 else 3

_ = 1 < 2 if 1 > 2 else 3
_ = 1 < 2 if 1 < 1 else 3
_ = 1 < 2 if 2 < 2 else 3
_ = 1 < 2 if 1 < 2 < 3 else 3

_ = nums[1:] if nums[2:] else nums
_ = nums[:1] if nums[:2] else nums
_ = nums[::1] if nums[::2] else nums

_ = {"k": "v"} if {"k": "not v"} else {}
_ = {"k": "v"} if {"not k": "v"} else {}
_ = {"k": "v"} if {"k": "v", "extra": "items"} else {}

_ = (1, 2, 3) if (1, 2, 3, 4) else ()
_ = (1, 2, 3) if (4, 5, 6) else ()

_ = {1, 2, 3} if {1, 2, 3, 4} else set()
_ = {1, 2, 3} if {4, 5, 6} else set()