Skip to content

Commit 64d61b7

Browse files
Document docstring snippet conventions in CLAUDE.md
Add a "Docstring Code Examples" section covering the companion file system: directory layout, `<!-- snippet-source #RegionName -->` markers, `ClassName_methodName_variant` naming, the function-parameter pattern for typed dependencies, and the prohibition on type-suppression comments inside regions.
1 parent 6fc7aff commit 64d61b7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

CLAUDE.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,55 @@ rather than adding new standalone sections.
109109
- Update config rev
110110
- Commit config first
111111

112+
## Docstring Code Examples
113+
114+
Code examples in `src/mcp/` docstrings are type-checked via companion files in
115+
`examples/snippets/docstrings/mcp/`, mirroring the source tree
116+
(`src/mcp/foo/bar.py``examples/snippets/docstrings/mcp/foo/bar.py`).
117+
Companion files are standalone scripts (not packages) starting with
118+
`from __future__ import annotations`. The companion file is the source of truth —
119+
always edit examples there, never directly in the docstring.
120+
121+
Each example lives in a named function (returning `-> None`) wrapping
122+
`# region Name` / `# endregion Name` markers. Names follow
123+
`ClassName_methodName_variant` for methods, `functionName_variant` for standalone
124+
functions, or `module_overview` for module docstrings. Pick a descriptive variant
125+
suffix (`_basic`, `_sync`/`_async`, `_with_context`, etc.). In the companion file:
126+
127+
````python
128+
def MyClass_do_thing_basic(obj: MyClass) -> None:
129+
# region MyClass_do_thing_basic
130+
result = obj.do_thing("arg")
131+
print(result)
132+
# endregion MyClass_do_thing_basic
133+
````
134+
135+
The sync script wraps region content in a fenced code block between
136+
`<!-- snippet-source #RegionName -->` and `<!-- /snippet-source -->` markers.
137+
In the source docstring:
138+
139+
````
140+
Example:
141+
<!-- snippet-source #MyClass_do_thing_basic -->
142+
```python
143+
result = obj.do_thing("arg")
144+
print(result)
145+
```
146+
<!-- /snippet-source -->
147+
````
148+
149+
Function parameters supply typed dependencies the example needs but does not create
150+
(e.g., `server: MCPServer`); module-level stubs are only for truly undefined references
151+
(e.g., `async def fetch_data() -> str: ...`).
152+
153+
NEVER put `# type: ignore`, `# pyright: ignore`, or `# noqa` inside a region — these
154+
sync verbatim into the docstring. Restructure the code or move problematic lines outside
155+
the region instead.
156+
157+
After editing a companion file, run `uv run --frozen pyright` to verify types, then
158+
`uv run python scripts/sync_snippets.py` to sync into docstrings. Use `--check` to
159+
verify sync without modifying files.
160+
112161
## Error Resolution
113162

114163
1. CI Failures

0 commit comments

Comments
 (0)