pydocstring 0.3.0

A zero-dependency Rust parser for Python docstrings (Google and NumPy styles) with a unified syntax tree and byte-precise source locations
Documentation
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
//! Style-independent typed views over the unified syntax tree.
//!
//! Every docstring style produces the same node vocabulary
//! ([`SyntaxKind::DOCUMENT`] / [`SyntaxKind::SECTION`] /
//! [`SyntaxKind::ENTRY`] / [`SyntaxKind::DIRECTIVE`] /
//! [`SyntaxKind::CITATION`]), so one set of zero-copy views — [`Document`],
//! [`Section`], [`Entry`], [`Directive`], [`Citation`], [`DefaultMarker`] — walks
//! any of them. This is the single code path over docstring structure: parse
//! with [`parse`](crate::parse::parse) (auto-detecting the style) and
//! traverse sections and entries uniformly, without per-style types.
//!
//! Every view holds a reference to the [`Parsed`] result it came from, so no
//! accessor takes a `source` argument: `entry.name().unwrap().text()`.
//!
//! # Example
//!
//! The same traversal works for a Google-style and a NumPy-style docstring:
//!
//! ```rust
//! use pydocstring::model::SectionKind;
//! use pydocstring::parse::parse;
//! use pydocstring::parse::unified::Document;
//!
//! fn parameter_names(source: &str) -> Vec<String> {
//!     let parsed = parse(source);
//!     let doc = Document::new(&parsed);
//!     doc.sections()
//!         .filter(|s| s.kind() == SectionKind::Parameters)
//!         .flat_map(|s| s.entries().collect::<Vec<_>>())
//!         .filter_map(|e| e.name().map(|n| n.text().to_owned()))
//!         .collect()
//! }
//!
//! let google = "Summary.\n\nArgs:\n    x (int): The value.\n    y: Another.\n";
//! let numpy = "Summary.\n\nParameters\n----------\nx : int\n    The value.\ny\n    Another.\n";
//! assert_eq!(parameter_names(google), vec!["x", "y"]);
//! assert_eq!(parameter_names(numpy), vec!["x", "y"]);
//! ```
//!
//! Repeatable markers are exposed per occurrence — [`Entry::defaults`] yields
//! one [`DefaultMarker`] per `default …` marker; [`Entry::default_value`] is
//! the first-occurrence shorthand that matches the model's normalization
//! rule:
//!
//! ```rust
//! use pydocstring::parse::parse;
//! use pydocstring::parse::unified::Document;
//!
//! let parsed = parse("Summary.\n\nParameters\n----------\nx : int, default 1, default 2\n    Desc.\n");
//! let doc = Document::new(&parsed);
//! let entry = doc.sections().next().unwrap().entries().next().unwrap();
//! assert_eq!(entry.defaults().count(), 2);
//! assert_eq!(entry.default_value().unwrap().text(), "1");
//! ```

use crate::model::FreeSectionKind;
use crate::model::SectionKind;
use crate::parse::Style;
use crate::parse::google::kind::GoogleSectionKind;
use crate::parse::numpy::kind::NumPySectionKind;
use crate::parse::text_block::TextBlock;
use crate::parse::text_block::find_text_block;
use crate::parse::token_ref::TokenRef;
use crate::syntax::Parsed;
use crate::syntax::SyntaxKind;
use crate::syntax::SyntaxNode;
use crate::text::TextRange;

// =============================================================================
// Document
// =============================================================================

/// Style-independent view of a parsed docstring root.
///
/// Construct with [`Document::new`] from any [`Parsed`] result, whatever the
/// source style.
#[derive(Debug, Clone, Copy)]
pub struct Document<'a> {
    parsed: &'a Parsed,
}

impl<'a> Document<'a> {
    /// View the root of `parsed` as a style-independent document.
    pub fn new(parsed: &'a Parsed) -> Self {
        debug_assert_eq!(parsed.root().kind(), SyntaxKind::DOCUMENT);
        Self { parsed }
    }

    /// The style the docstring was parsed as.
    pub fn style(&self) -> Style {
        self.parsed.style()
    }

    /// Access the underlying `SyntaxNode`.
    pub fn syntax(&self) -> &'a SyntaxNode {
        self.parsed.root()
    }

    /// Brief summary block, if present.
    pub fn summary(&self) -> Option<TextBlock<'a>> {
        find_text_block(self.parsed, self.parsed.root(), SyntaxKind::SUMMARY)
    }

    /// Extended summary block, if present.
    pub fn extended_summary(&self) -> Option<TextBlock<'a>> {
        find_text_block(self.parsed, self.parsed.root(), SyntaxKind::EXTENDED_SUMMARY)
    }

    /// Iterate over document-level directives (e.g. `.. deprecated:: 1.0`).
    pub fn directives(&self) -> impl Iterator<Item = Directive<'a>> {
        let parsed = self.parsed;
        self.parsed
            .root()
            .nodes(SyntaxKind::DIRECTIVE)
            .map(move |node| Directive { parsed, node })
    }

    /// Iterate over all sections, in source order.
    pub fn sections(&self) -> impl Iterator<Item = Section<'a>> {
        let parsed = self.parsed;
        self.parsed
            .root()
            .nodes(SyntaxKind::SECTION)
            .map(move |node| Section { parsed, node })
    }

    /// Iterate over stray-prose paragraph blocks (`PARAGRAPH` nodes) between
    /// sections, in source order.
    pub fn paragraphs(&self) -> impl Iterator<Item = TextBlock<'a>> {
        let parsed = self.parsed;
        self.parsed
            .root()
            .nodes(SyntaxKind::PARAGRAPH)
            .filter_map(move |node| TextBlock::cast(parsed, node))
    }
}

// =============================================================================
// Section
// =============================================================================

/// Style-independent view of a `SECTION` node.
#[derive(Debug, Clone, Copy)]
pub struct Section<'a> {
    parsed: &'a Parsed,
    node: &'a SyntaxNode,
}

impl<'a> Section<'a> {
    /// Try to cast a `SyntaxNode` from `parsed`'s tree into this typed
    /// wrapper.
    pub fn cast(parsed: &'a Parsed, node: &'a SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::SECTION).then_some(Self { parsed, node })
    }

    /// Access the underlying `SyntaxNode`.
    pub fn syntax(&self) -> &'a SyntaxNode {
        self.node
    }

    /// The source range of this section.
    pub fn range(&self) -> &'a TextRange {
        self.node.range()
    }

    /// The header name text (e.g. `"Args"`, `"Parameters"`).
    pub fn header_name(&self) -> &'a str {
        self.node
            .find_node(SyntaxKind::SECTION_HEADER)
            .expect("SECTION must have a SECTION_HEADER child")
            .required_token(SyntaxKind::NAME)
            .text(self.parsed.source())
    }

    /// The style-independent [`SectionKind`] of this section, resolved from
    /// the header name via the source style's section-name table.
    pub fn kind(&self) -> SectionKind {
        let name = self.header_name();
        let lower = name.to_ascii_lowercase();
        match self.parsed.style() {
            Style::Google => GoogleSectionKind::from_name(&lower).to_section_kind(name),
            Style::NumPy => NumPySectionKind::from_name(&lower).to_section_kind(name),
            // Plain docstrings produce no SECTION nodes; unreachable in
            // practice, but total: report an unknown free-text section.
            _ => SectionKind::FreeText(FreeSectionKind::Unknown(name.to_owned())),
        }
    }

    /// Iterate over the section's `ENTRY` nodes, in source order.
    pub fn entries(&self) -> impl Iterator<Item = Entry<'a>> {
        let parsed = self.parsed;
        self.node
            .nodes(SyntaxKind::ENTRY)
            .map(move |node| Entry { parsed, node })
    }

    /// Free-text body block (the `DESCRIPTION` child of a free-text
    /// section), if present.
    pub fn body(&self) -> Option<TextBlock<'a>> {
        find_text_block(self.parsed, self.node, SyntaxKind::DESCRIPTION)
    }

    /// Iterate over the section's `CITATION` nodes (References sections).
    pub fn citations(&self) -> impl Iterator<Item = Citation<'a>> {
        let parsed = self.parsed;
        self.node
            .nodes(SyntaxKind::CITATION)
            .map(move |node| Citation { parsed, node })
    }
}

// =============================================================================
// Entry
// =============================================================================

/// Style-independent view of an `ENTRY` node (a parameter, return, yield,
/// exception, warning, attribute, method, or "See Also" item).
#[derive(Debug, Clone, Copy)]
pub struct Entry<'a> {
    parsed: &'a Parsed,
    node: &'a SyntaxNode,
}

impl<'a> Entry<'a> {
    /// Try to cast a `SyntaxNode` from `parsed`'s tree into this typed
    /// wrapper.
    pub fn cast(parsed: &'a Parsed, node: &'a SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::ENTRY).then_some(Self { parsed, node })
    }

    /// Access the underlying `SyntaxNode`.
    pub fn syntax(&self) -> &'a SyntaxNode {
        self.node
    }

    /// The source range of this entry.
    pub fn range(&self) -> &'a TextRange {
        self.node.range()
    }

    /// All `NAME` tokens (entries can declare several comma-separated names).
    pub fn names(&self) -> impl Iterator<Item = TokenRef<'a>> {
        let parsed = self.parsed;
        self.node
            .tokens(SyntaxKind::NAME)
            .map(move |t| TokenRef::new(parsed, t))
    }

    /// The first `NAME` token, if any (exception/warning entries carry a
    /// type instead of a name).
    pub fn name(&self) -> Option<TokenRef<'a>> {
        self.names().next()
    }

    /// The `TYPE` annotation token, if present: a parameter / attribute
    /// type, a return / yield type, or the exception / warning type name
    /// of a raises / warns entry.
    pub fn type_annotation(&self) -> Option<TokenRef<'a>> {
        self.node
            .find_token(SyntaxKind::TYPE)
            .map(|t| TokenRef::new(self.parsed, t))
    }

    /// Description text block, if present.
    pub fn description(&self) -> Option<TextBlock<'a>> {
        find_text_block(self.parsed, self.node, SyntaxKind::DESCRIPTION)
    }

    /// Whether the entry carries at least one `optional` marker.
    pub fn is_optional(&self) -> bool {
        self.optionals().next().is_some()
    }

    /// All `optional` marker tokens, one per occurrence, in source order.
    pub fn optionals(&self) -> impl Iterator<Item = TokenRef<'a>> {
        let parsed = self.parsed;
        self.node
            .tokens(SyntaxKind::OPTIONAL)
            .map(move |t| TokenRef::new(parsed, t))
    }

    /// All `default …` markers, one [`DefaultMarker`] per occurrence, in
    /// source order.
    pub fn defaults(&self) -> impl Iterator<Item = DefaultMarker<'a>> {
        let parsed = self.parsed;
        self.node
            .nodes(SyntaxKind::DEFAULT)
            .map(move |node| DefaultMarker { parsed, node })
    }

    /// The first `default …` marker's value token, if present.
    ///
    /// First occurrence wins — the same normalization rule the model layer
    /// applies. Use [`Entry::defaults`] to see every occurrence.
    pub fn default_value(&self) -> Option<TokenRef<'a>> {
        self.defaults().next().and_then(|d| d.value())
    }
}

// =============================================================================
// DefaultMarker
// =============================================================================

/// Typed view of one `DEFAULT` marker node (`default X` / `default=X` /
/// `default: X` inside a type annotation).
#[derive(Debug, Clone, Copy)]
pub struct DefaultMarker<'a> {
    pub(crate) parsed: &'a Parsed,
    pub(crate) node: &'a SyntaxNode,
}

impl<'a> DefaultMarker<'a> {
    /// Try to cast a `SyntaxNode` from `parsed`'s tree into this typed
    /// wrapper.
    pub fn cast(parsed: &'a Parsed, node: &'a SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::DEFAULT).then_some(Self { parsed, node })
    }

    /// Access the underlying `SyntaxNode`.
    pub fn syntax(&self) -> &'a SyntaxNode {
        self.node
    }

    /// The source range of this marker.
    pub fn range(&self) -> &'a TextRange {
        self.node.range()
    }

    /// The `default` keyword token.
    pub fn keyword(&self) -> TokenRef<'a> {
        TokenRef::new(self.parsed, self.node.required_token(SyntaxKind::DEFAULT_KEYWORD))
    }

    /// The `=` / `:` separator token, if present.
    pub fn separator(&self) -> Option<TokenRef<'a>> {
        self.node
            .find_token(SyntaxKind::DEFAULT_SEPARATOR)
            .map(|t| TokenRef::new(self.parsed, t))
    }

    /// The value token, if present (zero-length placeholders excluded).
    pub fn value(&self) -> Option<TokenRef<'a>> {
        self.node
            .find_token(SyntaxKind::DEFAULT_VALUE)
            .map(|t| TokenRef::new(self.parsed, t))
    }
}

// =============================================================================
// Directive
// =============================================================================

/// Style-independent view of a `DIRECTIVE` node
/// (e.g. `.. deprecated:: 1.6.0`).
#[derive(Debug, Clone, Copy)]
pub struct Directive<'a> {
    pub(crate) parsed: &'a Parsed,
    pub(crate) node: &'a SyntaxNode,
}

impl<'a> Directive<'a> {
    /// Try to cast a `SyntaxNode` from `parsed`'s tree into this typed
    /// wrapper.
    pub fn cast(parsed: &'a Parsed, node: &'a SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::DIRECTIVE).then_some(Self { parsed, node })
    }

    /// Access the underlying `SyntaxNode`.
    pub fn syntax(&self) -> &'a SyntaxNode {
        self.node
    }

    /// The source range of this directive.
    pub fn range(&self) -> &'a TextRange {
        self.node.range()
    }

    /// The directive name token (e.g. `deprecated`).
    pub fn name(&self) -> TokenRef<'a> {
        TokenRef::new(self.parsed, self.node.required_token(SyntaxKind::DIRECTIVE_NAME))
    }

    /// The directive argument token (e.g. the version of a
    /// `.. deprecated::`), if present.
    pub fn argument(&self) -> Option<TokenRef<'a>> {
        self.node
            .find_token(SyntaxKind::ARGUMENT)
            .map(|t| TokenRef::new(self.parsed, t))
    }

    /// The directive body / description block, if present.
    pub fn description(&self) -> Option<TextBlock<'a>> {
        find_text_block(self.parsed, self.node, SyntaxKind::DESCRIPTION)
    }
}

// =============================================================================
// Citation
// =============================================================================

/// Style-independent view of a `CITATION` node (`.. [label] content` in a
/// References section, or a plain-text reference line).
#[derive(Debug, Clone, Copy)]
pub struct Citation<'a> {
    pub(crate) parsed: &'a Parsed,
    pub(crate) node: &'a SyntaxNode,
}

impl<'a> Citation<'a> {
    /// Try to cast a `SyntaxNode` from `parsed`'s tree into this typed
    /// wrapper.
    pub fn cast(parsed: &'a Parsed, node: &'a SyntaxNode) -> Option<Self> {
        (node.kind() == SyntaxKind::CITATION).then_some(Self { parsed, node })
    }

    /// Access the underlying `SyntaxNode`.
    pub fn syntax(&self) -> &'a SyntaxNode {
        self.node
    }

    /// The source range of this citation.
    pub fn range(&self) -> &'a TextRange {
        self.node.range()
    }

    /// The citation label token (`1`, `CIT2002`, `#f1`, …), if present.
    pub fn label(&self) -> Option<TokenRef<'a>> {
        self.node
            .find_token(SyntaxKind::LABEL)
            .map(|t| TokenRef::new(self.parsed, t))
    }

    /// The citation content block, if present.
    pub fn description(&self) -> Option<TextBlock<'a>> {
        find_text_block(self.parsed, self.node, SyntaxKind::DESCRIPTION)
    }
}