Skip to content
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

docs: Add support for class methods in API reference #7841

Merged
Merged
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
1 change: 0 additions & 1 deletion docs/docs/api/adapters/ChatAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- format
- format_fields
- format_finetune_data
- format_turn
- parse
show_source: true
show_undocumented_members: true
Expand Down
11 changes: 4 additions & 7 deletions docs/docs/api/primitives/Image.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
handler: python
options:
members:
- copy
- dict
- json
- model_copy
- model_dump
- model_dump_json
- model_post_init
- from_PIL
- from_file
- from_url
- serialize_model
- validate_input
show_source: true
show_undocumented_members: true
show_root_heading: true
Expand Down
1 change: 1 addition & 0 deletions docs/docs/api/primitives/Prediction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
options:
members:
- copy
- from_completions
- get
- inputs
- items
Expand Down
15 changes: 8 additions & 7 deletions docs/docs/api/signatures/Signature.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
handler: python
options:
members:
- copy
- dict
- json
- model_copy
- model_dump
- model_dump_json
- model_post_init
- append
- dump_state
- equals
- insert
- load_state
- prepend
- with_instructions
- with_updated_fields
show_source: true
show_undocumented_members: true
show_root_heading: true
Expand Down
19 changes: 16 additions & 3 deletions docs/scripts/generate_api_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@
}


def should_document_method(obj):
name = obj.__name__
# Exclude methods not defined in dspy, such as `model_dump_json` from pydantic.
module = getattr(obj, "__module__", "")
if not module or not module.startswith(f"dspy"):
return False
# Exclude private and dunder methods, but include `__call__`
if name == "__call__" or not name.startswith("_"):
return True
return False


def get_module_contents(module):
"""Get all public classes and functions from a module."""
contents_in_all = getattr(module, "__all__", None)
Expand All @@ -88,7 +100,7 @@ def get_module_contents(module):
if inspect.ismodule(obj) and obj.__name__.startswith(module.__name__) and not name.startswith("_"):
contents[name] = obj
elif (
(inspect.isclass(obj) or inspect.isfunction(obj))
(inspect.isclass(obj) or (inspect.isroutine(obj) and should_document_method(obj)))
and obj.__module__.startswith(module.__name__)
and not name.startswith("_")
):
Expand All @@ -100,8 +112,9 @@ def get_public_methods(cls):
"""Returns a list of all public methods in a class."""
return [
name
for name, member in inspect.getmembers(cls, predicate=inspect.isfunction)
if name == "__call__" or not name.startswith("_") # Exclude private and dunder methods, but include `__call__`
for name, member in inspect.getmembers(
cls, predicate=lambda x: inspect.isroutine(x) and should_document_method(x)
)
]


Expand Down
Loading