From 189339d2a09d23ea1883286972f366e19b397f91 Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Mon, 18 Nov 2024 12:58:39 +0000 Subject: [PATCH] chore(internal): minor test changes (#1874) --- src/openai/_utils/_sync.py | 5 +++-- tests/test_client.py | 15 +++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/openai/_utils/_sync.py b/src/openai/_utils/_sync.py index c0a0ae714c..5d9e2c2ac9 100644 --- a/src/openai/_utils/_sync.py +++ b/src/openai/_utils/_sync.py @@ -14,7 +14,9 @@ if sys.version_info >= (3, 9): to_thread = asyncio.to_thread else: - async def _to_thread( + # backport of https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread + # for Python 3.8 support + async def to_thread( func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs ) -> Any: """Asynchronously run function *func* in a separate thread. @@ -31,7 +33,6 @@ async def _to_thread( func_call = functools.partial(ctx.run, func, *args, **kwargs) return await loop.run_in_executor(None, func_call) - to_thread = _to_thread # inspired by `asyncer`, https://github.com/tiangolo/asyncer def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]: diff --git a/tests/test_client.py b/tests/test_client.py index 08aff23f53..7caa8cb319 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1771,18 +1771,18 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success def test_get_platform(self) -> None: - # Issue https://github.com/openai/openai-python/issues/1827 was caused - # asyncify leaving threads unterminated when used with nest_asyncio. + # A previous implementation of asyncify could leave threads unterminated when + # used with nest_asyncio. + # # Since nest_asyncio.apply() is global and cannot be un-applied, this # test is run in a separate process to avoid affecting other tests. - test_code = dedent("""\ + test_code = dedent(""" import asyncio import nest_asyncio - import threading - from openai._base_client import get_platform from openai._utils import asyncify + from openai._base_client import get_platform async def test_main() -> None: result = await asyncify(get_platform)() @@ -1795,17 +1795,12 @@ async def test_main() -> None: """) with subprocess.Popen( [sys.executable, "-c", test_code], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True, ) as process: try: process.wait(2) if process.returncode: - print(process.stdout) - print(process.stderr) raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code") except subprocess.TimeoutExpired as e: process.kill() raise AssertionError("calling get_platform using asyncify resulted in a hung process") from e -