Skip to content

Commit

Permalink
Explain how to use sub clients in API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Feb 14, 2025
1 parent c845441 commit c67a86f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
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:
32 changes: 31 additions & 1 deletion docs/sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,40 @@

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(" # 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

0 comments on commit c67a86f

Please sign in to comment.