Skip to content

Commit

Permalink
chore(internal): minor test changes (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stainless Bot committed Nov 18, 2024
1 parent 3d23437 commit 189339d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/openai/_utils/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]]:
Expand Down
15 changes: 5 additions & 10 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand All @@ -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

0 comments on commit 189339d

Please sign in to comment.