Skip to content

Commit

Permalink
Expanded type coverage to benchmarks, samples and tests. (#566)
Browse files Browse the repository at this point in the history
* Renamed json samples to fix duplicate module name.

Signed-off-by: dblock <[email protected]>

* Enabled mypy on all source files.

Signed-off-by: dblock <[email protected]>

* Added missing types.

Signed-off-by: dblock <[email protected]>

* Added CHANGELOG.

Signed-off-by: dblock <[email protected]>

* Move type: ignore to fix untyped decorator makes function untyped.

Signed-off-by: dblock <[email protected]>

* Fix nox -rs lint-3.7.

Signed-off-by: dblock <[email protected]>

* Fixed incorrect import.

Signed-off-by: dblock <[email protected]>

* Fix broken test.

Signed-off-by: dblock <[email protected]>

* Fixed TestBulk::test_bulk_works_with_bytestring_body.

Signed-off-by: dblock <[email protected]>

---------

Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock authored Nov 9, 2023
1 parent dcb79cc commit 56c96d7
Show file tree
Hide file tree
Showing 101 changed files with 1,234 additions and 1,019 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Generate `cat` client from API specs ([#529](https://github.com/opensearch-project/opensearch-py/pull/529))
- Use API generator for all APIs ([#551](https://github.com/opensearch-project/opensearch-py/pull/551))
- Merge `.pyi` type stubs inline ([#563](https://github.com/opensearch-project/opensearch-py/pull/563))
- Expanded type coverage to benchmarks, samples and tests ([#566](https://github.com/opensearch-project/opensearch-py/pull/566))
### Deprecated
- Deprecated point-in-time APIs (list_all_point_in_time, create_point_in_time, delete_point_in_time) and Security Client APIs (health_check and update_audit_config) ([#502](https://github.com/opensearch-project/opensearch-py/pull/502))
### Removed
Expand Down
5 changes: 3 additions & 2 deletions benchmarks/bench_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import asyncio
import uuid
from typing import Any

from opensearchpy import AsyncHttpConnection, AsyncOpenSearch

Expand All @@ -22,7 +23,7 @@
item_count = 100


async def index_records(client, item_count) -> None:
async def index_records(client: Any, item_count: int) -> None:
await asyncio.gather(
*[
client.index(
Expand All @@ -39,7 +40,7 @@ async def index_records(client, item_count) -> None:
)


async def test_async(client_count=1, item_count=1):
async def test_async(client_count: int = 1, item_count: int = 1) -> None:
clients = []
for i in range(client_count):
clients.append(
Expand Down
7 changes: 4 additions & 3 deletions benchmarks/bench_info_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import logging
import sys
import time
from typing import Any

from thread_with_return_value import ThreadWithReturnValue

Expand All @@ -36,8 +37,8 @@
root.addHandler(handler)


def get_info(client, request_count):
tt = 0
def get_info(client: Any, request_count: int) -> float:
tt: float = 0
for n in range(request_count):
start = time.time() * 1000
client.info()
Expand All @@ -46,7 +47,7 @@ def get_info(client, request_count):
return tt


def test(thread_count=1, request_count=1, client_count=1):
def test(thread_count: int = 1, request_count: int = 1, client_count: int = 1) -> None:
clients = []
for i in range(client_count):
clients.append(
Expand Down
7 changes: 4 additions & 3 deletions benchmarks/bench_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
import time
import uuid
from typing import Any

from thread_with_return_value import ThreadWithReturnValue

Expand All @@ -37,10 +38,10 @@
root.addHandler(handler)


def index_records(client, item_count):
def index_records(client: Any, item_count: int) -> Any:
tt = 0
for n in range(10):
data = []
data: Any = []
for i in range(item_count):
data.append(
json.dumps({"index": {"_index": index_name, "_id": str(uuid.uuid4())}})
Expand All @@ -63,7 +64,7 @@ def index_records(client, item_count):
return tt


def test(thread_count=1, item_count=1, client_count=1):
def test(thread_count: int = 1, item_count: int = 1, client_count: int = 1) -> None:
clients = []
for i in range(client_count):
clients.append(
Expand Down
19 changes: 15 additions & 4 deletions benchmarks/thread_with_return_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@


from threading import Thread
from typing import Any, Optional


class ThreadWithReturnValue(Thread):
_target: Any
_args: Any
_kwargs: Any

def __init__(
self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None
):
self,
group: Any = None,
target: Any = None,
name: Optional[str] = None,
args: Any = (),
kwargs: Any = {},
Verbose: Optional[bool] = None,
) -> None:
Thread.__init__(self, group, target, name, args, kwargs)
self._return = None

def run(self):
def run(self) -> None:
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)

def join(self, *args):
def join(self, *args: Any) -> Any:
Thread.join(self, *args)
return self._return
34 changes: 18 additions & 16 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@

# -- Project information -----------------------------------------------------

project = "OpenSearch Python Client"
copyright = "OpenSearch Project Contributors"
author = "OpenSearch Project Contributors"
from typing import Any

project: str = "OpenSearch Python Client"
copyright: str = "OpenSearch Project Contributors"
author: str = "OpenSearch Project Contributors"


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
extensions: Any = [
"sphinx.ext.autodoc",
"sphinx_rtd_theme",
"sphinx.ext.viewcode",
Expand All @@ -47,44 +49,44 @@
]

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

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
exclude_patterns: Any = []


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"
html_theme: str = "sphinx_rtd_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_static_path: Any = ["_static"]

# -- additional settings -------------------------------------------------
intersphinx_mapping = {
intersphinx_mapping: Any = {
"python": ("https://docs.python.org/3", None),
}

html_logo = "imgs/OpenSearch.svg"
html_logo: str = "imgs/OpenSearch.svg"

# These paths are either relative to html_static_path
# or fully qualified paths (eg. https://...)
html_css_files = [
html_css_files: Any = [
"css/custom.css",
]

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
html_show_sphinx = False
html_show_sphinx: bool = False

# add github link
html_context = {
html_context: Any = {
"display_github": True,
"github_user": "opensearch-project",
"github_repo": "opensearch-py",
Expand All @@ -94,18 +96,18 @@
# -- autodoc config -------------------------------------------------
# This value controls how to represent typehints.
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_typehints
autodoc_typehints = "description"
autodoc_typehints: str = "description"

# This value selects what content will be inserted into the main body of an autoclass directive.
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autoclass_content
autoclass_content = "both"
autoclass_content: str = "both"

# https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-add_module_names
# add_module_names = False

# The default options for autodoc directives.
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_default_options
autodoc_default_options = {
autodoc_default_options: Any = {
# If set, autodoc will generate document for the members of the target module, class or exception. # noqa: E501
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-option-automodule-members
"members": True,
Expand Down
27 changes: 16 additions & 11 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# under the License.


from typing import Any

import nox

SOURCE_FILES = (
Expand All @@ -40,16 +42,16 @@
)


@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"])
def test(session) -> None:
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]) # type: ignore
def test(session: Any) -> None:
session.install(".")
session.install("-r", "dev-requirements.txt")

session.run("python", "setup.py", "test")


@nox.session()
def format(session) -> None:
@nox.session() # type: ignore
def format(session: Any) -> None:
session.install("black", "isort")

session.run("isort", "--profile=black", *SOURCE_FILES)
Expand All @@ -59,8 +61,8 @@ def format(session) -> None:
lint(session)


@nox.session(python=["3.7"])
def lint(session) -> None:
@nox.session(python=["3.7"]) # type: ignore
def lint(session: Any) -> None:
session.install(
"flake8",
"black",
Expand All @@ -70,6 +72,9 @@ def lint(session) -> None:
"types-six",
"types-simplejson",
"types-python-dateutil",
"types-PyYAML",
"types-mock",
"types-pytz",
)

session.run("isort", "--check", "--profile=black", *SOURCE_FILES)
Expand All @@ -82,7 +87,7 @@ def lint(session) -> None:

# Run mypy on the package and then the type examples separately for
# the two different mypy use-cases, ourselves and our users.
session.run("mypy", "--strict", "opensearchpy/")
session.run("mypy", "--strict", *SOURCE_FILES)
session.run("mypy", "--strict", "test_opensearchpy/test_types/sync_types.py")
session.run("mypy", "--strict", "test_opensearchpy/test_types/async_types.py")

Expand All @@ -93,17 +98,17 @@ def lint(session) -> None:
session.run("mypy", "--strict", "test_opensearchpy/test_types/sync_types.py")


@nox.session()
def docs(session) -> None:
@nox.session() # type: ignore
def docs(session: Any) -> None:
session.install(".")
session.install(
"-rdev-requirements.txt", "sphinx-rtd-theme", "sphinx-autodoc-typehints"
)
session.run("python", "-m", "pip", "install", "sphinx-autodoc-typehints")


@nox.session()
def generate(session) -> None:
@nox.session() # type: ignore
def generate(session: Any) -> None:
session.install("-rdev-requirements.txt")
session.run("python", "utils/generate-api.py")
format(session)
1 change: 1 addition & 0 deletions opensearchpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,5 @@
"AsyncTransport",
"AsyncOpenSearch",
"AsyncHttpConnection",
"__versionstr__",
]
14 changes: 6 additions & 8 deletions opensearchpy/_async/helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import collections.abc as collections_abc
from fnmatch import fnmatch
from typing import Any, Optional, Sequence, Tuple, Type
from typing import Any, Optional, Tuple, Type

from six import add_metaclass

Expand Down Expand Up @@ -128,9 +128,7 @@ def __repr__(self) -> str:
)

@classmethod
def search(
cls, using: Optional[AsyncOpenSearch] = None, index: Optional[str] = None
) -> AsyncSearch:
def search(cls, using: Any = None, index: Any = None) -> AsyncSearch:
"""
Create an :class:`~opensearchpy.AsyncSearch` instance that will search
over this ``Document``.
Expand All @@ -142,9 +140,9 @@ def search(
@classmethod
async def get( # type: ignore
cls,
id: str,
using: Optional[AsyncOpenSearch] = None,
index: Optional[str] = None,
id: Any,
using: Any = None,
index: Any = None,
**kwargs: Any,
) -> Any:
"""
Expand Down Expand Up @@ -189,7 +187,7 @@ async def exists(
@classmethod
async def mget(
cls,
docs: Sequence[str],
docs: Any,
using: Optional[AsyncOpenSearch] = None,
index: Optional[str] = None,
raise_on_error: Optional[bool] = True,
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/helpers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def save(self, using: Any = None) -> Any:


class AsyncIndex(object):
def __init__(self, name: Any, using: str = "default") -> None:
def __init__(self, name: Any, using: Any = "default") -> None:
"""
:arg name: name of the index
:arg using: connection alias to use, defaults to ``'default'``
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/http_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def close(self) -> None:


class AIOHttpConnection(AsyncConnection):
session: Optional[aiohttp.ClientSession]
session: aiohttp.ClientSession
ssl_assert_fingerprint: Optional[str]

def __init__(
Expand Down
Loading

0 comments on commit 56c96d7

Please sign in to comment.