Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README-PYPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def main():
api_key=os.getenv("OPENROUTER_API_KEY", ""),
) as open_router:
# Rest of application here...
pass


# Or when using async:
Expand All @@ -318,6 +319,7 @@ async def amain():
api_key=os.getenv("OPENROUTER_API_KEY", ""),
) as open_router:
# Rest of application here...
pass
```
<!-- End Resource Management [resource-management] -->

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ def main():
api_key=os.getenv("OPENROUTER_API_KEY", ""),
) as open_router:
# Rest of application here...
pass


# Or when using async:
Expand All @@ -318,6 +319,7 @@ async def amain():
api_key=os.getenv("OPENROUTER_API_KEY", ""),
) as open_router:
# Rest of application here...
pass
```
<!-- End Resource Management [resource-management] -->

Expand Down
44 changes: 44 additions & 0 deletions tests/test_docs_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import ast
import re
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[1]
SNIPPET = re.compile(r"```python\n(.*?)```", re.S)


def _snippets():
"""Every documented snippet that actually uses the SDK.

Blocks under docs/components/ and docs/operations/ are type signatures rather
than programs, so only blocks that import from `openrouter` are collected.
"""
files = [ROOT / "README.md", ROOT / "README-PYPI.md", ROOT / "USAGE.md"]
files += sorted(ROOT.glob("docs/**/*.mdx"))

found = []
for path in files:
if not path.exists():
continue
text = path.read_text(encoding="utf-8")
for match in SNIPPET.finditer(text):
code = match.group(1)
if "from openrouter" in code or "import openrouter" in code:
line = text[: match.start()].count("\n") + 1
found.append(
pytest.param(code, id=f"{path.relative_to(ROOT).as_posix()}:{line}")
)
return found


@pytest.mark.parametrize("code", _snippets())
def test_documented_snippet_is_valid_python(code):
# The README's Resource Management block is generated between Speakeasy
# section markers, so a regeneration can silently reintroduce the empty
# `with` bodies this guards against.
ast.parse(code)


def test_snippet_collection_is_not_empty():
assert len(_snippets()) > 100