pydocstring
A fast, zero-dependency Rust parser for Python docstrings with full AST and source location tracking.
Features
- Zero external dependencies — pure Rust implementation
- Docstring styles:
- Google style — fully supported
- NumPy style — fully supported
- Accurate source spans (byte offsets) on every AST node
- Diagnostic-based error reporting — partial results + diagnostics, never panics
- Style auto-detection — automatically identifies NumPy or Google style
- Comprehensive test coverage (140+ tests)
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Quick Start
NumPy Style
use parse_numpy;
let docstring = r#"
Calculate the area of a rectangle.
This function takes the width and height of a rectangle
and returns its area.
Parameters
----------
width : float
The width of the rectangle.
height : float
The height of the rectangle.
Returns
-------
float
The area of the rectangle.
Raises
------
ValueError
If width or height is negative.
"#;
let result = parse_numpy;
println!;
for item in &result.items
Google Style
use parse_google;
let docstring = r#"
Calculate the area of a rectangle.
This function takes the width and height of a rectangle
and returns its area.
Args:
width (float): The width of the rectangle.
height (float): The height of the rectangle.
Returns:
float: The area of the rectangle.
Raises:
ValueError: If width or height is negative.
"#;
let result = parse_google;
println!;
for item in &result.items
Style Auto-Detection
use ;
let numpy_doc = "Summary.\n\nParameters\n----------\nx : int\n Desc.";
assert_eq!;
let google_doc = "Summary.\n\nArgs:\n x: Desc.";
assert_eq!;
Diagnostic-Based Error Handling
Parsers always return a result — even for malformed input. Diagnostics are collected alongside the best-effort AST:
use parse_google;
let result = parse_google;
if result.has_errors
// The AST is still available
println!;
Source Locations
Every AST node carries a TextRange (byte offsets) so linters can report precise positions:
use parse_numpy;
use LineIndex;
let docstring = "Summary.\n\nParameters\n----------\nx : int\n Desc.";
let result = parse_numpy;
for item in &result.items
Supported Sections
NumPy Style
| Section | Method | Return Type |
|---|---|---|
| Parameters | parameters() |
Vec<&NumPyParameter> |
| Other Parameters | other_parameters() |
Vec<&NumPyParameter> |
| Returns | returns() |
Vec<&NumPyReturns> |
| Yields | yields() |
Vec<&NumPyReturns> |
| Receives | receives() |
Vec<&NumPyParameter> |
| Raises | raises() |
Vec<&NumPyException> |
| Warns | warns() |
Vec<&NumPyWarning> |
| Warnings | warnings() |
Option<&TextRange> |
| See Also | see_also() |
Vec<&SeeAlsoItem> |
| Notes | notes() |
Option<&TextRange> |
| References | references() |
Vec<&NumPyReference> |
| Examples | examples() |
Option<&TextRange> |
| Attributes | attributes() |
Vec<&NumPyAttribute> |
| Methods | methods() |
Vec<&NumPyMethod> |
Additionally, NumPyDocstring has fields: summary, deprecation, extended_summary.
Google Style
| Section | Method | Return Type |
|---|---|---|
| Args | args() |
Vec<&GoogleArgument> |
| Keyword Args | keyword_args() |
Vec<&GoogleArgument> |
| Other Parameters | other_parameters() |
Vec<&GoogleArgument> |
| Returns | returns() |
Vec<&GoogleReturns> |
| Yields | yields() |
Vec<&GoogleReturns> |
| Receives | receives() |
Vec<&GoogleArgument> |
| Raises | raises() |
Vec<&GoogleException> |
| Warns | warns() |
Vec<&GoogleWarning> |
| Warnings | warnings() |
Option<&TextRange> |
| See Also | see_also() |
Vec<&GoogleSeeAlsoItem> |
| Notes | notes() |
Option<&TextRange> |
| References | references() |
Option<&TextRange> |
| Examples | examples() |
Option<&TextRange> |
| Attributes | attributes() |
Vec<&GoogleAttribute> |
| Methods | methods() |
Vec<&GoogleMethod> |
| Todo | todo() |
Option<&TextRange> |
Additionally, GoogleDocstring has fields: summary, description, and admonition sections (Attention, Caution, Danger, etc.).
Development
# Build
# Run tests
# Run examples
License
MIT