Skip to content

[Backport 8.17] Explain how to use sub clients in API docs #2802

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
merged 1 commit into from
Feb 18, 2025
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
2 changes: 1 addition & 1 deletion docs/sphinx/api/elasticsearch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Elasticsearch
-------------

.. py:module:: elasticsearch.client
.. py:module:: elasticsearch

.. autoclass:: Elasticsearch
:members:
34 changes: 33 additions & 1 deletion docs/sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,42 @@

extensions = ["sphinx.ext.autodoc", "sphinx.ext.doctest", "sphinx.ext.intersphinx"]

autoclass_content = "both"
autoclass_content = "class"
autodoc_class_signature = "separated"

autodoc_typehints = "description"


def client_name(full_name):
# Get the class name, e.g. ['elasticsearch', 'client', 'TextStructureClient'] -> 'TextStructure'
class_name = full_name.split(".")[-1].removesuffix("Client")
# Convert to snake case, e.g. 'TextStructure' -> '_text_structure'
snake_case = "".join(["_" + c.lower() if c.isupper() else c for c in class_name])
# Remove the leading underscore
return snake_case.lstrip("_")


def add_client_usage_example(app, what, name, obj, options, lines):
if what == "class" and "Client" in name:
sub_client_name = client_name(name)
lines.append(
f"To use this client, access ``client.{sub_client_name}`` from an "
" :class:`~elasticsearch.Elasticsearch` client. For example::"
)
lines.append("")
lines.append(" from elasticsearch import Elasticsearch")
lines.append("")
lines.append(" # Create the client instance")
lines.append(" client = Elasticsearch(...)")
lines.append(f" # Use the {sub_client_name} client")
lines.append(f" client.{sub_client_name}.<method>(...)")
lines.append("")


def setup(app):
app.connect("autodoc-process-docstring", add_client_usage_example)


# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

Expand Down
2 changes: 2 additions & 0 deletions elasticsearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
from ._utils import fixup_module_metadata

# This file exists for backwards compatibility.
# We can't remove it as we use it for the Sphinx docs which show the full page, and we'd
# rather show `elasticsearch.client.FooClient` than `elasticsearch._sync.client.FooClient`.
warnings.warn(
"Importing from the 'elasticsearch.client' module is deprecated. "
"Instead use 'elasticsearch' module for importing the client.",
Expand Down