File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ Complexity. Statements have the following effects on Cyclomatic Complexity:
22
22
if +1 An `if ` statement is a single decision.
23
23
elif +1 The `elif ` statement adds another decision.
24
24
else +0 The `else ` statement does not cause a new decision. The decision is at the `if `.
25
+ case pattern +1 The `case pattern ` statement is a single decision.
26
+ case _ +0 The `case _ ` statement does not cause a new decision. The decision is at the `case pattern `.
25
27
for +1 There is a decision at the start of the loop.
26
28
while +1 There is a decision at the `while ` statement.
27
29
except +1 Each `except ` branch adds a new conditional path of execution.
Original file line number Diff line number Diff line change 81
81
4 ,
82
82
{},
83
83
),
84
+ (
85
+ '''
86
+ match a:
87
+ case 1: pass
88
+ ''' ,
89
+ 2 ,
90
+ {},
91
+ ),
92
+ (
93
+ '''
94
+ match a:
95
+ case 1: pass
96
+ case _: pass
97
+ ''' ,
98
+ 2 ,
99
+ {},
100
+ ),
101
+ (
102
+ '''
103
+ match a:
104
+ case 1: pass
105
+ case 2: pass
106
+ ''' ,
107
+ 3 ,
108
+ {},
109
+ ),
110
+ (
111
+ '''
112
+ match a:
113
+ case 1: pass
114
+ case 2: pass
115
+ case _: pass
116
+ ''' ,
117
+ 3 ,
118
+ {},
119
+ ),
84
120
(
85
121
'''
86
122
for x in range(10): print(x)
Original file line number Diff line number Diff line change @@ -236,6 +236,13 @@ def generic_visit(self, node):
236
236
# Note: Lambda functions are not counted anymore, see #68
237
237
elif name in ('If' , 'IfExp' ):
238
238
self .complexity += 1
239
+ elif name == 'Match' :
240
+ # check if _ (else) used
241
+ contain_underscore = any (
242
+ (case for case in node .cases if
243
+ getattr (case .pattern , "pattern" , False ) is None ))
244
+ # Max used for case when match contain only _ (else)
245
+ self .complexity += max (0 , len (node .cases ) - contain_underscore )
239
246
# The For and While blocks count as 1 plus the `else` block.
240
247
elif name in ('For' , 'While' , 'AsyncFor' ):
241
248
self .complexity += bool (node .orelse ) + 1
You can’t perform that action at this time.
0 commit comments