doc string changes with sample to generate doc strings for packages/modules

This commit is contained in:
2026-02-27 14:10:15 +05:30
parent fbe9e1f109
commit ed0fac8b3d

View File

@@ -1,86 +1,208 @@
""" """
# doc-forge Renderer-agnostic Python documentation compiler that converts docstrings into
structured documentation for both humans (MkDocs) and machines (MCP / AI agents).
`doc-forge` is a renderer-agnostic Python documentation compiler designed for `doc-forge` statically analyzes your source code, builds a semantic model of
speed, flexibility, and beautiful output. It decouples the introspection of modules and objects, and renders that model into documentation outputs without
your code from the rendering process, allowing you to generate documentation executing your code.
for various platforms (starting with MkDocs) from a single internal models.
---
## Core Philosophy ## Core Philosophy
`doc-forge` operates on two fundamental principles: `doc-forge` follows a compiler architecture:
1. **The Atomic Unit is a Python Import Path**: Documentation is organized around the semantic structure of your code (e.g., `mypackage.utils`), not the filesystem. 1. **Front-end (Introspection)**
2. **The Documentation Compiler Paradigm**: We separate documentation into three distinct phases: Static analysis of modules, classes, functions, signatures, and docstrings.
- **Front-end (Introspection)**: Static analysis of source code and docstrings.
- **Middle-end (Semantic Model)**: A renderer-neutral internal representation.
- **Back-end (Renderers)**: Generation of human-facing (MkDocs) or machine-facing (MCP) outputs.
## Documentation Design 2. **Middle-end (Semantic Model)**
Renderer-neutral structured representation of your API.
`doc-forge` is an "AI-Native" documentation compiler. To get the most out of it, design your docstrings with both humans and LLMs in mind: 3. **Back-end (Renderers)**
### For Humans (Readability & Structure) * MkDocs → human documentation
- **`__init__.py` as Landing Pages**: Use the docstring of your package's `__init__.py` as the home page. Include overviews, installation instructions, and high-level examples here. * MCP JSON → AI-readable documentation
- **Single Source of Truth**: Keep all technical details in docstrings. This ensures your MkDocs/Sphinx sites stay in sync with the code.
- **Semantic Hierarchy**: Use standard Markdown headers to structure complex module documentation.
### For LLMs (AI-Native Knowledge) The atomic unit of documentation is the Python import path.
- **Model Context Protocol (MCP)**: `doc-forge` exports your docs as structured JSON. This allows AI agents to "understand" your API surface area without layout noise.
- **Canonical Paths**: Use dotted import paths as primary identifiers. AI tools use these to link code usage to documentation.
- **Type Annotations**: While not in docstrings, `doc-forge` (via Griffe) extracts signatures. Clean type hints dramatically improve an LLM's ability to generate correct code using your library.
## Available Commands
- **build**: Build documentation (MkDocs site or MCP resources). Example:
- **serve**: Serve documentation (MkDocs or MCP).
- **tree**: Visualize the introspected project structure. ```python
from my_package.foo import Bar
```
---
## Docstring Writing Standard
Docstrings are the single source of truth. `doc-forge` does not generate content.
It compiles and renders what you write.
Documentation follows the Python import hierarchy.
---
## Package docstring (`package/__init__.py`) — Full user guide
This is the landing page. A developer must be able to install and use the
package after reading only this docstring.
Example:
```python
'''
my_package
Short description of what this package provides.
## Installation ## Installation
Install using `pip` with the optional `mkdocs` dependencies for a complete setup: pip install my-package
```bash ## Quick start
pip install doc-forge
from my_package.foo import Bar
bar = Bar()
result = bar.process("example")
## Core concepts
Bar
Primary object exposed by the package.
foo module
Provides core functionality.
## Typical workflow
1. Import public objects
2. Initialize objects
3. Call methods
## Public API
foo.Bar
foo.helper_function
'''
``` ```
## Quick Start ---
## Submodule docstring (`package/foo/__init__.py`) — Subsystem guide
Explains a specific subsystem.
Example:
```python
'''
foo subsystem.
Provides core functionality.
## Usage
from my_package.foo import Bar
bar = Bar()
bar.process("example")
'''
```
---
## Class docstring — Object contract
Defines responsibility and behavior.
Example:
```python
class Bar:
'''
Performs processing on input data.
Instances may be reused across multiple calls.
'''
```
Include:
* Responsibility
* Lifecycle expectations
* Thread safety (if relevant)
* Performance characteristics (if relevant)
---
## Function and method docstrings — API specification
Example:
```python
def process(self, value: str) -> str:
'''
Process an input value.
Args:
value:
Input string.
Returns:
Processed string.
Raises:
ValueError:
If the input is invalid.
'''
```
---
## Attribute docstrings (optional)
Example:
```python
self.name: str
'''Identifier used during processing.'''
```
---
## Writing Rules
**Required**
* Use Markdown headings
* Provide real import examples
* Document all public APIs
* Keep descriptions precise and factual
**Avoid**
* Plain-text separators like `====`
* Duplicate external documentation
* Informal or conversational language
---
## How doc-forge uses these docstrings
Build MkDocs site:
1. **Build Documentation**:
Introspect your package and generate documentation in one step:
```bash ```bash
# Build MkDocs site doc-forge build --mkdocs --module my_package
doc-forge build --mkdocs --module my_package --site-name "My Docs" ```
# Build MCP resources Build MCP documentation:
```bash
doc-forge build --mcp --module my_package doc-forge build --mcp --module my_package
``` ```
2. **Define Navigation**: Both outputs are generated directly from docstrings.
Create a `docforge.nav.yml` to organize your documentation:
```yaml
home: my_package/index.md
groups:
Core API:
- my_package/core/*.md
Utilities:
- my_package/utils.md
```
3. **Preview**:
```bash
# Serve MkDocs site
doc-forge serve --mkdocs
# Serve MCP documentation
doc-forge serve --mcp
```
## Project Structure
- `docforge.loaders`: Introspects source code using static analysis (`griffe`).
- `docforge.models`: The internal representation of your project, modules, and objects.
- `docforge.renderers`: Converters that turn the models into physical files.
- `docforge.nav`: Managers for logical-to-physical path mapping and navigation.
""" """
from .loaders import GriffeLoader, discover_module_paths from .loaders import GriffeLoader, discover_module_paths