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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use ;
use cratepython_bool_terminal_kinds;
use crate::*;
// Fitzpatrick's ABC rules adapted for Python.
//
// - Assignments: every `Assignment` node that contains an explicit `=`
// token (plain assignment, walrus `:=` lives in `NamedExpression`,
// handled separately), plus every `AugmentedAssignment` (`+=`,
// `-=`, …) and every `NamedExpression` (walrus). Bare type-only
// annotations like `x: int` also parse as `Assignment` but have no
// `=` child — these are excluded so a class-level type annotation
// does not inflate the assignment count.
// - Branches: every `Call` node. Python's "object construction" is
// syntactically a `Call` (`Foo()` parses as `call`), so the same
// arm covers it without a separate `New`-style case.
// - Conditions: comparison operators (`ComparisonOperator` wraps
// `<`, `>`, `==`, `!=`, `is`, `is not`, `in`, `not in`, etc. as a
// single node), `ConditionalExpression` (ternary `a if c else b`),
// the unary `NotOperator` (paper's "unary conditional expression",
// Rule 7 / Figure 4), and the explicit arms of control flow:
// `ElifClause`, `ElseClause`, `ExceptClause`, `FinallyClause`,
// `CaseClause`. We do not separately count the `if` / `while`
// keyword: the condition expression itself is already covered by
// `ComparisonOperator`. This matches the token-level approach
// used for PHP / Bash.
//
// `BooleanOperator` (Python's `and` / `or` wrapper) is
// deliberately NOT counted, and `NotOperator` is kept as the
// paper's "unary conditional expression". See the module-level
// `Stats` doc-comment for the cross-language `&&` / `||` policy
// (issue #395, walker tracked in #403).
// Python ABC unary-conditional walker (Fitzpatrick Rule 9 / Figure 4;
// issue #403). Python's `a and b` parses as `boolean_operator` (NOT
// `binary_operator`), so the walker triggers on the `And` / `Or`
// keyword tokens and iterates the immediate children of the parent
// `boolean_operator`. Terminal-bool kinds: `Identifier`, `True`,
// `False`, `Call`, `Attribute` (`obj.attr`), and `Subscript` (`xs[i]`).
//
// Unlike the C-family walkers, this one deliberately does NOT recurse
// into `NotOperator`, `ComparisonOperator`, or nested `BooleanOperator`
// children — each of those is counted by its own top-level dispatcher
// arm. Re-counting them inside the walker would inflate the metric for
// any `not x and y` / `x == 0 and y` shape. `ParenthesizedExpression`
// is still unwrapped to catch bare-identifier operands like
// `if (a) and b:` that the dispatcher would otherwise miss.
// Phase-2B (issue #403): Python `if` / `while` condition slot.
// Python has no paren wrap around if-conditions and no top-level
// terminal arm, so the condition has to be classified directly:
// - Identifier / True / False / Call / Attribute / Subscript at
// the top level counts once (Rule 6: bare-boolean condition).
// - ParenthesizedExpression unwraps via `python_inspect_container`.
// - NotOperator / ComparisonOperator / BooleanOperator are
// skipped: each is counted by its own top-level dispatcher arm
// (the `Or`/`And` keyword walker, the `NotOperator` arm, and
// the `ComparisonOperator` arm at lines `~1334-1390`).
// Phase-2B (issue #1161): the condition slot of a Python conditional
// expression, `<consequence> if <condition> else <alternative>`. Before
// this, `a if c() else b` reported 1 — the `ConditionalExpression` node
// alone — where every C-family language reports 2 for the equivalent
// `c() ? a : b`, and `python_inspect_container`'s `ConditionalExpression`
// boolean-context seed was unreachable because no call site ever passed
// that parent.
//
// tree-sitter-python's `conditional_expression` is a bare
// `seq(expression, 'if', expression, 'else', expression)` carrying no
// grammar fields at all, so the slot cannot be named the way Ruby's and
// the C family's can. It is located by ROLE rather than by a bare index
// (`.claude/rules/grammar-dispatch.md` item 3): the first child after
// the `if` keyword that is not a comment.
//
// Both halves of that are load-bearing, and neither is theoretical.
// Comments are tree-sitter `extras`, so they land as direct children of
// `conditional_expression` and shift every index after them — `child(2)`
// reads the `if` token itself for `a\n# why\nif b else c`. They can also
// sit between the keyword and the condition, where they would be the
// anchor's next child, which is why the scan skips them rather than
// taking the first. The C family is immune to both only because it can
// ask for the slot by name.
//
// Only the condition is routed. Python's branch operands are already
// counted by the top-level `NotOperator` / `ComparisonOperator` arms — a
// different mechanism from the C-family walkers — so a wholesale
// `cpp_walk_ternary` copy would double-count: `(b) if a else (c)` would
// score its two parenthesised operands, which the identical
// unparenthesised `b if a else c` correctly scores at zero.
// The `case … if g:` guard of a `case_clause`, modelled as a condition
// slot exactly like the `if` / `while` slots (#1454, transferring
// #1422's C# rule). Before this, a guard scored whatever operator
// happened to sit inside it: `case n if n > 5:` counted one via the
// `comparison_operator` arm while `case n if is_even(n):` and
// `case _ if b:` counted zero, so three semantically identical guards
// produced two different numbers. As a slot every spelling contributes
// exactly one — a call / attribute / subscript / bare identifier through
// `python_bool_terminal_kinds!()`, a comparison or `not` through the arm
// that already owns it — and a compound guard (`case n if a > 1 and b:`)
// keeps its sub-structure rather than collapsing to one.
//
// By grammar FIELD, not index (`.claude/rules/grammar-dispatch.md` §3):
// `case_clause` names its guard `guard`, which is what keeps a
// comprehension's `if_clause` — the same kind, in a wholly different
// role — out of this slot. The clause itself carries no field for its
// expression (it is `seq('if', expression)`), so the operand is located
// as a named child rather than at a fixed offset, and *every* named
// child is passed: tree-sitter `extra`s are named and may precede it,
// so `case n if # why\n b:` hands a `comment` to a first-child read.
// Python's extras at this pin are `comment` and `line_continuation`,
// neither a `python_bool_terminal_kinds!()` member nor a
// `parenthesized_expression`, so passing them through adds nothing and
// the loop cannot double count a clause that holds one expression by
// construction.
//
// No double count (§5): cyclomatic reaches this guard through the `If`
// *keyword token* inside the `if_clause`, which no ABC arm matches.