omnicoach 0.3.0

Lightweight, high-signal static analysis and command linter for Python and Rust
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Flags nontrivial Python functions whose docstring has no `Args:` section explaining their
//! non-obvious parameters.
//!
//! Design: `docs/dev/complex_function_docstring/03_design.md` and `03c_section_requirements.md`
//! (proposal P2, without the prose-mention exemption).

use crate::code_lint::ast::python::{
    DocstringSectionKind, PythonClassInfo, PythonParameterInfo, PythonParameterKind,
};
use crate::code_lint::ast::{self, ParsedFile};
use crate::code_lint::contract::{CodeRule, RuleTarget};
use crate::diagnostic::{Diagnostic, Language, RuleName, ViolationTemplate, violation_template};
use crate::rule_declaration::{
    Classification, Consensus, CountOption, Declaration, Example, ImpactedQuality,
    LanguageDefaults, Precision, Reference, RuleDoc, RuleOptions, Topic,
};
use std::collections::HashSet;
use std::path::Path;

const MIN_STATEMENTS: CountOption = CountOption {
    key: "min-statements",
    doc: "Minimum statements in a function body for an `Args:` section to be required when a \
          non-obvious parameter is present; 0 disables this trigger.",
    default: LanguageDefaults::new(12, &[]),
};

const MIN_COGNITIVE_COMPLEXITY: CountOption = CountOption {
    key: "min-cognitive-complexity",
    doc: "Minimum cognitive complexity of a function for an `Args:` section to be required when a \
          non-obvious parameter is present; 0 disables this trigger.",
    default: LanguageDefaults::new(6, &[]),
};

const TEMPLATE: ViolationTemplate = violation_template! {
    summary: "Docstring of `{function}` has no `Args:` section explaining {parameters}.",
    rationale: "A flag, an optional value, a callback or a variadic parameter changes how `{function}` behaves in ways its name and type annotation do not state.",
    suggestion: "Add an `Args:` section to the docstring of `{function}` that explains {parameters}.",
};

/// The rule's declaration.
pub const RULE: CodeRule<(CountOption, CountOption)> = CodeRule {
    declaration: Declaration {
        name: RuleName("unexplained-parameter"),
        template: &TEMPLATE,
        languages: &[Language::Python],
        options: RuleOptions::code_rule((MIN_STATEMENTS, MIN_COGNITIVE_COMPLEXITY)),
        classification: Classification {
            topics: &[Topic::DOCSTRINGS],
            precision: Precision::Heuristic,
            consensus: Consensus::Opinionated,
            impacted_quality: ImpactedQuality::Maintainability,
        },
        doc: RuleDoc {
            summary: "Requires an `Args:` section when a nontrivial function has an optional, boolean, callable or variadic parameter.",
            what_it_does: indoc::indoc! {r"
                Flags a documented function or method that reaches `min-statements` or
                `min-cognitive-complexity`, has no `Args:` section (`Args:`, `Arguments:`,
                `Parameters:` or `Keyword Args:`), and takes at least one parameter whose
                contract is not stated by its name and type alone:

                - an `Optional[...]` / `... | None` annotation or a `= None` default;
                - a `bool` annotation or a `= True` / `= False` default;
                - a `Callable` annotation;
                - `*args` or `**kwargs`.

                One finding per function lists every such parameter in declaration order.

                For `__init__` and `__new__`, an `Args:` section in either the method docstring or
                the enclosing class docstring satisfies the rule, and for `__init__`, a parameter
                stored unchanged on `self` (`self.<name> = <name>`) is also satisfied by an entry
                under `Attributes:` in the class docstring. Not checked: functions without a
                docstring (left to `undocumented-nontrivial-function`), functions whose docstring
                already has an `Args:` section (incomplete sections are covered by Ruff `D417`),
                `_`-prefixed parameter names, nested functions, `@override` methods, property
                setters and deleters, data-model dunder methods other than `__init__`, `__new__`
                and `__call__`, and test files."},
            why_is_this_bad: indoc::indoc! {r"
                Google style allows omitting `Args:` when a function's name, signature and
                summary suffice, and Ruff `D417` only checks `Args:` sections that already exist.
                When a nontrivial function takes a boolean flag, a nullable parameter, a callback
                or `*args` / `**kwargs` and omits `Args:` altogether, callers have to read the
                body to learn what `None` falls back to, what each branch of the flag does, when
                the callback runs, or where variadic arguments are forwarded."},
            known_problems: Some(indoc::indoc! {r#"
                - Parameters whose non-obvious contract comes from a string or integer sentinel
                  default (`mode: str = "auto"`, `limit: int = -1`) or from a type alias hiding
                  `Optional`, `bool` or `Callable` are not recognized.
                - If the summary sentence already explains the parameter inline in prose without
                  an `Args:` section, the function is still flagged."#}),
            references: &[
                Reference {
                    title: "Google Python Style Guide §3.8.3: Functions and Methods",
                    url: "https://google.github.io/styleguide/pyguide.html#383-functions-and-methods",
                },
                Reference {
                    title: "Ruff rule `missing-argument-description` (D417)",
                    url: "https://docs.astral.sh/ruff/rules/undocumented-param/",
                },
            ],
            examples: &[Example {
                language: Language::Python,
                flagged: indoc::indoc! {r#"
                    def sync_records(
                        records: list[Record],
                        *,
                        dry_run: bool = False,
                        batch_size: int | None = None,
                    ) -> int:
                        """Synchronizes records with the remote store."""
                        if not records:
                            return 0
                        limit = batch_size or len(records)
                        sent = 0
                        for index in range(0, len(records), limit):
                            chunk = records[index : index + limit]
                            if not dry_run and chunk:
                                push_chunk(chunk)
                            sent += len(chunk)
                        return sent
                "#},
                flagged_span: "sync_records",
                fixed: indoc::indoc! {r#"
                    def sync_records(
                        records: list[Record],
                        *,
                        dry_run: bool = False,
                        batch_size: int | None = None,
                    ) -> int:
                        """Synchronizes records with the remote store.

                        Args:
                            records: Records to send.
                            dry_run: If True, counts batches without pushing them.
                            batch_size: Maximum records per push, or None to push all at once.
                        """
                        if not records:
                            return 0
                        limit = batch_size or len(records)
                        sent = 0
                        for index in range(0, len(records), limit):
                            chunk = records[index : index + limit]
                            if not dry_run and chunk:
                                push_chunk(chunk)
                            sent += len(chunk)
                        return sent
                "#},
            }],
        },
    },
    target: RuleTarget::SourceOnly,
    check: check_file,
};

const FUNCTION: &str = "function";
const PARAMETERS: &str = "parameters";

fn check_file(
    rule: &CodeRule<(CountOption, CountOption)>,
    path: &Path,
    file: &ParsedFile,
    (min_statements, min_cognitive_complexity): (usize, usize),
) -> Vec<Diagnostic> {
    ast::python::extract_function_signatures(file)
        .into_iter()
        .filter(|signature| {
            !signature.is_nested()
                && !signature.is_documented_elsewhere()
                && signature.is_nontrivial(min_statements, min_cognitive_complexity)
        })
        .filter_map(|signature| {
            let own_docstring = signature.docstring();
            let constructor_class = signature
                .is_constructor()
                .then(|| ast::python::enclosing_class(&signature))
                .flatten();
            let class_docstring = constructor_class
                .as_ref()
                .and_then(|class| class.docstring.as_ref());
            if own_docstring.is_none() && class_docstring.is_none() {
                return None;
            }
            if own_docstring
                .as_ref()
                .is_some_and(|doc| doc.has_section(DocstringSectionKind::Args))
                || class_docstring.is_some_and(|doc| doc.has_section(DocstringSectionKind::Args))
            {
                return None;
            }
            let documented_unchanged_attrs = constructor_class
                .as_ref()
                .filter(|_| signature.name == "__init__")
                .map(documented_unchanged_constructor_parameters)
                .unwrap_or_default();
            let missing: Vec<String> = signature
                .parameters
                .iter()
                .filter(|parameter| {
                    !parameter.name.starts_with('_')
                        && is_non_obvious(parameter)
                        && !documented_unchanged_attrs.contains(&parameter.name)
                })
                .map(|parameter| format!("`{}`", parameter.display_name()))
                .collect();
            if missing.is_empty() {
                return None;
            }
            Some(rule.diagnostic_at_node(
                path,
                &signature.name_node,
                &[
                    (FUNCTION, &signature.name),
                    (PARAMETERS, &missing.join(", ")),
                ],
            ))
        })
        .collect()
}

/// Names of parameters of `class.constructor()` that are stored unchanged on `self`
/// ([`PythonClassInfo::unchanged_constructor_parameters`]) and listed under `Attributes:` in the
/// class docstring.
fn documented_unchanged_constructor_parameters(class: &PythonClassInfo<'_>) -> HashSet<String> {
    let Some(docstring) = &class.docstring else {
        return HashSet::new();
    };
    let documented = docstring.section_entry_names(DocstringSectionKind::Attributes);
    class
        .unchanged_constructor_parameters()
        .into_iter()
        .filter(|name| documented.contains(name))
        .collect()
}

/// Returns true if `parameter`'s value changes behavior in ways its name and type annotation do
/// not state: `*args` / `**kwargs`, a default of `None`, `True` or `False`, or an annotation that
/// includes `None`, `bool` or `Callable`.
fn is_non_obvious(parameter: &PythonParameterInfo<'_>) -> bool {
    if parameter.kind == PythonParameterKind::Receiver {
        return false;
    }
    let has_non_obvious_default = parameter
        .default_node
        .as_ref()
        .is_some_and(|node| matches!(node.text().trim(), "None" | "True" | "False"));
    parameter.is_variadic()
        || has_non_obvious_default
        || parameter
            .type_node
            .as_ref()
            .is_some_and(ast::python::includes_none_bool_or_callable)
}

#[cfg(test)]
crate::test_utils::rule_test!(
    RULE,
    {
        Python => {
            pass: [
                args_section_present_exempt => r#"
                    def sync_records(records: list[int], *, dry_run: bool = False) -> int:
                        """Synchronizes records.

                        Args:
                            records: Records to send.
                            dry_run: If True, skips network writes.
                        """
                        if not records:
                            return 0
                        total = 0
                        for item in records:
                            if item > 0:
                                if not dry_run:
                                    send(item)
                                total += 1
                        return total
                "#,
                ordinary_parameters_exempt => r#"
                    def merge_ranges(ranges: list[list[int]], step: int) -> list[list[int]]:
                        """Merges overlapping ranges."""
                        if not ranges:
                            return []
                        merged = []
                        for start, end in sorted(ranges):
                            if merged and start <= merged[-1][1] + step:
                                merged[-1][1] = max(merged[-1][1], end)
                            else:
                                merged.append([start, end])
                        return merged
                "#,
                below_complexity_gate_exempt => r#"
                    def set_verbose(enabled: bool = False) -> bool:
                        """Sets verbosity."""
                        return enabled
                "#,
                undocumented_function_exempt => r"
                    def sync_records(records: list[int], *, dry_run: bool = False) -> int:
                        if not records:
                            return 0
                        total = 0
                        for item in records:
                            if item > 0:
                                if not dry_run:
                                    send(item)
                                total += 1
                        return total
                ",
                nested_function_exempt => r#"
                    def outer(items: list[int]) -> int:
                        """Processes items."""
                        def inner(flag: bool = False) -> int:
                            """Helper."""
                            total = 0
                            for item in items:
                                if item > 0:
                                    if flag:
                                        total += item
                                    else:
                                        total -= item
                            return total
                        return inner()
                "#,
                override_method_exempt => r#"
                    from typing import override

                    class Worker(BaseWorker):
                        @override
                        def run(self, items: list[int], *, dry_run: bool = False) -> int:
                            """Runs the worker."""
                            total = 0
                            for item in items:
                                if item > 0:
                                    if not dry_run:
                                        send(item)
                                    total += 1
                            return total
                "#,
                underscore_prefixed_parameter_exempt => r#"
                    def sync_records(records: list[int], _dry_run: bool = False) -> int:
                        """Synchronizes records."""
                        total = 0
                        for item in records:
                            if item > 0:
                                if item % 2 == 0:
                                    total += item
                                else:
                                    total += 1
                        return total
                "#,
                unchanged_constructor_attribute_exempt => r#"
                    class Client:
                        """HTTP client.

                        Attributes:
                            timeout: Request timeout in seconds, or None for no timeout.
                        """

                        def __init__(self, timeout: float | None = None) -> None:
                            self.timeout = timeout
                            if timeout is not None:
                                if timeout < 0:
                                    if timeout < -1:
                                        raise ValueError(timeout)
                "#,
                class_args_section_satisfies_constructor_exempt => r#"
                    class Client:
                        """HTTP client.

                        Args:
                            timeout: Request timeout in seconds, or None for no timeout.
                        """

                        def __init__(self, timeout: float | None = None) -> None:
                            self.timeout = timeout or 5.0
                            if self.timeout < 0:
                                if self.timeout < -1:
                                    if self.timeout < -2:
                                        raise ValueError(self.timeout)
                "#,
            ],
            fail: [
                optional_and_bool_parameters_flagged => r#"
                    def sync_records(
                        records: list[int],
                        *,
                        dry_run: bool = False,
                        batch_size: int | None = None,
                    ) -> int:
                        """Synchronizes records."""
                        if not records:
                            return 0
                        total = 0
                        for item in records:
                            if item > 0:
                                if not dry_run:
                                    send(item, batch_size)
                                total += 1
                        return total
                "# => "sync_records",
                callable_and_variadic_parameters_flagged => r#"
                    from collections.abc import Callable

                    def dispatch(
                        items: list[int],
                        on_item: Callable[[int], None],
                        *args: int,
                        **kwargs: str,
                    ) -> int:
                        """Dispatches items."""
                        total = 0
                        for item in items:
                            if item > 0:
                                if item % 2 == 0:
                                    on_item(item)
                                total += 1
                        return total
                "# => "dispatch",
                transformed_constructor_parameter_flagged => r#"
                    class Client:
                        """HTTP client.

                        Attributes:
                            timeout: Request timeout in seconds, or None for no timeout.
                            retries: Retry count after clamping.
                        """

                        def __init__(
                            self,
                            timeout: float | None = None,
                            retries: int | None = None,
                        ) -> None:
                            self.timeout = timeout
                            self.retries = retries or 3
                            if self.retries < 0:
                                if self.retries < -1:
                                    if self.retries < -2:
                                        raise ValueError(self.retries)
                "# => "__init__",
                rebound_constructor_parameter_flagged => r#"
                    class Client:
                        """HTTP client.

                        Attributes:
                            endpoint: Normalized endpoint URL.
                        """

                        def __init__(self, endpoint: str | None = None) -> None:
                            endpoint = endpoint or "http://localhost"
                            self.endpoint = endpoint
                            if not self.endpoint:
                                if len(self.endpoint) == 0:
                                    if self.endpoint == "":
                                        raise ValueError(self.endpoint)
                "# => "__init__",
            ]
        }
    }
);