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
use ;
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`. The predicate of an `if` / `elif` / `while`, a
// ternary condition and a `case` guard are each a boolean slot that
// pays one condition unless an arm inside it already did (see
// `python_count_condition`).
//
// `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).
// One step of the value peel: the expression a wrapper evaluates to, or
// `None` for anything that is not a wrapper. A parenthesis holds its
// only operand — read by role, not at child(1) after the `(` token,
// where a comment sits in `if ( # c` (#1455) — and a walrus
// `(n := g())` evaluates to its `value`, so `if (y := x > 1):` tests
// the comparison its own arm already counts. The walrus was a terminal
// kind until #1526, which charged that slot a second time.
//
// `not` is deliberately not peeled: the `NotOperator` arm counts it
// when its operand pays nothing else, so `python_condition_scores_itself`
// stops on it instead.
// Whether another arm of `compute` already charges `expr` (already
// peeled) as a condition: a comparison, a `not`, a ternary (each its own
// arm) or an `and` / `or` chain, whose walker scores each operand
// through `python_count_condition` and so always carries a condition of
// its own.
// Scores one boolean slot: an `if` / `elif` / `while` predicate, a
// ternary condition, a `case` guard, or an operand of an `and` / `or`
// chain (see `count_boolean_slot`). Python is truthy-valued, so every
// expression the grammar can put there — `-x`, `x + 1`, a lambda — is a
// decision, and scored 0 while the slot paid only for a fixed list of
// terminal kinds (#1526).
// 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`.
//
// 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, 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 its
// first named child that is not an `extra`: a comment may precede it
// (`case n if # why\n b:`), and would pay as a slot of its own.
//
// No double count (§5): cyclomatic reaches this guard through the `If`
// *keyword token* inside the `if_clause`, which no ABC arm matches.