Skip to content

Commit 4551297

Browse files
Restructure snippet system docs into shared intro with subsections
The previous documentation had two flat sections where the docstring section contained all the detail and the markdown section referred to it vaguely. Reorganize into a `## Code Snippet System` intro covering shared concepts (marker format, region extraction, naming conventions, function wrappers, typed params, `# type: ignore` prohibition, pyright workflow) with `### Markdown Code Examples` and `### Docstring Code Examples` subsections covering only what is unique to each target type.
1 parent 1411fe0 commit 4551297

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

CLAUDE.md

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,19 @@ rather than adding new standalone sections.
109109
- Update config rev
110110
- Commit config first
111111

112-
## Docstring Code Examples
112+
## Code Snippet System
113113

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.
114+
`scripts/sync_snippets.py` replaces the content between
115+
`<!-- snippet-source ... -->` / `<!-- /snippet-source -->` markers with code
116+
from the referenced source file. The source file is the source of truth —
117+
never edit synced content directly in the target.
120118

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:
119+
To sync only part of a file, append `#RegionName` to the path. Regions are
120+
delimited in source files by `# region Name` / `# endregion Name` markers.
121+
Each example lives in a named function (returning `-> None`) wrapping a region.
122+
Names follow `ClassName_methodName_variant` for methods, `functionName_variant`
123+
for standalone functions, or `module_overview` for module docstrings. Pick a
124+
descriptive variant suffix (`_basic`, `_sync`/`_async`, `_with_context`, etc.):
126125

127126
````python
128127
def MyClass_do_thing_basic(obj: MyClass) -> None:
@@ -132,46 +131,50 @@ def MyClass_do_thing_basic(obj: MyClass) -> None:
132131
# endregion MyClass_do_thing_basic
133132
````
134133

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-
149134
Function parameters supply typed dependencies the example needs but does not create
150135
(e.g., `server: MCPServer`); module-level stubs are only for truly undefined references
151136
(e.g., `async def fetch_data() -> str: ...`).
152137

153138
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.
139+
sync verbatim into the target. Restructure the code to address the errors instead.
156140

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.
141+
After editing an example file, run `uv run --frozen pyright` to verify types, then
142+
`uv run python scripts/sync_snippets.py` to sync. Use `--check` to verify without
143+
modifying files.
160144

161-
## Markdown Code Examples
145+
### Markdown Code Examples
162146

163-
The `sync_snippets.py` script also syncs snippets to `docs/**/*.md` and `README.v2.md`.
164-
These files use explicit paths with optional `#Region` markers for `snippet-source`
165-
(path-less `#Region` markers are only supported in `src/` files):
147+
Code examples in `README.v2.md` and `docs/**/*.md` use explicit paths relative
148+
to the repo root:
166149

167150
````markdown
168-
<!-- snippet-source examples/snippets/servers/foo.py -->
151+
<!-- snippet-source examples/snippets/servers/basic_tool.py -->
169152
```python
170-
# contents of examples/snippets/servers/foo.py
153+
# replaced by sync script
171154
```
172155
<!-- /snippet-source -->
173156
````
174157

158+
### Docstring Code Examples
159+
160+
Code examples in `src/` docstrings use companion files in
161+
`examples/snippets/docstrings/`, mirroring the source tree
162+
(`src/mcp/foo/bar.py``examples/snippets/docstrings/mcp/foo/bar.py`).
163+
Companion files are standalone scripts (not packages) starting with
164+
`from __future__ import annotations`.
165+
166+
Docstrings use path-less `#Region` markers (only supported in `src/` files):
167+
168+
````
169+
Example:
170+
<!-- snippet-source #MyClass_do_thing_basic -->
171+
```python
172+
result = obj.do_thing("arg")
173+
print(result)
174+
```
175+
<!-- /snippet-source -->
176+
````
177+
175178
## Error Resolution
176179

177180
1. CI Failures

0 commit comments

Comments
 (0)