Skip to content

Commit 7498726

Browse files
pquentingithub-actions[bot]
authored andcommitted
Explain how to use sub clients in API docs (#2798)
(cherry picked from commit db22037)
1 parent 894a4be commit 7498726

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Diff for: docs/sphinx/api/elasticsearch.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Elasticsearch
44
-------------
55

6-
.. py:module:: elasticsearch.client
6+
.. py:module:: elasticsearch
77
88
.. autoclass:: Elasticsearch
99
:members:

Diff for: docs/sphinx/conf.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,42 @@
2121

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

24-
autoclass_content = "both"
24+
autoclass_content = "class"
25+
autodoc_class_signature = "separated"
2526

2627
autodoc_typehints = "description"
2728

29+
30+
def client_name(full_name):
31+
# Get the class name, e.g. ['elasticsearch', 'client', 'TextStructureClient'] -> 'TextStructure'
32+
class_name = full_name.split(".")[-1].removesuffix("Client")
33+
# Convert to snake case, e.g. 'TextStructure' -> '_text_structure'
34+
snake_case = "".join(["_" + c.lower() if c.isupper() else c for c in class_name])
35+
# Remove the leading underscore
36+
return snake_case.lstrip("_")
37+
38+
39+
def add_client_usage_example(app, what, name, obj, options, lines):
40+
if what == "class" and "Client" in name:
41+
sub_client_name = client_name(name)
42+
lines.append(
43+
f"To use this client, access ``client.{sub_client_name}`` from an "
44+
" :class:`~elasticsearch.Elasticsearch` client. For example::"
45+
)
46+
lines.append("")
47+
lines.append(" from elasticsearch import Elasticsearch")
48+
lines.append("")
49+
lines.append(" # Create the client instance")
50+
lines.append(" client = Elasticsearch(...)")
51+
lines.append(f" # Use the {sub_client_name} client")
52+
lines.append(f" client.{sub_client_name}.<method>(...)")
53+
lines.append("")
54+
55+
56+
def setup(app):
57+
app.connect("autodoc-process-docstring", add_client_usage_example)
58+
59+
2860
# Add any paths that contain templates here, relative to this directory.
2961
templates_path = ["_templates"]
3062

Diff for: elasticsearch/client.py

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
from ._utils import fixup_module_metadata
7474

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

0 commit comments

Comments
 (0)