1
1
# pylint: disable=invalid-name,unnecessary-pass,no-else-return,useless-else-on-loop
2
2
# pylint: disable=undefined-variable,consider-using-sys-exit,unused-variable,too-many-return-statements
3
- # pylint: disable=redefined-outer-name,using-constant-test,unused-argument
3
+ # pylint: disable=redefined-outer-name,using-constant-test,unused-argument,unnecessary-lambda-assignment
4
4
# pylint: disable=broad-except, not-context-manager, no-method-argument, unspecified-encoding, broad-exception-raised
5
5
6
6
"""Checks use of "too-complex" check"""
7
7
8
8
9
- def f1 (): # [too-complex]
9
+ def just_a_pass (): # [too-complex]
10
10
"""McCabe rating: 1"""
11
11
pass
12
12
13
13
14
- def f2 (n ): # [too-complex]
14
+ def just_a_yield (): # [too-complex]
15
+ """McCabe rating: 1"""
16
+ yield from range (10 )
17
+
18
+
19
+ def just_a_return (): # [too-complex]
20
+ """McCabe rating: 1"""
21
+ return 42
22
+
23
+
24
+ def just_a_raise (): # [too-complex]
25
+ """McCabe rating: 1"""
26
+ raise ValueError ("An error occurred" )
27
+
28
+
29
+ def one_edge_multiple_operations (n ): # [too-complex]
15
30
"""McCabe rating: 1"""
16
31
k = n + 4
17
32
s = k + n
18
33
return s
19
34
20
35
21
- def f3 (n ): # [too-complex]
36
+ def if_elif_else (n ): # [too-complex]
22
37
"""McCabe rating: 3"""
23
38
if n > 3 :
24
39
return "bigger than three"
@@ -28,44 +43,83 @@ def f3(n): # [too-complex]
28
43
return "smaller than or equal to three"
29
44
30
45
31
- def f4 (): # [too-complex]
46
+ def if_with_conditionals (a , b , c ): # [too-complex]
47
+ """McCabe rating: 2"""
48
+ if ( # pylint: disable=too-many-boolean-expressions
49
+ a
50
+ and b
51
+ or c
52
+ or (a and not b )
53
+ or (b and not c )
54
+ or (c and not a )
55
+ or (a and b and c )
56
+ ):
57
+ return True
58
+ return False
59
+
60
+
61
+ def for_loop (): # [too-complex]
32
62
"""McCabe rating: 2"""
33
63
for i in range (10 ):
34
64
print (i )
35
65
36
66
37
- def f5 (mylist ): # [too-complex]
67
+ def for_loop_with_else (mylist ): # [too-complex]
38
68
"""McCabe rating: 2"""
39
69
for i in mylist :
40
70
print (i )
41
71
else :
42
72
print (None )
43
73
44
74
45
- def f6 (n ): # [too-complex]
75
+ def recursive_if_else (n ): # [too-complex]
46
76
"""McCabe rating: 2"""
47
77
if n > 4 :
48
- return f (n - 1 )
78
+ return recursive_if_else (n - 1 )
49
79
else :
50
80
return n
51
81
52
82
53
- def f7 (): # [too-complex]
83
+ def for_loop_with_break (): # [too-complex]
84
+ """McCabe rating: 3"""
85
+ for i in range (10 ):
86
+ if i == 5 :
87
+ break
88
+
89
+
90
+ def for_loop_with_continue (): # [too-complex]
91
+ """McCabe rating: 3"""
92
+ for i in range (10 ):
93
+ if i % 2 == 0 :
94
+ continue
95
+ print (i )
96
+
97
+
98
+ def for_loop_with_continue_and_break (): # [too-complex]
99
+ """McCabe rating: 4"""
100
+ for i in range (10 ):
101
+ if i % 2 == 0 :
102
+ continue
103
+ if i % 5 == 0 :
104
+ break
105
+
106
+
107
+ def inner_functions (): # [too-complex]
54
108
"""McCabe rating: 3"""
55
109
56
- def b ():
110
+ def inner_function (): # Known false negative ?
57
111
"""McCabe rating: 2"""
58
112
59
- def c ():
113
+ def innermost_function (): # Known false negative ?
60
114
"""McCabe rating: 1"""
61
115
pass
62
116
63
- c ()
117
+ innermost_function ()
64
118
65
- b ()
119
+ inner_function ()
66
120
67
121
68
- def f8 (): # [too-complex]
122
+ def try_with_multiple_except_and_else (): # [too-complex]
69
123
"""McCabe rating: 4"""
70
124
try :
71
125
print (1 )
@@ -77,7 +131,44 @@ def f8(): # [too-complex]
77
131
print (4 )
78
132
79
133
80
- def f9 (): # [too-complex]
134
+ def with_ (fp ): # [too-complex]
135
+ """McCabe rating: 1"""
136
+ with open (fp ) as f :
137
+ content = f .read ()
138
+ return content
139
+
140
+
141
+ def lambda_with_if (lst ): # [too-complex]
142
+ """McCabe rating should be 4, but is 1 (known false negative ?)
143
+
144
+ See counterpart 'comprehension_with_if' below."""
145
+ f = lambda x : [x for x in lst if x % 2 == 0 ] or range (10 )
146
+ return f (lst )
147
+
148
+
149
+ def comprehension_with_if (lst ): # [too-complex]
150
+ """McCabe rating: should be 4 but is 1 (known false negative ?)
151
+ https://github.com/PyCQA/mccabe/issues/69
152
+ """
153
+ return [x for x in lst if x % 2 == 0 ] or range (10 )
154
+
155
+
156
+ def comprehension_with_if_equivalent (lst ): # [too-complex]
157
+ """McCabe rating: 4
158
+
159
+ See counterpart 'comprehension_with_if' above.
160
+ """
161
+ xs = []
162
+ for x in lst :
163
+ if x % 2 == 0 :
164
+ xs .append (x )
165
+ if xs :
166
+ return xs
167
+ else :
168
+ return range (10 )
169
+
170
+
171
+ def nested_ifs_elifs_elses (): # [too-complex]
81
172
"""McCabe rating: 9"""
82
173
myint = 2
83
174
if myint > 5 :
@@ -103,7 +194,7 @@ def f9(): # [too-complex]
103
194
myint = 4
104
195
105
196
106
- def f10 (): # [too-complex]
197
+ def big_elif_chain_with_nested_ifs (): # [too-complex]
107
198
"""McCabe rating: 11"""
108
199
myint = 2
109
200
if myint == 5 :
@@ -130,16 +221,16 @@ def f10(): # [too-complex]
130
221
return myint
131
222
132
223
133
- class MyClass1 :
224
+ class ExampleComplexityClass :
134
225
"""Class of example to test mccabe"""
135
226
136
- _name = "MyClass " # To force a tail.node=None
227
+ _name = "ExampleComplexityKlass " # To force a tail.node=None
137
228
138
- def method1 ( ): # [too-complex]
229
+ def just_a_pass ( self ): # [too-complex]
139
230
"""McCabe rating: 1"""
140
231
pass
141
232
142
- def method2 (self , param1 ): # [too-complex, too-many-branches]
233
+ def highly_complex (self , param1 ): # [too-complex, too-many-branches]
143
234
"""McCabe rating: 15"""
144
235
if not param1 :
145
236
pass
@@ -148,9 +239,7 @@ def method2(self, param1): # [too-complex, too-many-branches]
148
239
pass
149
240
else :
150
241
pass
151
-
152
242
pass
153
-
154
243
if param1 :
155
244
pass
156
245
if param1 :
@@ -166,7 +255,6 @@ def method2(self, param1): # [too-complex, too-many-branches]
166
255
if param1 :
167
256
for value in range (5 ):
168
257
pass
169
-
170
258
pass
171
259
for count in range (6 ):
172
260
with open ("myfile" ) as fp :
@@ -195,7 +283,8 @@ def method2(self, param1): # [too-complex, too-many-branches]
195
283
return param1
196
284
197
285
198
- for count in range (10 ): # [too-complex]
286
+ for count in range (10 ): # [too-complex]
287
+ # McCabe rating: 4
199
288
if count == 1 :
200
289
exit (0 )
201
290
elif count == 2 :
@@ -204,7 +293,7 @@ def method2(self, param1): # [too-complex, too-many-branches]
204
293
exit (2 )
205
294
206
295
207
- def method3 ( self ): # [too-complex]
296
+ def try_finally_with_nested_ifs ( ): # [too-complex]
208
297
"""McCabe rating: 3"""
209
298
try :
210
299
if True :
@@ -215,23 +304,23 @@ def method3(self): # [too-complex]
215
304
pass
216
305
return True
217
306
218
- def match_case_complexity (avg ): # [too-complex]
307
+
308
+ def match_case (avg ): # [too-complex]
219
309
"""McCabe rating: 4
220
310
See https://github.com/astral-sh/ruff/issues/11421
221
311
"""
222
312
# pylint: disable=bare-name-capture-pattern
223
313
match avg :
224
- case avg if avg < .3 :
314
+ case avg if avg < 0 .3 :
225
315
avg_grade = "F"
226
- case avg if avg < .7 :
316
+ case avg if avg < 0 .7 :
227
317
avg_grade = "E+"
228
318
case _:
229
319
raise ValueError (f"Unexpected average: { avg } " )
230
320
return avg_grade
231
321
232
322
233
-
234
- def nested_match (data ): # [too-complex]
323
+ def nested_match_case (data ): # [too-complex]
235
324
"""McCabe rating: 8
236
325
237
326
Nested match statements."""
@@ -251,3 +340,13 @@ def nested_match(data): # [too-complex]
251
340
return "Product with no price"
252
341
case _:
253
342
return "Unknown data type"
343
+
344
+
345
+ def yield_in_for_loop (a = None , b = None , c = None ): # [too-complex]
346
+ """McCabe rating: 4"""
347
+ yield from a or ()
348
+ for elt in b :
349
+ if elt is not None :
350
+ yield elt
351
+ if c is not None :
352
+ yield c
0 commit comments