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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Flags `@override` method docstrings that drop a contract section of the docstring they replace.
//!
//! Design: `docs/dev/trivial_override_docstring/03_design.md`. Section kinds are compared, never
//! header text or entries; only ancestors defined earlier in the same file are seen.

use crate::code_lint::ast::python::{DocstringSection, DocstringSectionKind, PythonDocstring};
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, Declaration, Example, ImpactedQuality, Precision, Reference,
    RuleDoc, RuleOptions, Topic,
};
use std::path::Path;

const TEMPLATE: ViolationTemplate = violation_template! {
    summary: "Docstring of `{function}` has no {sections} section, unlike the `{class}` docstring it replaces.",
    rationale: "A method's own docstring replaces the inherited one in `help()`, generated documentation and IDE hovers, so readers of the override no longer see that part of the contract.",
    suggestion: "Add the missing sections if the docstring states how the override differs from the base; otherwise remove the docstring so that the complete one is inherited.",
};

/// The rule's declaration.
pub const RULE: CodeRule = CodeRule {
    declaration: Declaration {
        name: RuleName("partial-override-docstring"),
        template: &TEMPLATE,
        languages: &[Language::Python],
        options: RuleOptions::code_rule(()),
        classification: Classification {
            topics: &[Topic::DOCSTRINGS],
            precision: Precision::Exact,
            consensus: Consensus::Opinionated,
            impacted_quality: ImpactedQuality::Maintainability,
        },
        doc: RuleDoc {
            summary: "Flags `@override` method docstrings that omit an `Args:`, `Returns:`, `Yields:` or `Raises:` section of the inherited docstring.",
            what_it_does: indoc::indoc! {r"
                Flags the docstring of a method decorated with `@override` (`typing.override` or
                `typing_extensions.override`) when the docstring it replaces has an `Args:`,
                `Returns:`, `Yields:` or `Raises:` section of a kind the override's docstring lacks.
                The inherited docstring is the one `inspect.getdoc` would show without the
                override's: the first ancestor class defined earlier in the file whose method of
                the same name has a docstring. Section kinds are compared, so `Arguments:`
                satisfies `Args:`; `Returns:` and `Yields:` satisfy each other, as does a summary
                starting with `Returns` or `Yields`. A base `Raises:` section listing only
                `NotImplementedError` is not required, and other sections (`Example:`, `Note:`)
                never are. An override has no docstring or a complete one, which is stricter than
                the Google style guide's docstring with at least the differences. A docstring equal
                to the inherited summary is left to `copied-base-docstring`."},
            why_is_this_bad: indoc::indoc! {r"
                Documentation tools never merge an override's docstring with the inherited one:
                `help()`, Sphinx, Pyright hovers and `inspect.getdoc` show only the override's. A
                one-line override docstring over a base that documents its parameters, result and
                errors hides that contract from every reader of the override, even when the
                override changes none of it."},
            known_problems: Some(indoc::indoc! {r"
                Only ancestors defined earlier in the same file are seen. Entries are not compared:
                an override `Args:` section that omits one of the base's parameters is not flagged.
                Methods without `@override` are not inspected: enable mypy's `explicit-override`
                error code or Pyright's `reportImplicitOverride` to make every override explicit.
                A deferral phrase such as `See base class.` over a sectioned base is also reported
                by `deferral-docstring`."}),
            references: &[
                Reference {
                    title: "Google Python Style Guide §3.8.3.1: Overridden Methods",
                    url: "https://google.github.io/styleguide/pyguide.html#3831-overridden-methods",
                },
                Reference {
                    title: "inspect.getdoc: docstring inheritance",
                    url: "https://docs.python.org/3/library/inspect.html#inspect.getdoc",
                },
            ],
            examples: &[Example {
                language: Language::Python,
                flagged: indoc::indoc! {r#"
                    from typing import override

                    class SessionService:
                        def create(self, name: str) -> Session:
                            """Creates a session.

                            Args:
                                name: The session name, unique per user.

                            Returns:
                                The new session.
                            """
                            raise NotImplementedError

                    class InMemorySessionService(SessionService):
                        @override
                        def create(self, name: str) -> Session:
                            """Creates a session in memory."""
                            return Session(name)
                "#},
                flagged_span: r#""""Creates a session in memory.""""#,
                fixed: indoc::indoc! {r#"
                    from typing import override

                    class SessionService:
                        def create(self, name: str) -> Session:
                            """Creates a session.

                            Args:
                                name: The session name, unique per user.

                            Returns:
                                The new session.
                            """
                            raise NotImplementedError

                    class InMemorySessionService(SessionService):
                        @override
                        def create(self, name: str) -> Session:
                            """Creates a session in memory.

                            Args:
                                name: The session name, unique per user.

                            Returns:
                                The new session, lost when the process exits.
                            """
                            return Session(name)
                "#},
            }],
        },
    },
    target: RuleTarget::All,
    check: check_file,
};

/// The contract section kinds, in rendering order, with their canonical header.
const CONTRACT_SECTIONS: &[(DocstringSectionKind, &str)] = &[
    (DocstringSectionKind::Args, "Args"),
    (DocstringSectionKind::Returns, "Returns"),
    (DocstringSectionKind::Yields, "Yields"),
    (DocstringSectionKind::Raises, "Raises"),
];
const NOT_IMPLEMENTED_ERROR: &str = "NotImplementedError";

const FUNCTION: &str = "function";
const SECTIONS: &str = "sections";
const CLASS: &str = "class";

fn check_file(rule: &CodeRule, path: &Path, file: &ParsedFile, (): ()) -> Vec<Diagnostic> {
    let classes = ast::python::extract_classes(file);
    let mut diagnostics = Vec::new();
    for (index, class) in classes.iter().enumerate() {
        for method in class.methods() {
            if !method.has_override_decorator() {
                continue;
            }
            let Some(docstring) = method.docstring() else {
                continue;
            };
            let Some(ast::python::PythonInheritedDocstring {
                ancestor,
                docstring: inherited,
            }) = ast::python::inherited_method_docstring(&classes, index, &method.name)
            else {
                continue;
            };
            if docstring.copies_summary_of(&inherited) {
                continue;
            }
            let required = required_kinds(&inherited);
            let documented = documented_kinds(&docstring);
            let missing: Vec<String> = CONTRACT_SECTIONS
                .iter()
                .filter(|(kind, _)| required.contains(kind) && !documented.contains(kind))
                .map(|(_, header)| format!("`{header}:`"))
                .collect();
            if !missing.is_empty() {
                diagnostics.push(rule.diagnostic_at_node(
                    path,
                    &docstring.node(),
                    &[
                        (FUNCTION, &method.name),
                        (SECTIONS, &missing.join(" or ")),
                        (CLASS, &ancestor.name),
                    ],
                ));
            }
        }
    }
    diagnostics
}

/// The section kinds of the inherited docstring, without a `Raises:` section that only
/// documents an abstract method.
fn required_kinds(inherited: &PythonDocstring<'_>) -> Vec<DocstringSectionKind> {
    inherited
        .sections()
        .iter()
        .filter(|section| !is_abstract_raises(section))
        .map(|section| section.kind)
        .collect()
}

/// Returns true if `section` is a `Raises:` section whose entries are all `NotImplementedError`.
fn is_abstract_raises(section: &DocstringSection) -> bool {
    section.kind == DocstringSectionKind::Raises
        && !section.entries.is_empty()
        && section
            .entries
            .iter()
            .all(|entry| entry.bare_name() == NOT_IMPLEMENTED_ERROR)
}

/// The section kinds the override's docstring documents; a `Returns:` or `Yields:` section, or
/// a summary starting with a result verb, documents both result kinds.
fn documented_kinds(docstring: &PythonDocstring<'_>) -> Vec<DocstringSectionKind> {
    let mut kinds: Vec<DocstringSectionKind> = docstring
        .sections()
        .iter()
        .map(|section| section.kind)
        .collect();
    if docstring.describes_result() {
        kinds.extend([DocstringSectionKind::Returns, DocstringSectionKind::Yields]);
    }
    kinds
}

#[cfg(test)]
crate::test_utils::rule_test!(
    RULE,
    {
        Python => {
            pass: [
                no_docstring => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.
                            """

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            return b''
                "#,
                complete_override_docstring => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.

                            Returns:
                                The stored bytes.

                            Raises:
                                KeyError: The key is absent.
                            """

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk.

                            Args:
                                key: The file name.

                            Returns:
                                The file content.

                            Raises:
                                KeyError: The file is absent.
                            """
                "#,
                difference_over_unsectioned_base => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads the value stored under a key."""

                    class CachedStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads from the in-memory cache before the disk."""
                "#,
                undocumented_ancestor_exempt => r#"
                    from typing import override

                    class LocalStore(storage.Store):
                        def read(self, key: str) -> bytes:
                            return b''

                    class FileStore(LocalStore):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk."""
                "#,
                base_defined_after_subclass_exempt => r#"
                    from typing import override

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk."""

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.
                            """
                "#,
                module_level_function_exempt => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.
                            """

                    class FileStore(Store):
                        pass

                    @override
                    def read(key: str) -> bytes:
                        """Reads a value from disk."""
                "#,
                undecorated_override_exempt => r#"
                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.
                            """

                    class FileStore(Store):
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk."""
                "#,
                non_contract_sections_exempt => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Example:
                                store.read('alice')

                            Note:
                                Reads are not cached.
                            """

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk."""
                "#,
                other_header_of_same_kind_exempt => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.

                            Returns:
                                The stored bytes.
                            """

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk.

                            Arguments:
                                key: The file name.

                            Return:
                                The file content.
                            """
                "#,
                yields_for_returns_exempt => r#"
                    from typing import override

                    class Store:
                        def rows(self) -> Iterator[bytes]:
                            """Lists the stored values.

                            Returns:
                                An iterator over the values.
                            """

                    class FileStore(Store):
                        @override
                        def rows(self) -> Iterator[bytes]:
                            """Lists the stored files.

                            Yields:
                                The content of each file.
                            """
                "#,
                result_verb_summary_exempt => r#"
                    from typing import override

                    class Store:
                        def size(self) -> int:
                            """Counts the stored values.

                            Returns:
                                The number of values.
                            """

                    class FileStore(Store):
                        @override
                        def size(self) -> int:
                            """Returns the number of files in the directory."""
                "#,
                summary_copy_exempt => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.
                            """

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value."""
                "#,
                abstract_raises_exempt => r#"
                    from typing import override

                    class Store:
                        def clear(self) -> None:
                            """Removes every value.

                            Raises:
                                NotImplementedError: Subclasses implement it.
                            """
                            raise NotImplementedError

                    class FileStore(Store):
                        @override
                        def clear(self) -> None:
                            """Deletes every file in the directory."""
                "#,
            ],
            fail: [
                summary_over_sectioned_base => r#"
                    from typing import override

                    class SessionService:
                        def create(self, name: str, owner: str) -> Session:
                            """Creates a session.

                            Args:
                                name: The session name.
                                owner: The user who owns the session.

                            Returns:
                                The new session.

                            Raises:
                                ValueError: The name is taken.
                            """

                    class InMemorySessionService(SessionService):
                        @override
                        def create(self, name: str, owner: str) -> Session:
                            """Creates a session in memory."""
                "# => r#""""Creates a session in memory.""""#,
                unchanged_signature_missing_args => r#"
                    from typing import override

                    class Store:
                        def delete(self, key: str) -> None:
                            """Deletes a value.

                            Args:
                                key: The key to delete.
                            """

                    class FileStore(Store):
                        @override
                        def delete(self, key: str) -> None:
                            """Deletes the file and its parent directory when empty."""
                "# => r#""""Deletes the file and its parent directory when empty.""""#,
                sectioned_override_missing_raises => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.

                            Raises:
                                KeyError: The key is absent.
                            """

                    class FileStore(Store):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk.

                            Args:
                                key: The file name.
                            """
                "# => r#""""Reads a value from disk.

                    Args:
                        key: The file name.
                    """"#,
                raises_with_other_entries_required => r#"
                    from typing import override

                    class Store:
                        def clear(self) -> None:
                            """Removes every value.

                            Raises:
                                NotImplementedError: Subclasses implement it.
                                PermissionError: The store is read-only.
                            """

                    class FileStore(Store):
                        @override
                        def clear(self) -> None:
                            """Deletes every file in the directory."""
                "# => r#""""Deletes every file in the directory.""""#,
                undocumented_nearer_ancestor_skipped => r#"
                    from typing import override

                    class Store:
                        def read(self, key: str) -> bytes:
                            """Reads a value.

                            Args:
                                key: The key to read.
                            """

                    class LocalStore(Store):
                        def read(self, key: str) -> bytes:
                            return b''

                    class FileStore(LocalStore):
                        @override
                        def read(self, key: str) -> bytes:
                            """Reads a value from disk."""
                "# => r#""""Reads a value from disk.""""#,
            ],
        },
    }
);