-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathfunctions_test.py
More file actions
288 lines (203 loc) · 4.57 KB
/
functions_test.py
File metadata and controls
288 lines (203 loc) · 4.57 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#Consistent Returns
__all__ = [ '' ]
def ok1(x):
if x:
return None
else:
return
def ok2(x):
if x:
return 4
else:
return "Hi"
def cr1(x):
if x:
return 4
def cr2(x):
if x:
return 4
else:
return
def ok3(x):
try:
return
finally:
do_something()
#Modification of parameter with default
def ok4(x = []):
return len(x)
def mpd(x = []):
x.append("x")
class ExplicitReturnInInit(object):
def __init__(self):
return self
#These are OK
class ExplicitReturnNoneInInit(object):
def __init__(self):
return None
class PlainReturnInInit(object):
def __init__(self):
return
def error():
raise Exception()
class InitCallsError(object):
def __init__(self):
return error()
class InitCallsInit(InitCallsError):
def __init__(self):
return InitCallsError.__init__(self)
class InitIsGenerator(object):
def __init__(self):
yield self
def use_implicit_return_value(arg):
x = do_nothing()
return call_non_callable(arg)
#The return in the lambda is OK as it is auto-generated
y = lambda x : do_nothing()
def do_nothing():
pass
#Using name other than 'cls' for first parameter in methods.
# This shouldn't apply to classmethods (first parameter should be 'cls' or similar)
# or static methods (first parameter can be anything)
class Normal(object):
def n_ok(self):
pass
@staticmethod
def n_smethod(ok):
pass
@classmethod
def n_cmethod(self):
pass
# this is allowed because it has a decorator other than @classmethod
@classmethod
@id
def n_suppress(any_name):
pass
class Class(type):
def __init__(cls):
pass
def c_method(y):
pass
def c_ok(cls):
pass
@id
def c_suppress(any_name):
pass
#Using name other than 'self' for first parameter in methods.
# This shouldn't apply to classmethods (first parameter should be 'cls' or similar)
# or static methods (first parameter can be anything)
class NonSelf(object):
def __init__(x):
pass
def s_method(y):
pass
def s_ok(self):
pass
@staticmethod
def s_smethod(ok):
pass
@classmethod
def s_cmethod(cls):
pass
def s_smethod2(ok):
pass
s_smethod2 = staticmethod(s_smethod2)
def s_cmethod2(cls):
pass
s_cmethod2 = classmethod(s_cmethod2)
def returns_self(self):
return self
def return_value_ignored():
ok2()
ok4()
sorted([1,2])
d = {}
def use_return_values():
x = ok2()
x = ok2()
x = ok3()
x = ok3()
x = ok4()
x = ok4()
x = y()
x = y()
x = returns_self()
x = returns_self()
x = sorted(x)
x = sorted(x)
x = ok2()
x = ok2()
x = ok3()
x = ok3()
x = ok4()
x = ok4()
x = y()
x = y()
x = returns_self()
x = returns_self()
x = sorted(x)
x = sorted(x)
def ok_to_ignore():
ok1
do_nothing()
returns_self()
ok3()
y()
class DeprecatedSliceMethods(object):
def __getslice__(self, start, stop):
pass
def __setslice__(self, start, stop, value):
pass
def __delslice__(self, start, stop):
pass
@decorator
def nested_call_implicit_return_func_ok(arg):
do_nothing()
#OK as it returns result of a call to super().__init__()
class InitCallsInit(InitCallsError):
def __init__(self):
return super(InitCallsInit, self).__init__()
#Harmless, so we allow it.
def use_implicit_return_value_ok(arg):
return do_nothing()
def mutli_return(arg):
if arg:
return do_something()
else:
return do_nothing()
#Modification of parameter with default
def augassign(x = []):
x += ["x"]
#Possible FPs for non-self. ODASA-2439
class C(object):
def _func(f):
return f
_func(x)
#or
@_func
def meth(self):
pass
def dont_care(arg):
pass
class C(object):
meth = dont_care
class Meta(type):
#__new__ is an implicit class method, so the first arg is the metaclass
def __new__(metacls, name, bases, cls_dict):
return super(Meta, metacls).__new__(metacls, name, bases, cls_dict)
#ODASA 3658
from sys import exit
#Consistent returns
def ok5():
try:
may_raise()
return 0
except ValueError as e:
print(e)
exit(EXIT_ERROR)
#ODASA-6062
import zope.interface
class Z(zope.interface.Interface):
def meth(arg):
pass
Z().meth(0)