{"id":46476525,"url":"https://github.com/qraqras/pydocstring","last_synced_at":"2026-04-02T21:30:10.361Z","repository":{"id":339198733,"uuid":"1159706334","full_name":"qraqras/pydocstring","owner":"qraqras","description":"A zero-dependency Rust parser for Python docstrings (Google \u0026 NumPy) — full AST with byte-precise source locations, built for linters and formatters.","archived":false,"fork":false,"pushed_at":"2026-03-28T10:33:39.000Z","size":469,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-28T13:54:58.535Z","etag":null,"topics":["docstring","docstrings","parser","python","rust"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/pydocstring","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qraqras.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-17T04:00:25.000Z","updated_at":"2026-03-22T13:32:22.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/qraqras/pydocstring","commit_stats":null,"previous_names":["qraqras/pydocstring"],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/qraqras/pydocstring","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qraqras%2Fpydocstring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qraqras%2Fpydocstring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qraqras%2Fpydocstring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qraqras%2Fpydocstring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qraqras","download_url":"https://codeload.github.com/qraqras/pydocstring/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qraqras%2Fpydocstring/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31316826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-02T12:59:32.332Z","status":"ssl_error","status_checked_at":"2026-04-02T12:54:48.875Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docstring","docstrings","parser","python","rust"],"created_at":"2026-03-06T07:09:35.268Z","updated_at":"2026-04-02T21:30:10.308Z","avatar_url":"https://github.com/qraqras.png","language":"Rust","readme":"# pydocstring\n\n[![Crates.io Version](https://img.shields.io/crates/v/pydocstring?color=FFC12d)](https://crates.io/crates/pydocstring)\n[![Crates.io MSRV](https://img.shields.io/crates/msrv/pydocstring?color=FFC12d)](https://blog.rust-lang.org/2025/02/20/Rust-1.85.0)\n[![PyPI - Version](https://img.shields.io/pypi/v/pydocstring-rs?color=0062A8)](https://pypi.org/project/pydocstring-rs/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pydocstring-rs?color=0062A8)](https://devguide.python.org/versions/)\n\nA zero-dependency Rust parser for Python docstrings (Google / NumPy style).\n\nProduces a **unified syntax tree** with **byte-precise source locations** on every token — designed as infrastructure for linters and formatters.\n\nPython bindings are also available as [`pydocstring-rs`](https://pypi.org/project/pydocstring-rs/).\n\n## Features\n\n- **Full syntax tree** — builds a complete AST, not just extracted fields; traverse it with the built-in `Visitor` + `walk`\n- **Typed nodes per style** — style-specific accessors like `GoogleArg`, `NumPyParameter` with full type safety\n- **Byte-precise source locations** — every token carries its exact byte range for pinpoint diagnostics\n- **Zero dependencies** — pure Rust, no external crates, no regex\n- **Error-resilient** — never panics; malformed input still yields a best-effort tree\n- **Style auto-detection** — hand it a docstring, get back `Style::Google`, `Style::NumPy`, or `Style::Plain`\n\n## Installation\n\n```toml\n[dependencies]\npydocstring = \"0.1.9\"\n```\n\n## Usage\n\n### Parsing\n\n```rust\nuse pydocstring::parse::google::{parse_google, GoogleDocstring, GoogleSectionKind};\n\nlet input = \"Summary.\\n\\nArgs:\\n    x (int): The value.\\n    y (int): Another value.\";\nlet result = parse_google(input);\nlet doc = GoogleDocstring::cast(result.root()).unwrap();\n\nprintln!(\"{}\", doc.summary().unwrap().text(result.source()));\n\nfor section in doc.sections() {\n    if section.section_kind(result.source()) == GoogleSectionKind::Args {\n        for arg in section.args() {\n            println!(\"{}: {}\",\n                arg.name().text(result.source()),\n                arg.r#type().map(|t| t.text(result.source())).unwrap_or(\"\"));\n        }\n    }\n}\n```\n\nNumPy style works the same way — use `parse_numpy` / `NumPyDocstring` instead.\n\n### Style Auto-Detection\n\n```rust\nuse pydocstring::parse::{detect_style, Style};\n\nassert_eq!(detect_style(\"Summary.\\n\\nArgs:\\n    x: Desc.\"), Style::Google);\nassert_eq!(detect_style(\"Summary.\\n\\nParameters\\n----------\\nx : int\"), Style::NumPy);\nassert_eq!(detect_style(\"Just a summary.\"), Style::Plain);\n```\n\n`Style::Plain` covers docstrings with no recognised section markers: summary-only,\nsummary + extended summary, and unrecognised styles such as Sphinx.\n\n### Unified Auto-Detecting Parser\n\nUse `parse()` to let the library detect the style and parse in one step:\n\n```rust\nuse pydocstring::parse::parse;\nuse pydocstring::syntax::SyntaxKind;\n\nlet result = parse(\"Summary.\\n\\nArgs:\\n    x: Desc.\");\nassert_eq!(result.root().kind(), SyntaxKind::GOOGLE_DOCSTRING);\n\nlet result = parse(\"Just a summary.\");\nassert_eq!(result.root().kind(), SyntaxKind::PLAIN_DOCSTRING);\n```\n\n### Source Locations\n\nEvery token carries byte offsets for precise diagnostics:\n\n```rust\nuse pydocstring::parse::google::{parse_google, GoogleDocstring, GoogleSectionKind};\n\nlet result = parse_google(\"Summary.\\n\\nArgs:\\n    x (int): The value.\");\nlet doc = GoogleDocstring::cast(result.root()).unwrap();\n\nfor section in doc.sections() {\n    if section.section_kind(result.source()) == GoogleSectionKind::Args {\n        for arg in section.args() {\n            let name = arg.name();\n            println!(\"'{}' at byte {}..{}\",\n                name.text(result.source()), name.range().start(), name.range().end());\n        }\n    }\n}\n```\n\n### Syntax Tree\n\nThe parse result is a tree of `SyntaxNode` (branches) and `SyntaxToken` (leaves), each tagged with a `SyntaxKind`. Use `pretty_print()` to visualize:\n\n```rust\nuse pydocstring::parse::google::parse_google;\n\nlet result = parse_google(\"Summary.\\n\\nArgs:\\n    x (int): The value.\");\nprintln!(\"{}\", result.pretty_print());\n```\n\n```text\nGOOGLE_DOCSTRING@0..42 {\n  SUMMARY: \"Summary.\"@0..8\n  GOOGLE_SECTION@10..42 {\n    GOOGLE_SECTION_HEADER@10..15 {\n      NAME: \"Args\"@10..14\n      COLON: \":\"@14..15\n    }\n    GOOGLE_ARG@20..42 {\n      NAME: \"x\"@20..21\n      OPEN_BRACKET: \"(\"@22..23\n      TYPE: \"int\"@23..26\n      CLOSE_BRACKET: \")\"@26..27\n      COLON: \":\"@27..28\n      DESCRIPTION: \"The value.\"@29..39\n    }\n  }\n}\n```\n\n### Visitor Pattern\n\nWalk the tree with the `Visitor` trait for style-agnostic analysis:\n\n```rust\nuse pydocstring::syntax::{Visitor, walk, SyntaxToken, SyntaxKind};\nuse pydocstring::parse::google::parse_google;\n\nstruct NameCollector\u003c'a\u003e {\n    source: \u0026'a str,\n    names: Vec\u003cString\u003e,\n}\n\nimpl Visitor for NameCollector\u003c'_\u003e {\n    fn visit_token(\u0026mut self, token: \u0026SyntaxToken) {\n        if token.kind() == SyntaxKind::NAME {\n            self.names.push(token.text(self.source).to_string());\n        }\n    }\n}\n\nlet result = parse_google(\"Summary.\\n\\nArgs:\\n    x: Desc.\\n    y: Desc.\");\nlet mut collector = NameCollector { source: result.source(), names: vec![] };\nwalk(result.root(), \u0026mut collector);\nassert_eq!(collector.names, vec![\"Args\", \"x\", \"y\"]);\n```\n\n### Style-Independent Model (IR)\n\nConvert any parsed docstring into a style-independent intermediate representation for analysis or transformation:\n\n```rust\nuse pydocstring::parse::google::{parse_google, to_model::to_model};\n\nlet parsed = parse_google(\"Summary.\\n\\nArgs:\\n    x (int): The value.\\n\");\nlet doc = to_model(\u0026parsed).unwrap();\n\nassert_eq!(doc.summary.as_deref(), Some(\"Summary.\"));\nfor section in \u0026doc.sections {\n    if let pydocstring::model::Section::Parameters(params) = section {\n        assert_eq!(params[0].names, vec![\"x\"]);\n        assert_eq!(params[0].type_annotation.as_deref(), Some(\"int\"));\n    }\n}\n```\n\n### Emitting (Code Generation)\n\nRe-emit a `Docstring` model in any style — useful for style conversion or formatting:\n\n```rust\nuse pydocstring::model::{Docstring, Section, Parameter};\nuse pydocstring::emit::google::emit_google;\nuse pydocstring::emit::numpy::emit_numpy;\n\nlet doc = Docstring {\n    summary: Some(\"Brief summary.\".into()),\n    sections: vec![Section::Parameters(vec![Parameter {\n        names: vec![\"x\".into()],\n        type_annotation: Some(\"int\".into()),\n        description: Some(\"The value.\".into()),\n        is_optional: false,\n        default_value: None,\n    }])],\n    ..Default::default()\n};\n\nlet google = emit_google(\u0026doc);\nassert!(google.contains(\"Args:\"));\n\nlet numpy = emit_numpy(\u0026doc);\nassert!(numpy.contains(\"Parameters\\n----------\"));\n```\n\nCombine parsing and emitting to convert between styles:\n\n```rust\nuse pydocstring::parse::google::{parse_google, to_model::to_model};\nuse pydocstring::emit::numpy::emit_numpy;\n\nlet parsed = parse_google(\"Summary.\\n\\nArgs:\\n    x (int): The value.\\n\");\nlet doc = to_model(\u0026parsed).unwrap();\nlet numpy_text = emit_numpy(\u0026doc);\nassert!(numpy_text.contains(\"Parameters\\n----------\"));\n```\n\n## Supported Sections\n\nBoth styles support the following section categories. Typed accessor methods are available on each style's section node.\n\n| Category                          | Google                                   | NumPy                                   |\n|-----------------------------------|------------------------------------------|-----------------------------------------|\n| Parameters                        | `args()` → `GoogleArg`                   | `parameters()` → `NumPyParameter`       |\n| Returns                           | `returns()` → `GoogleReturns`            | `returns()` → `NumPyReturns`            |\n| Yields                            | `yields()` → `GoogleYields`              | `yields()` → `NumPyYields`              |\n| Raises                            | `exceptions()` → `GoogleException`       | `exceptions()` → `NumPyException`       |\n| Warns                             | `warnings()` → `GoogleWarning`           | `warnings()` → `NumPyWarning`           |\n| See Also                          | `see_also_items()` → `GoogleSeeAlsoItem` | `see_also_items()` → `NumPySeeAlsoItem` |\n| Attributes                        | `attributes()` → `GoogleAttribute`       | `attributes()` → `NumPyAttribute`       |\n| Methods                           | `methods()` → `GoogleMethod`             | `methods()` → `NumPyMethod`             |\n| Free text (Notes, Examples, etc.) | `body_text()`                            | `body_text()`                           |\n\nRoot-level accessors: `summary()`, `extended_summary()` (NumPy also has `deprecation()`). `PlainDocstring` exposes only `summary()` and `extended_summary()`.\n\n## Development\n\n```bash\ncargo build\ncargo test\ncargo run --example parse_auto\ncargo run --example parse_google\ncargo run --example parse_numpy\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqraqras%2Fpydocstring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqraqras%2Fpydocstring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqraqras%2Fpydocstring/lists"}