forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualsOrNotEquals.py
More file actions
147 lines (111 loc) · 3.2 KB
/
EqualsOrNotEquals.py
File metadata and controls
147 lines (111 loc) · 3.2 KB
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
class A:
def __init__(self, a):
self.a = a
# OK: __ne__ if not defined delegates to eq automatically
def __eq__(self, other):
return self.a == other.a
assert (A(1) == A(1))
assert not (A(1) == A(2))
assert not (A(1) != A(1))
assert (A(1) != A(2))
class B: # $ Alert
def __init__(self, b):
self.b = b
# BAD: eq defaults to `is`
def __ne__(self, other):
return self.b != other.b
assert not (B(1) == B(1)) # potentially unexpected
assert not (B(2) == B(2))
assert not (B(1) != B(1))
assert (B(1) != B(2))
class C:
def __init__(self, c):
self.c = c
def __eq__(self, other):
return self.c == other.c
def __ne__(self, other):
return self.c != other.c
class D(C): # $ Alert
def __init__(self, c, d):
super().__init__(c)
self.d = d
# BAD: ne is not defined, but the superclass ne is used instead of delegating, which may be incorrect
def __eq__(self, other):
return self.c == other.c and self.d == other.d
assert (D(1,2) == D(1,2))
assert not (D(1,2) == D(1,3))
assert (D(1,2) != D(3,2))
assert not (D(1,2) != D(1,3)) # Potentially unexpected
class E:
def __init__(self, e):
self.e = e
def __eq__(self, other):
return self.e == other.e
def __ne__(self, other):
return not self.__eq__(other)
class F(E):
def __init__(self, e, f):
super().__init__(e)
self.f = f
# OK: superclass ne delegates to eq
def __eq__(self, other):
return self.e == other.e and self.f == other.f
assert (F(1,2) == F(1,2))
assert not (F(1,2) == F(1,3))
assert (F(1,2) != F(3,2))
assert (F(1,2) != F(1,3))
# Variations
class E2:
def __init__(self, e):
self.e = e
def __eq__(self, other):
return self.e == other.e
def __ne__(self, other):
return not self == other
class F2(E2):
def __init__(self, e, f):
super().__init__(e)
self.f = f
# OK: superclass ne delegates to eq
def __eq__(self, other):
return self.e == other.e and self.f == other.f
assert (F2(1,2) == F2(1,2))
assert not (F2(1,2) == F2(1,3))
assert (F2(1,2) != F2(3,2))
assert (F2(1,2) != F2(1,3))
class E3:
def __init__(self, e):
self.e = e
def __eq__(self, other):
return self.e == other.e
def __ne__(self, other):
return not other.__eq__(self)
class F3(E3):
def __init__(self, e, f):
super().__init__(e)
self.f = f
# OK: superclass ne delegates to eq
def __eq__(self, other):
return self.e == other.e and self.f == other.f
assert (F3(1,2) == F3(1,2))
assert not (F3(1,2) == F3(1,3))
assert (F3(1,2) != F3(3,2))
assert (F3(1,2) != F3(1,3))
class E4:
def __init__(self, e):
self.e = e
def __eq__(self, other):
return self.e == other.e
def __ne__(self, other):
return not other == self
class F4(E4):
def __init__(self, e, f):
super().__init__(e)
self.f = f
# OK: superclass ne delegates to eq
def __eq__(self, other):
return self.e == other.e and self.f == other.f
assert (F4(1,2) == F4(1,2))
assert not (F4(1,2) == F4(1,3))
assert (F4(1,2) != F4(3,2))
assert (F4(1,2) != F4(1,3))