-
Notifications
You must be signed in to change notification settings - Fork 3
Add function reference to AGENTS.md with auto-generated docs/functions.txt #1624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nathanjmcdougall
merged 7 commits into
main
from
copilot/configure-agents-dependency-functions
Mar 29, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b0d312
Initial plan
Copilot 5f0a246
Add function reference to AGENTS.md and docs/functions.txt
Copilot 22c36f5
Auto-generate docs/functions.txt via export-functions hook from sourc…
Copilot 1f21438
Simplify export-functions hook: accept source files as args, flat output
Copilot c03e58e
Add docstrings to 112 public module-level functions
Copilot b28e6da
Fix docstring ordering and AGENTS.md sync formatting
Copilot 0bc5d6f
Use --source-root for file discovery, add --strict, add missing docst…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
nathanjmcdougall marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| """Export public module-level functions with docstrings from a Python package. | ||
|
|
||
| Recursively scans all Python source files under a package root directory for | ||
| public module-level functions with docstrings and writes a flat markdown bullet | ||
| list to an output file. Functions are listed in the order they appear in each | ||
| file, with files processed in sorted order. Class methods and nested functions | ||
| are not included. Functions without a docstring are skipped unless --strict is | ||
| used, in which case the script exits non-zero when any are found. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import ast | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _module_name(source_file: Path, source_root: Path) -> str: | ||
| """Derive a dotted module name from a file path, including the package name.""" | ||
| # Use source_root.parent so the package directory name itself is part of the path. | ||
| rel = source_file.relative_to(source_root.parent) | ||
| parts = rel.with_suffix("").parts | ||
| # Drop __init__ from the tail so the module name matches the package path. | ||
| if parts and parts[-1] == "__init__": | ||
| parts = parts[:-1] | ||
| return ".".join(parts) | ||
|
|
||
|
|
||
| def _get_module_public_functions(path: Path) -> list[tuple[str, str | None]]: | ||
| """Return (name, docstring_first_line_or_None) for each top-level public function. | ||
|
|
||
| Only direct children of the module node are included (no class methods or | ||
| nested functions). Functions are returned in source order. | ||
| """ | ||
| try: | ||
| source = path.read_text(encoding="utf-8") | ||
| except (OSError, UnicodeDecodeError) as exc: | ||
| print(f"ERROR: Cannot read {path}: {exc}", file=sys.stderr) | ||
| return [] | ||
|
|
||
| try: | ||
| tree = ast.parse(source) | ||
| except SyntaxError as exc: | ||
| print(f"ERROR: Cannot parse {path}: {exc}", file=sys.stderr) | ||
| return [] | ||
|
|
||
| results: list[tuple[str, str | None]] = [] | ||
| for node in ast.iter_child_nodes(tree): | ||
| if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): | ||
| continue | ||
| if node.name.startswith("_"): | ||
| continue | ||
| docstring = ast.get_docstring(node) | ||
| if docstring is not None: | ||
| first_line = docstring.split("\n")[0].strip() | ||
| # Normalize RST-style double backticks to markdown single backticks | ||
| # so the output is compatible with prettier's markdown formatting. | ||
| first_line = first_line.replace("``", "`") | ||
| results.append((node.name, first_line if first_line else None)) | ||
| else: | ||
| results.append((node.name, None)) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def _collect_py_files(source_root: Path) -> list[Path]: | ||
| """Return all .py files under source_root in sorted order.""" | ||
| return sorted(source_root.rglob("*.py")) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser( | ||
| description="Export public function reference from a Python package.", | ||
| ) | ||
| parser.add_argument( | ||
| "--output-file", | ||
| required=True, | ||
| help="Path to the output file to write.", | ||
| ) | ||
| parser.add_argument( | ||
| "--source-root", | ||
| required=True, | ||
| help="Root package directory to scan for public functions.", | ||
| ) | ||
| parser.add_argument( | ||
| "--strict", | ||
| action="store_true", | ||
| default=False, | ||
| help="Fail if any public module-level function lacks a docstring.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| output_file = Path(args.output_file) | ||
| source_root = Path(args.source_root) | ||
|
|
||
| if not source_root.is_dir(): | ||
| print(f"ERROR: Source root {source_root} is not a directory.", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| bullets: list[str] = [] | ||
| missing: list[tuple[Path, str]] = [] | ||
|
|
||
| for py_file in _collect_py_files(source_root): | ||
| try: | ||
| module = _module_name(py_file, source_root) | ||
| except ValueError: | ||
| print( | ||
| f"ERROR: {py_file} is not relative to source root {source_root}.", | ||
| file=sys.stderr, | ||
| ) | ||
| return 1 | ||
|
|
||
| for func_name, first_line in _get_module_public_functions(py_file): | ||
| if first_line is not None: | ||
| bullets.append(f"- `{func_name}()` (`{module}`) — {first_line}") | ||
| else: | ||
| missing.append((py_file, func_name)) | ||
|
|
||
| content = "\n".join(bullets) + "\n" | ||
|
|
||
| output_file.parent.mkdir(parents=True, exist_ok=True) | ||
| output_file.write_text(content, encoding="utf-8") | ||
|
|
||
| print(f"Function reference written to {output_file}.") | ||
|
|
||
| if args.strict and missing: | ||
| print( | ||
| f"ERROR: {len(missing)} public function(s) missing a docstring:", | ||
| file=sys.stderr, | ||
| ) | ||
| for path, name in missing: | ||
| print(f" - {path}:{name}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.