Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157"""Validate Python code examples in FastMCP documentation.
Extracts code blocks from .mdx docs and checks:1. Syntax — every example parses as valid Python2. FastMCP imports — every ``from fastmcp.x import y`` resolves
Supports tags in code fence prefix (for future use): ```python test="skip" — skip all checks ```python lint="skip" — skip all checks
Run: uv run pytest tests/docs/test_doc_examples.py -v -s"""
from __future__ import annotations
import astfrom pathlib import Pathfrom uuid import uuid4
import pytestfrom pytest_examples import CodeExamplefrom pytest_examples.find_examples import _extract_code_chunks
DOCS_DIR = Path("docs")_SKIP_DIRS = {"python-sdk", "public"}
# Snapshot baselines — ratchet DOWN as doc examples are fixed.MAX_SYNTAX_FAILURES = 0MAX_IMPORT_FAILURES = 0
def _find_mdx_examples() -> list[CodeExample]: """Find Python code examples in .mdx files.
pytest-examples only supports ``.md``; we call its internal ``_extract_code_chunks`` directly so ``.mdx`` works without copying. """ examples: list[CodeExample] = [] for mdx_file in sorted(DOCS_DIR.rglob("*.mdx")): rel = mdx_file.relative_to(DOCS_DIR) if rel.parts and rel.parts[0] in _SKIP_DIRS: continue code = mdx_file.read_text("utf-8") group = uuid4() examples.extend(_extract_code_chunks(mdx_file, code, group)) return examples
def _should_skip(example: CodeExample) -> bool: settings = example.prefix_settings() return settings.get("lint") == "skip" or settings.get("test") == "skip"
def _check_syntax(example: CodeExample) -> str | None: """Return error description if syntax is invalid, else None.""" try: ast.parse(example.source) return None except SyntaxError as e: rel = Path(example.path).relative_to(DOCS_DIR) return f"{rel}:{example.start_line}: line {e.lineno}: {e.msg}"
def _check_fastmcp_imports(example: CodeExample) -> list[str]: """Return list of broken fastmcp import descriptions.""" try: tree = ast.parse(example.source) except SyntaxError: return []
errors: list[str] = [] rel = Path(example.path).relative_to(DOCS_DIR)
for node in ast.walk(tree): if isinstance(node, ast.Import): for alias in node.names: if alias.name.startswith("fastmcp"): try: __import__(alias.name) except ImportError: errors.append( f"{rel}:{example.start_line}: cannot import '{alias.name}'" ) elif isinstance(node, ast.ImportFrom): if node.module and node.module.startswith("fastmcp"): names = [a.name for a in node.names] try: mod = __import__(node.module, fromlist=names) except ImportError: errors.append( f"{rel}:{example.start_line}: " f"cannot import module '{node.module}'" ) continue for name in names: if not hasattr(mod, name): errors.append( f"{rel}:{example.start_line}: " f"'{node.module}' has no '{name}'" ) return errors
# This scans the entire docs corpus and imports optional integrations on cold CI.@pytest.mark.timeout(30)def test_doc_examples_quality(): """Doc examples should not regress in syntax or import correctness.
Checks every Python code block in ``docs/*.mdx`` (excluding auto-generated ``python-sdk/`` and ``public/`` directories). Reports all failures and asserts counts don't exceed known baselines. """ examples = _find_mdx_examples() syntax_failures: list[str] = [] import_failures: list[str] = []
for ex in examples: if _should_skip(ex): continue
err = _check_syntax(ex) if err: syntax_failures.append(err) continue
# Frozen version snapshots (docs/v2/, docs/v3/) document older FastMCP # releases, so their imports are validated against a package that no # longer ships them. Syntax-check them above, but skip live-import # validation. if Path(ex.path).relative_to(DOCS_DIR).parts[0] in ("v2", "v3"): continue
import_failures.extend(_check_fastmcp_imports(ex))
total = len(examples) print(f"\nDoc examples checked: {total}") print(f"Syntax failures: {len(syntax_failures)}") print(f"Import failures: {len(import_failures)}")
if syntax_failures: print("\nSyntax errors:") for f in syntax_failures: print(f" {f}")
if import_failures: print("\nBroken imports:") for f in import_failures: print(f" {f}")
assert len(syntax_failures) <= MAX_SYNTAX_FAILURES, ( f"Syntax failures regressed: {len(syntax_failures)} > {MAX_SYNTAX_FAILURES}" ) assert len(import_failures) <= MAX_IMPORT_FAILURES, ( f"Import failures regressed: {len(import_failures)} > {MAX_IMPORT_FAILURES}" )