Skip to content

Commit fd6fa41

Browse files
authored
Add 'Type Hints' section to documentation
1 parent 6f4cfae commit fd6fa41

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

docs/sphinx/index.rst

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ raise the limit:
198198
``fork``. Note that Python's ``multiprocessing`` module uses ``fork`` to
199199
create new processes on POSIX systems.
200200

201-
SSL and Authentication
202-
~~~~~~~~~~~~~~~~~~~~~~
201+
TLS/SSL and Authentication
202+
~~~~~~~~~~~~~~~~~~~~~~~~~~
203203

204204
You can configure the client to use ``SSL`` for connecting to your
205205
elasticsearch cluster, including certificate verification and HTTP auth:
@@ -304,7 +304,54 @@ an issue) it also just uses ``localhost:9200`` as the address instead of the
304304
actual address of the host. If the trace logger has not been configured
305305
already it is set to `propagate=False` so it needs to be activated separately.
306306

307-
.. _logging library: http://docs.python.org/3.3/library/logging.html
307+
.. _logging library: http://docs.python.org/3/library/logging.html
308+
309+
Type Hints
310+
~~~~~~~~~~
311+
312+
Starting in ``elasticsearch-py`` v7.10.0 the library now ships with `type hints`_
313+
and supports basic static type analysis with tools like `Mypy`_ and `Pyright`_.
314+
315+
If we write a script that has a type error like using ``request_timeout`` with
316+
a ``str`` argument instead of ``float`` and then run Mypy on the script:
317+
318+
.. code-block:: python
319+
320+
# script.py
321+
from elasticsearch import Elasticsearch
322+
323+
es = Elasticsearch(...)
324+
es.search(
325+
index="test-index",
326+
request_timeout="5" # type error!
327+
)
328+
329+
# $ mypy script.py
330+
# script.py:5: error: Argument "request_timeout" to "search" of "Elasticsearch" has
331+
# incompatible type "str"; expected "Union[int, float, None]"
332+
# Found 1 error in 1 file (checked 1 source file)
333+
334+
For now many parameter types for API methods aren't specific to
335+
a type (ie they are of type ``typing.Any``) but in the future
336+
they will be tightened for even better static type checking.
337+
338+
Type hints also allow tools like your IDE to check types and provide better
339+
auto-complete functionality.
340+
341+
.. warning::
342+
343+
The type hints for API methods like ``search`` don't match the function signature
344+
that can be found in the source code. Type hints represent optimal usage of the
345+
API methods. Using keyword arguments is highly recommended so all optional parameters
346+
and ``body`` are keyword-only in type hints.
347+
348+
JetBrains PyCharm will use the warning ``Unexpected argument`` to denote that the
349+
parameter may be keyword-only.
350+
351+
.. _type hints: https://www.python.org/dev/peps/pep-0484
352+
.. _mypy: http://mypy-lang.org
353+
.. _pyright: https://github.com/microsoft/pyright
354+
308355

309356
Environment considerations
310357
--------------------------
@@ -317,18 +364,9 @@ functionality - the cluster would supply the client with IP addresses to
317364
directly connect to the cluster, circumventing the load balancer. Depending on
318365
your configuration this might be something you don't want or break completely.
319366

320-
In some environments (notably on Google App Engine) your HTTP requests might be
321-
restricted so that ``GET`` requests won't accept body. In that case use the
322-
``send_get_body_as`` parameter of :class:`~elasticsearch.Transport` to send all
323-
bodies via post:
324-
325-
.. code-block:: python
326-
327-
from elasticsearch import Elasticsearch
328-
es = Elasticsearch(send_get_body_as='POST')
329-
330367
Compression
331368
~~~~~~~~~~~
369+
332370
When using capacity-constrained networks (low throughput), it may be handy to enable
333371
compression. This is especially useful when doing bulk loads or inserting large
334372
documents. This will configure compression.
@@ -341,7 +379,7 @@ documents. This will configure compression.
341379
Compression is enabled by default when connecting to Elastic Cloud via ``cloud_id``.
342380

343381
Running on AWS with IAM
344-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
382+
~~~~~~~~~~~~~~~~~~~~~~~
345383

346384
If you want to use this client with IAM based authentication on AWS you can use
347385
the `requests-aws4auth`_ package:
@@ -452,4 +490,3 @@ Indices and tables
452490
* :ref:`genindex`
453491
* :ref:`modindex`
454492
* :ref:`search`
455-

0 commit comments

Comments
 (0)