python-contract-docstrings

Write Google-style docstrings documenting Python function contracts, preconditions, and error behavior.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill python-contract-docstrings-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-contract-docstrings
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/python/contract-docstrings
Command: npx skills add https://github.com/asarchami/dotfiles --skill python-contract-docstrings-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Most docstrings only describe what a function does, leaving callers unaware of preconditions, raised exceptions, and silently swallowed errors. This Skill analyzes Python source code and writes docstrings that document each function's actual contract — what it demands from callers and what it guarantees. ## Core Features & Use Cases - Contract Analysis: Traces code paths, callees, and callers to identify input invariants, errors raised on violation, errors from external state, and silenced exceptions. - Google-Style Docstrings: Adds structured Contract sections covering Preconditions, Raises, and Silences without changing any code behavior. - Use Case: Point the Skill at a module with undocumented error handling, and it will read every function, trace what exceptions callees raise, and document that a ValueError is raised on empty input while OSError during cache writes is silently logged and ignored. ## Quick Start Ask the AI to analyze the functions in your Python file and write contract docstrings documenting preconditions, raised errors, and silenced exceptions.

Frequently Asked Questions about python-contract-docstrings

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

FAQPage Schema
How do I write docstrings that document function preconditions in Python?

Analyze each function's guard clauses, assertions, and implicit assumptions to identify preconditions, then document them in a Contract section of a Google-style docstring. Include what exception is raised when each precondition is violated, whether explicit or implicit.

How to document exceptions raised by Python functions?

Trace the function's code paths and its callees to find every exception that can propagate, then list them in a Raises section with the triggering condition. Distinguish explicit guard-clause errors from implicit failures like TypeError or KeyError.

What is a contract docstring versus a regular docstring?

A regular docstring describes what a function does, while a contract docstring describes what it demands and guarantees. It documents input invariants, errors raised on violation, errors from external state, and silenced exceptions.

Does writing contract docstrings change my code behavior?

No, the process only adds or replaces docstrings and never refactors code. The workflow explicitly forbids behavior changes and recommends running the test suite and type checker afterward to verify nothing broke.

When should I skip writing contract docstrings for a function?

Skip trivial functions such as one-liners, simple property accessors, and __repr__ methods where the contract is self-evident. Also omit any contract detail you cannot verify by reading the code, since accuracy takes priority over completeness.