Remove redundant mock backport dependency and upgrade syntax for Python 3.8+ (#785)

* Upgrade syntax with pyupgrade --py38-plus

Signed-off-by: Hugo van Kemenade <[email protected]>

* Convert to f-strings with flynt

Signed-off-by: Hugo van Kemenade <[email protected]>

* Format with Black

Signed-off-by: Hugo van Kemenade <[email protected]>

* Remove redundant mock backport dependency

Signed-off-by: Hugo van Kemenade <[email protected]>

* isort imports

Signed-off-by: Hugo van Kemenade <[email protected]>

* Add changelog entry

Signed-off-by: Hugo van Kemenade <[email protected]>

---------

Signed-off-by: Hugo van Kemenade <[email protected]>
This commit is contained in:
Hugo van Kemenade
2024-07-20 16:19:20 -04:00
committed by GitHub
parent de96d28e45
commit 6e3f1a1194
95 changed files with 229 additions and 300 deletions
@@ -32,11 +32,11 @@ import ssl
import warnings
from platform import python_version
from typing import Any
from unittest.mock import MagicMock, patch
import aiohttp
import pytest
from _pytest.mark.structures import MarkDecorator
from mock import MagicMock, patch
from multidict import CIMultiDict
from pytest import raises
@@ -154,7 +154,7 @@ class TestAIOHttpConnection:
async def test_default_user_agent(self) -> None:
con = AIOHttpConnection()
assert con._get_default_user_agent() == "opensearch-py/%s (Python %s)" % (
assert con._get_default_user_agent() == "opensearch-py/{} (Python {})".format(
__versionstr__,
python_version(),
)
@@ -342,7 +342,7 @@ class TestAIOHttpConnection:
buf = b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"
con = await self._get_mock_connection(response_body=buf)
_, _, data = await con.perform_request("GET", "/")
assert u"你好\uda6a" == data # fmt: skip
assert "你好\uda6a" == data # fmt: skip
@pytest.mark.parametrize("exception_cls", reraise_exceptions) # type: ignore
async def test_recursion_error_reraised(self, exception_cls: Any) -> None:
@@ -9,10 +9,10 @@
from typing import Any
from unittest.mock import Mock
import pytest
from _pytest.mark.structures import MarkDecorator
from mock import Mock
from pytest import fixture
from opensearchpy.connection.async_connections import add_connection, async_connections
@@ -7,7 +7,6 @@
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
from __future__ import unicode_literals
import codecs
import ipaddress
@@ -73,7 +73,7 @@ async def test_cloned_index_has_analysis_attribute() -> None:
client = object()
i = AsyncIndex("my-index", using=client)
random_analyzer_name = "".join((choice(string.ascii_letters) for _ in range(100)))
random_analyzer_name = "".join(choice(string.ascii_letters) for _ in range(100))
random_analyzer = analyzer(
random_analyzer_name, tokenizer="standard", filter="standard"
)
@@ -117,7 +117,7 @@ async def test_registered_doc_type_included_in_search() -> None:
async def test_aliases_add_to_object() -> None:
random_alias = "".join((choice(string.ascii_letters) for _ in range(100)))
random_alias = "".join(choice(string.ascii_letters) for _ in range(100))
alias_dict: Any = {random_alias: {}}
index = AsyncIndex("i", using="alias")
@@ -127,7 +127,7 @@ async def test_aliases_add_to_object() -> None:
async def test_aliases_returned_from_to_dict() -> None:
random_alias = "".join((choice(string.ascii_letters) for _ in range(100)))
random_alias = "".join(choice(string.ascii_letters) for _ in range(100))
alias_dict: Any = {random_alias: {}}
index = AsyncIndex("i", using="alias")
@@ -137,7 +137,7 @@ async def test_aliases_returned_from_to_dict() -> None:
async def test_analyzers_added_to_object() -> None:
random_analyzer_name = "".join((choice(string.ascii_letters) for _ in range(100)))
random_analyzer_name = "".join(choice(string.ascii_letters) for _ in range(100))
random_analyzer = analyzer(
random_analyzer_name, tokenizer="standard", filter="standard"
)
@@ -153,7 +153,7 @@ async def test_analyzers_added_to_object() -> None:
async def test_analyzers_returned_from_to_dict() -> None:
random_analyzer_name = "".join((choice(string.ascii_letters) for _ in range(100)))
random_analyzer_name = "".join(choice(string.ascii_letters) for _ in range(100))
random_analyzer = analyzer(
random_analyzer_name, tokenizer="standard", filter="standard"
)
@@ -26,8 +26,8 @@
from typing import Any
from unittest import mock
import mock
import pytest
from multidict import CIMultiDict
@@ -25,8 +25,6 @@
# under the License.
from __future__ import unicode_literals
from typing import Any
import pytest
@@ -27,9 +27,9 @@
import asyncio
from typing import Any, List
from unittest.mock import MagicMock, patch
import pytest
from mock import MagicMock, patch
from opensearchpy import TransportError
from opensearchpy._async.helpers import actions
@@ -40,13 +40,13 @@ pytestmark = pytest.mark.asyncio
class AsyncMock(MagicMock):
async def __call__(self, *args: Any, **kwargs: Any) -> Any:
return super(AsyncMock, self).__call__(*args, **kwargs)
return super().__call__(*args, **kwargs)
def __await__(self) -> Any:
return self().__await__()
class FailingBulkClient(object):
class FailingBulkClient:
def __init__(
self,
client: Any,
@@ -69,7 +69,7 @@ class FailingBulkClient(object):
return await self.client.bulk(*args, **kwargs)
class TestStreamingBulk(object):
class TestStreamingBulk:
async def test_actions_remain_unchanged(self, async_client: Any) -> None:
actions1 = [{"_id": 1}, {"_id": 2}]
async for ok, _ in actions.async_streaming_bulk(
@@ -281,7 +281,7 @@ class TestStreamingBulk(object):
assert 4 == failing_client._called
class TestBulk(object):
class TestBulk:
async def test_bulk_works_with_single_item(self, async_client: Any) -> None:
docs = [{"answer": 42, "_id": 1}]
success, failed = await actions.async_bulk(
@@ -453,7 +453,7 @@ async def scan_teardown(async_client: Any) -> Any:
await async_client.clear_scroll(scroll_id="_all")
class TestScan(object):
class TestScan:
async def test_order_can_be_preserved(
self, async_client: Any, scan_teardown: Any
) -> None:
@@ -492,8 +492,8 @@ class TestScan(object):
]
assert 100 == len(docs)
assert set(map(str, range(100))) == set(d["_id"] for d in docs)
assert set(range(100)) == set(d["_source"]["answer"] for d in docs)
assert set(map(str, range(100))) == {d["_id"] for d in docs}
assert set(range(100)) == {d["_source"]["answer"] for d in docs}
async def test_scroll_error(self, async_client: Any, scan_teardown: Any) -> None:
bulk: Any = []
@@ -824,7 +824,7 @@ async def reindex_setup(async_client: Any) -> Any:
yield
class TestReindex(object):
class TestReindex:
async def test_reindex_passes_kwargs_to_scan_and_bulk(
self, async_client: Any, reindex_setup: Any
) -> None:
@@ -7,7 +7,6 @@
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
from __future__ import unicode_literals
from typing import Any, Dict
@@ -64,7 +64,7 @@ class Repository(AsyncDocument):
@classmethod
def search(cls, using: Any = None, index: Optional[str] = None) -> Any:
return super(Repository, cls).search().filter("term", commit_repo="repo")
return super().search().filter("term", commit_repo="repo")
class Index:
name = "git"
@@ -98,7 +98,7 @@ def repo_search_cls(opensearch_version: Any) -> Any:
}
def search(self) -> Any:
s = super(RepoSearch, self).search()
s = super().search()
return s.filter("term", commit_repo="repo")
return RepoSearch
@@ -7,7 +7,6 @@
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
from __future__ import unicode_literals
from typing import Any
@@ -31,7 +30,7 @@ class Repository(AsyncDocument):
@classmethod
def search(cls, using: Any = None, index: Any = None) -> Any:
return super(Repository, cls).search().filter("term", commit_repo="repo")
return super().search().filter("term", commit_repo="repo")
class Index:
name = "git"
@@ -8,8 +8,6 @@
# GitHub history for details.
from __future__ import unicode_literals
import unittest
import pytest
@@ -8,8 +8,6 @@
# GitHub history for details.
from __future__ import unicode_literals
import pytest
from _pytest.mark.structures import MarkDecorator
@@ -121,7 +121,7 @@ class AsyncYamlRunner(YamlRunner):
if hasattr(self, "run_" + action_type):
await await_if_coro(getattr(self, "run_" + action_type)(action))
else:
raise RuntimeError("Invalid action type %r" % (action_type,))
raise RuntimeError(f"Invalid action type {action_type!r}")
async def run_do(self, action: Any) -> Any:
api = self.client
@@ -170,7 +170,7 @@ class AsyncYamlRunner(YamlRunner):
else:
if catch:
raise AssertionError(
"Failed to catch %r in %r." % (catch, self.last_response)
f"Failed to catch {catch!r} in {self.last_response!r}."
)
# Filter out warnings raised by other components.
@@ -197,7 +197,7 @@ class AsyncYamlRunner(YamlRunner):
for feature in features:
if feature in IMPLEMENTED_FEATURES:
continue
pytest.skip("feature '%s' is not supported" % feature)
pytest.skip(f"feature '{feature}' is not supported")
if "version" in skip:
version, reason = skip["version"], skip["reason"]
@@ -8,8 +8,6 @@
# GitHub history for details.
from __future__ import unicode_literals
import os
from unittest import IsolatedAsyncioTestCase
+1 -1
View File
@@ -8,10 +8,10 @@
# GitHub history for details.
import uuid
from unittest.mock import Mock
import pytest
from _pytest.mark.structures import MarkDecorator
from mock import Mock
pytestmark: MarkDecorator = pytest.mark.asyncio
@@ -25,15 +25,13 @@
# under the License.
from __future__ import unicode_literals
import asyncio
import json
from typing import Any
from unittest.mock import patch
import pytest
from _pytest.mark.structures import MarkDecorator
from mock import patch
from opensearchpy import AIOHttpConnection, AsyncTransport
from opensearchpy.connection import Connection
@@ -51,7 +49,7 @@ class DummyConnection(Connection):
self.delay = kwargs.pop("delay", 0)
self.calls: Any = []
self.closed = False
super(DummyConnection, self).__init__(**kwargs)
super().__init__(**kwargs)
async def perform_request(self, *args: Any, **kwargs: Any) -> Any:
if self.closed:
@@ -253,7 +251,7 @@ class TestTransport:
assert dt is t.connection_pool.dead_timeout
async def test_custom_connection_class(self) -> None:
class MyConnection(object):
class MyConnection:
def __init__(self, **kwargs: Any) -> None:
self.kwargs = kwargs