Skip to content

Commit 189339d

Browse files
author
Stainless Bot
committed
chore(internal): minor test changes (#1874)
1 parent 3d23437 commit 189339d

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/openai/_utils/_sync.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
if sys.version_info >= (3, 9):
1515
to_thread = asyncio.to_thread
1616
else:
17-
async def _to_thread(
17+
# backport of https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread
18+
# for Python 3.8 support
19+
async def to_thread(
1820
func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs
1921
) -> Any:
2022
"""Asynchronously run function *func* in a separate thread.
@@ -31,7 +33,6 @@ async def _to_thread(
3133
func_call = functools.partial(ctx.run, func, *args, **kwargs)
3234
return await loop.run_in_executor(None, func_call)
3335

34-
to_thread = _to_thread
3536

3637
# inspired by `asyncer`, https://github.com/tiangolo/asyncer
3738
def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]:

tests/test_client.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -1771,18 +1771,18 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
17711771
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
17721772

17731773
def test_get_platform(self) -> None:
1774-
# Issue https://github.com/openai/openai-python/issues/1827 was caused
1775-
# asyncify leaving threads unterminated when used with nest_asyncio.
1774+
# A previous implementation of asyncify could leave threads unterminated when
1775+
# used with nest_asyncio.
1776+
#
17761777
# Since nest_asyncio.apply() is global and cannot be un-applied, this
17771778
# test is run in a separate process to avoid affecting other tests.
1778-
test_code = dedent("""\
1779+
test_code = dedent("""
17791780
import asyncio
17801781
import nest_asyncio
1781-
17821782
import threading
17831783
1784-
from openai._base_client import get_platform
17851784
from openai._utils import asyncify
1785+
from openai._base_client import get_platform
17861786
17871787
async def test_main() -> None:
17881788
result = await asyncify(get_platform)()
@@ -1795,17 +1795,12 @@ async def test_main() -> None:
17951795
""")
17961796
with subprocess.Popen(
17971797
[sys.executable, "-c", test_code],
1798-
stdout=subprocess.PIPE,
1799-
stderr=subprocess.PIPE,
18001798
text=True,
18011799
) as process:
18021800
try:
18031801
process.wait(2)
18041802
if process.returncode:
1805-
print(process.stdout)
1806-
print(process.stderr)
18071803
raise AssertionError("calling get_platform using asyncify resulted in a non-zero exit code")
18081804
except subprocess.TimeoutExpired as e:
18091805
process.kill()
18101806
raise AssertionError("calling get_platform using asyncify resulted in a hung process") from e
1811-

0 commit comments

Comments
 (0)