docstring

Write PyTorch docstrings with Sphinx/reStructuredText formatting and examples.

2|Updated May 9, 2025
One-click install
npx skills add https://github.com/linjunhui/cuda-learning --skill docstring-linjunhui
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: docstring
Source: https://github.com/linjunhui/cuda-learning/tree/main/Pytorch学习/pytorch/.claude/skills/docstring
Command: npx skills add https://github.com/linjunhui/cuda-learning --skill docstring-linjunhui

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill ensures that all PyTorch functions and methods have high-quality, consistent docstrings that adhere to project conventions, saving developers time and improving code readability and maintainability.

Core Features & Use Cases

  • Convention Adherence: Guides the creation of docstrings following PyTorch's Sphinx/reStructuredText format, including raw strings and specific section ordering.
  • Comprehensive Documentation: Ensures inclusion of function signatures, brief descriptions, mathematical formulas, cross-references, arguments, returns, and examples.
  • Automated Formatting: Helps structure docstrings with correct indentation, Sphinx roles (e.g., :func:, :class:), and LaTeX math notation for tensor shapes.
  • Use Case: You've just implemented a new feature in PyTorch and need to document its public API. Instead of spending hours meticulously formatting the docstring and ensuring it meets all PyTorch standards, you use this Skill to generate a perfectly structured and formatted docstring, allowing you to focus on the code itself.

Quick Start

Write a PyTorch docstring for the my_new_function(input: Tensor, dim: int = -1) -> Tensor function, describing its purpose as applying a custom activation and providing an example.

Frequently Asked Questions about docstring

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write PyTorch docstrings that follow project conventions?

PyTorch docstrings require Sphinx/reStructuredText format with raw strings, specific section ordering (signature, description, args, returns, examples), and Sphinx roles like `:func:` and `:class:`. This Skill generates properly structured docstrings enforcing these conventions, including mathematical formulas and cross-references, so your documentation stays consistent with PyTorch standards.

What sections should a PyTorch function docstring include?

PyTorch docstrings must contain function signature, brief description, mathematical formulas, cross-references, notes or warnings, Args, Keyword Args, Returns, and Examples sections. This Skill ensures all required sections are present and correctly formatted with proper indentation, Sphinx roles, and LaTeX notation for tensor shapes.

Can I automate docstring generation for PyTorch functions and methods?

Yes. This Skill generates complete, convention-compliant docstrings for PyTorch functions and methods automatically, handling raw-string formatting, section structure, and Sphinx markup. You provide the function signature and purpose; it produces a fully formatted docstring ready for torch/_tensor_docs.py, torch/nn/functional.py, and similar PyTorch codebases.

How do I ensure docstring examples work correctly in PyTorch documentation?

This Skill generates example code within PyTorch docstrings using Sphinx code-block directives and proper tensor shape notation in LaTeX. It structures examples to demonstrate function usage clearly, ensuring they integrate seamlessly with PyTorch's Sphinx documentation build and cross-reference system.

What's the best way to format LaTeX math and cross-references in PyTorch docstrings?

PyTorch docstrings use reStructuredText inline math notation (`:math:` role) for formulas and Sphinx cross-reference roles (`:func:`, `:class:`, `:meth:`) for links. This Skill automatically applies correct syntax and indentation for both mathematical expressions and references within docstrings.

Do I need to manually format Sphinx roles and raw strings in PyTorch docstrings?

No. This Skill handles raw-string prefixes, Sphinx role syntax (`:func:`, `:class:`), section formatting, and indentation automatically. You describe what the function does; it produces a fully formatted, Sphinx-compliant docstring following PyTorch conventions exactly.