Skip to content

Add type hints to APIs #1297

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 4 commits into from
Sep 28, 2020
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: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include README.rst
include README
include tox.ini
include setup.py
recursive-include elasticsearch* py.typed
recursive-include docs *

prune docs/_build
Expand Down
6 changes: 6 additions & 0 deletions docs/sphinx/async.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ AsyncTransport
.. autoclass:: AsyncTransport
:members:

AsyncConnection
~~~~~~~~~~~~~~~~~

.. autoclass:: AsyncConnection
:members:

AIOHttpConnection
~~~~~~~~~~~~~~~~~

Expand Down
9 changes: 7 additions & 2 deletions elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@
if sys.version_info < (3, 6):
raise ImportError

from ._async.http_aiohttp import AIOHttpConnection
from ._async.http_aiohttp import AIOHttpConnection, AsyncConnection
from ._async.transport import AsyncTransport
from ._async.client import AsyncElasticsearch

__all__ += ["AIOHttpConnection", "AsyncTransport", "AsyncElasticsearch"]
__all__ += [
"AIOHttpConnection",
"AsyncConnection",
"AsyncTransport",
"AsyncElasticsearch",
]
except (ImportError, SyntaxError):
pass
62 changes: 62 additions & 0 deletions elasticsearch/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import sys
from typing import Tuple

from .client import Elasticsearch as Elasticsearch
from .transport import Transport as Transport
from .connection_pool import (
ConnectionPool as ConnectionPool,
ConnectionSelector as ConnectionSelector,
RoundRobinSelector as RoundRobinSelector,
)
from .serializer import JSONSerializer as JSONSerializer
from .connection import (
Connection as Connection,
RequestsHttpConnection as RequestsHttpConnection,
Urllib3HttpConnection as Urllib3HttpConnection,
)
from .exceptions import (
ImproperlyConfigured as ImproperlyConfigured,
ElasticsearchException as ElasticsearchException,
SerializationError as SerializationError,
TransportError as TransportError,
NotFoundError as NotFoundError,
ConflictError as ConflictError,
RequestError as RequestError,
ConnectionError as ConnectionError,
SSLError as SSLError,
ConnectionTimeout as ConnectionTimeout,
AuthenticationException as AuthenticationException,
AuthorizationException as AuthorizationException,
ElasticsearchDeprecationWarning as ElasticsearchDeprecationWarning,
)

try:
if sys.version_info < (3, 6):
raise ImportError

from ._async.http_aiohttp import AIOHttpConnection as AIOHttpConnection
from ._async.transport import AsyncTransport as AsyncTransport
from ._async.client import AsyncElasticsearch as AsyncElasticsearch
except (ImportError, SyntaxError):
pass

VERSION: Tuple[int, int, int]
__version__: Tuple[int, int, int]
__versionstr__: str
36 changes: 36 additions & 0 deletions elasticsearch/_async/_extra_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# type: ignore

# This file exists for the sole reason of making mypy not
# complain about type issues to do with 'aiohttp' and 'yarl'.
# We're in a catch-22 situation:
# - If we use 'type: ignore' on 'import aiohttp' and it's not installed
# mypy will complain that the annotation is unnecessary.
# - If we don't use 'type: ignore' on 'import aiohttp' and it
# it's not installed mypy will complain that it can't find
# type hints for aiohttp.
# So to make mypy happy we move all our 'extra' imports here
# and add a global 'type: ignore' which mypy never complains
# about being unnecessary.

import aiohttp
import aiohttp.client_exceptions as aiohttp_exceptions
import yarl

__all__ = ["aiohttp", "aiohttp_exceptions", "yarl"]
37 changes: 37 additions & 0 deletions elasticsearch/_async/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1972,3 +1972,40 @@ async def update_by_query(self, index, body=None, params=None, headers=None):
headers=headers,
body=body,
)

@query_params()
async def close_point_in_time(self, body=None, params=None, headers=None):
"""
Close a point in time
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/point-in-time-api.html>`_

:arg body: a point-in-time id to close
"""
return await self.transport.perform_request(
"DELETE", "/_pit", params=params, headers=headers, body=body
)

@query_params(
"expand_wildcards", "ignore_unavailable", "keep_alive", "preference", "routing"
)
async def open_point_in_time(self, index=None, params=None, headers=None):
"""
Open a point in time that can be used in subsequent searches
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/point-in-time-api.html>`_

:arg index: A comma-separated list of index names to open point
in time; use `_all` or empty string to perform the operation on all
indices
:arg expand_wildcards: Whether to expand wildcard expression to
concrete indices that are open, closed or both. Valid choices: open,
closed, hidden, none, all Default: open
:arg ignore_unavailable: Whether specified concrete indices
should be ignored when unavailable (missing or closed)
:arg keep_alive: Specific the time to live for the point in time
:arg preference: Specify the node or shard the operation should
be performed on (default: random)
:arg routing: Specific routing value
"""
return await self.transport.perform_request(
"POST", _make_path(index, "_pit"), params=params, headers=headers
)
Loading