Skip to content

Commit

Permalink
Remove redundant mock backport dependency and upgrade syntax for Pyth…
Browse files Browse the repository at this point in the history
…on 3.8+ (#785)

* Upgrade syntax with pyupgrade --py38-plus

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

* Convert to f-strings with flynt

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

* Format with Black

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

* Remove redundant mock backport dependency

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

* isort imports

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

* Add changelog entry

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>

---------

Signed-off-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
  • Loading branch information
hugovk committed Jul 20, 2024
1 parent de96d28 commit 6e3f1a1
Show file tree
Hide file tree
Showing 95 changed files with 229 additions and 300 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Deprecated
### Removed
- Removed redundant dependency on six ([#781](https://github.com/opensearch-project/opensearch-py/pull/781))
- Removed redundant dependency on mock and upgrade Python syntax ([#785](https://github.com/opensearch-project/opensearch-py/pull/785))
### Fixed
- Fixed Search helper to ensure proper retention of the _collapse attribute in chained operations. ([#771](https://github.com/opensearch-project/opensearch-py/pull/771))
### Updated APIs
Expand Down
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ requests>=2, <3
pytest
pytest-cov
coverage
mock
sphinx<7.4
sphinx_rtd_theme
jinja2
Expand Down
1 change: 0 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def lint(session: Any) -> None:
"types-simplejson",
"types-python-dateutil",
"types-PyYAML",
"types-mock",
"types-pytz",
)

Expand Down
5 changes: 2 additions & 3 deletions opensearchpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@


# flake8: noqa
from __future__ import absolute_import

import logging
import re
import warnings

from ._version import __versionstr__

_major, _minor, _patch = [
_major, _minor, _patch = (
int(x) for x in re.search(r"^(\d+)\.(\d+)\.(\d+)", __versionstr__).groups() # type: ignore
]
)

VERSION = __version__ = (_major, _minor, _patch)

Expand Down
8 changes: 3 additions & 5 deletions opensearchpy/_async/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
# -----------------------------------------------------------------------------------------+


from __future__ import unicode_literals

import logging
from typing import Any, Type

Expand Down Expand Up @@ -197,7 +195,7 @@ def __init__(
self,
hosts: Any = None,
transport_class: Type[AsyncTransport] = AsyncTransport,
**kwargs: Any
**kwargs: Any,
) -> None:
"""
:arg hosts: list of nodes, or a single node, we should connect to.
Expand Down Expand Up @@ -240,10 +238,10 @@ def __repr__(self) -> Any:
# truncate to 5 if there are too many
if len(cons) > 5:
cons = cons[:5] + ["..."]
return "<{cls}({cons})>".format(cls=self.__class__.__name__, cons=cons)
return f"<{self.__class__.__name__}({cons})>"
except Exception:
# probably operating on custom transport and connection_pool, ignore
return super(AsyncOpenSearch, self).__repr__()
return super().__repr__()

async def __aenter__(self) -> Any:
if hasattr(self.transport, "_async_call"):
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from opensearchpy.transport import Transport


class Client(object):
class Client:
"""
A generic async OpenSearch client.
"""
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class HttpClient(NamespacedClient):
def __init__(self, client: Client) -> None:
super(HttpClient, self).__init__(client)
super().__init__(client)

async def get(
self,
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/client/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PluginsClient(NamespacedClient):
index_management: Any

def __init__(self, client: Client) -> None:
super(PluginsClient, self).__init__(client)
super().__init__(client)
self.ml = MlClient(client)
self.transforms = TransformsClient(client)
self.rollups = RollupsClient(client)
Expand Down
4 changes: 2 additions & 2 deletions opensearchpy/_async/helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __repr__(self) -> str:
return "{}({})".format(
self.__class__.__name__,
", ".join(
"{}={!r}".format(key, getattr(self.meta, key))
f"{key}={getattr(self.meta, key)!r}"
for key in ("index", "id")
if key in self.meta
),
Expand Down Expand Up @@ -249,7 +249,7 @@ async def mget(
raise RequestError(400, message, error_docs)
if missing_docs:
missing_ids = [doc["_id"] for doc in missing_docs]
message = "Documents %s not found." % ", ".join(missing_ids)
message = f"Documents {', '.join(missing_ids)} not found."
raise NotFoundError(404, message, {"docs": missing_docs})
return objs

Expand Down
4 changes: 2 additions & 2 deletions opensearchpy/_async/helpers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from opensearchpy.helpers.utils import merge


class AsyncIndexTemplate(object):
class AsyncIndexTemplate:
def __init__(
self,
name: Any,
Expand Down Expand Up @@ -57,7 +57,7 @@ async def save(self, using: Any = None) -> Any:
)


class AsyncIndex(object):
class AsyncIndex:
def __init__(self, name: Any, using: Any = "default") -> None:
"""
:arg name: name of the index
Expand Down
4 changes: 2 additions & 2 deletions opensearchpy/_async/helpers/update_by_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, **kwargs: Any) -> None:
overridden by methods (`using`, `index` and `doc_type` respectively).
"""
super(AsyncUpdateByQuery, self).__init__(**kwargs)
super().__init__(**kwargs)
self._response_class = UpdateByQueryResponse
self._script: Any = {}
self._query_proxy = QueryProxy(self, "query")
Expand Down Expand Up @@ -70,7 +70,7 @@ def _clone(self) -> Any:
of all the underlying objects. Used internally by most state modifying
APIs.
"""
ubq = super(AsyncUpdateByQuery, self)._clone()
ubq = super()._clone()

ubq._response_class = self._response_class
ubq._script = self._script.copy()
Expand Down
6 changes: 3 additions & 3 deletions opensearchpy/_async/http_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(
opaque_id: Optional[str] = None,
loop: Any = None,
trust_env: Optional[bool] = False,
**kwargs: Any
**kwargs: Any,
) -> None:
"""
Default connection class for ``AsyncOpenSearch`` using the `aiohttp` library and the http protocol.
Expand Down Expand Up @@ -140,7 +140,7 @@ def __init__(
headers=headers,
http_compress=http_compress,
opaque_id=opaque_id,
**kwargs
**kwargs,
)

if http_auth is not None:
Expand Down Expand Up @@ -276,7 +276,7 @@ async def perform_request(
else:
url = self.url_prefix + url
if query_string:
url = "%s?%s" % (url, query_string)
url = f"{url}?{query_string}"
url = self.host + url

timeout = aiohttp.ClientTimeout(
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def __init__(
self._async_init_called = False
self._sniff_on_start_event: Optional[asyncio.Event] = None

super(AsyncTransport, self).__init__(
super().__init__(
hosts=[],
connection_class=connection_class,
connection_pool_class=connection_pool_class,
Expand Down
8 changes: 3 additions & 5 deletions opensearchpy/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
# -----------------------------------------------------------------------------------------+


from __future__ import unicode_literals

import logging
from typing import Any, Type

Expand Down Expand Up @@ -197,7 +195,7 @@ def __init__(
self,
hosts: Any = None,
transport_class: Type[Transport] = Transport,
**kwargs: Any
**kwargs: Any,
) -> None:
"""
:arg hosts: list of nodes, or a single node, we should connect to.
Expand Down Expand Up @@ -240,10 +238,10 @@ def __repr__(self) -> Any:
# truncate to 5 if there are too many
if len(cons) > 5:
cons = cons[:5] + ["..."]
return "<{cls}({cons})>".format(cls=self.__class__.__name__, cons=cons)
return f"<{self.__class__.__name__}({cons})>"
except Exception:
# probably operating on custom transport and connection_pool, ignore
return super(OpenSearch, self).__repr__()
return super().__repr__()

def __enter__(self) -> Any:
if hasattr(self.transport, "_async_call"):
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from opensearchpy.transport import Transport


class Client(object):
class Client:
"""
A generic async OpenSearch client.
"""
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class HttpClient(NamespacedClient):
def __init__(self, client: Client) -> None:
super(HttpClient, self).__init__(client)
super().__init__(client)

def get(
self,
Expand Down
2 changes: 1 addition & 1 deletion opensearchpy/client/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PluginsClient(NamespacedClient):
index_management: Any

def __init__(self, client: Client) -> None:
super(PluginsClient, self).__init__(client)
super().__init__(client)
self.ml = MlClient(client)
self.transforms = TransformsClient(client)
self.rollups = RollupsClient(client)
Expand Down
14 changes: 5 additions & 9 deletions opensearchpy/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
# under the License.


from __future__ import unicode_literals

import base64
import weakref
from datetime import date, datetime
Expand Down Expand Up @@ -59,7 +57,7 @@ def _normalize_hosts(hosts: Any) -> Any:
for host in hosts:
if isinstance(host, string_types):
if "://" not in host:
host = "//%s" % host # type: ignore
host = f"//{host}" # type: ignore

parsed_url = urlparse(host)
h = {"host": parsed_url.hostname}
Expand All @@ -72,7 +70,7 @@ def _normalize_hosts(hosts: Any) -> Any:
h["use_ssl"] = True

if parsed_url.username or parsed_url.password:
h["http_auth"] = "%s:%s" % (
h["http_auth"] = "{}:{}".format(
unquote(parsed_url.username),
unquote(parsed_url.password),
)
Expand Down Expand Up @@ -160,11 +158,9 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:
"Only one of 'http_auth' and 'api_key' may be passed at a time"
)
elif http_auth is not None:
headers["authorization"] = "Basic %s" % (
_base64_auth_header(http_auth),
)
headers["authorization"] = f"Basic {_base64_auth_header(http_auth)}"
elif api_key is not None:
headers["authorization"] = "ApiKey %s" % (_base64_auth_header(api_key),)
headers["authorization"] = f"ApiKey {_base64_auth_header(api_key)}"

# don't escape ignore, request_timeout, or timeout
for p in ("ignore", "request_timeout", "timeout"):
Expand Down Expand Up @@ -209,7 +205,7 @@ def _base64_auth_header(auth_value: Any) -> str:
return to_str(auth_value)


class NamespacedClient(object):
class NamespacedClient:
def __init__(self, client: Any) -> None:
self.client = client

Expand Down
4 changes: 2 additions & 2 deletions opensearchpy/connection/async_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def remove_connection(self, alias: str) -> None:
errors += 1

if errors == 2:
raise KeyError("There is no connection with alias %r." % alias)
raise KeyError(f"There is no connection with alias {alias!r}.")

async def create_connection(self, alias: str = "default", **kwargs: Any) -> Any:
"""
Expand Down Expand Up @@ -104,7 +104,7 @@ async def get_connection(self, alias: str = "default") -> Any:
return await self.create_connection(alias, **self._kwargs[alias])
except KeyError:
# no connection and no kwargs to set one up
raise KeyError("There is no connection with alias %r." % alias)
raise KeyError(f"There is no connection with alias {alias!r}.")


async_connections = AsyncConnections()
Expand Down
18 changes: 9 additions & 9 deletions opensearchpy/connection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
_WARNING_RE = re.compile(r"\"([^\"]*)\"")


class Connection(object):
class Connection:
"""
Class responsible for maintaining a connection to an OpenSearch node. It
holds persistent connection pool to it and its main interface
Expand Down Expand Up @@ -81,7 +81,7 @@ def __init__(
headers: Optional[Dict[str, str]] = None,
http_compress: Optional[bool] = None,
opaque_id: Optional[str] = None,
**kwargs: Any
**kwargs: Any,
) -> None:
if port is None:
port = 9200
Expand Down Expand Up @@ -119,27 +119,27 @@ def __init__(
self.hostname = host
self.port = port
if ":" in host: # IPv6
self.host = "%s://[%s]" % (scheme, host)
self.host = f"{scheme}://[{host}]"
else:
self.host = "%s://%s" % (scheme, host)
self.host = f"{scheme}://{host}"
if self.port is not None:
self.host += ":%s" % self.port
self.host += f":{self.port}"
if url_prefix:
url_prefix = "/" + url_prefix.strip("/")
self.url_prefix = url_prefix
self.timeout = timeout

def __repr__(self) -> str:
return "<%s: %s>" % (self.__class__.__name__, self.host)
return f"<{self.__class__.__name__}: {self.host}>"

def __eq__(self, other: object) -> bool:
if not isinstance(other, Connection):
raise TypeError("Unsupported equality check for %s and %s" % (self, other))
raise TypeError(f"Unsupported equality check for {self} and {other}")
return self.__hash__() == other.__hash__()

def __lt__(self, other: object) -> bool:
if not isinstance(other, Connection):
raise TypeError("Unsupported lt check for %s and %s" % (self, other))
raise TypeError(f"Unsupported lt check for {self} and {other}")
return self.__hash__() < other.__hash__()

def __hash__(self) -> int:
Expand Down Expand Up @@ -317,7 +317,7 @@ def _raise_error(
)

def _get_default_user_agent(self) -> str:
return "opensearch-py/%s (Python %s)" % (__versionstr__, python_version())
return f"opensearch-py/{__versionstr__} (Python {python_version()})"

@staticmethod
def default_ca_certs() -> Union[str, None]:
Expand Down
4 changes: 2 additions & 2 deletions opensearchpy/connection/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def remove_connection(self, alias: str) -> None:
errors += 1

if errors == 2:
raise KeyError("There is no connection with alias %r." % alias)
raise KeyError(f"There is no connection with alias {alias!r}.")

def create_connection(self, alias: str = "default", **kwargs: Any) -> Any:
"""
Expand Down Expand Up @@ -118,7 +118,7 @@ def get_connection(self, alias: str = "default") -> Any:
return self.create_connection(alias, **self._kwargs[alias])
except KeyError:
# no connection and no kwargs to set one up
raise KeyError("There is no connection with alias %r." % alias)
raise KeyError(f"There is no connection with alias {alias!r}.")


connections = Connections()
Expand Down
Loading

0 comments on commit 6e3f1a1

Please sign in to comment.