diff --git a/.stats.yml b/.stats.yml index c63d10a5..c539f8e1 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,2 +1,2 @@ configured_endpoints: 41 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/finch%2Ffinch-bdcde3824560a11eb7286deb9068127ac50ca5d2e4036d1400a8feba24f485a8.yml +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/finch%2Ffinch-ca7f774eb91877a7573cadf88c8aa3c26c2e7133ba3855dbca5a434b46c762b5.yml diff --git a/src/finch/_utils/_sync.py b/src/finch/_utils/_sync.py index 8b3aaf2b..ad7ec71b 100644 --- a/src/finch/_utils/_sync.py +++ b/src/finch/_utils/_sync.py @@ -7,16 +7,20 @@ from typing import Any, TypeVar, Callable, Awaitable from typing_extensions import ParamSpec +import anyio +import sniffio +import anyio.to_thread + T_Retval = TypeVar("T_Retval") T_ParamSpec = ParamSpec("T_ParamSpec") if sys.version_info >= (3, 9): - to_thread = asyncio.to_thread + _asyncio_to_thread = asyncio.to_thread else: # backport of https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread # for Python 3.8 support - async def to_thread( + async def _asyncio_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. @@ -34,6 +38,17 @@ async def to_thread( return await loop.run_in_executor(None, func_call) +async def to_thread( + func: Callable[T_ParamSpec, T_Retval], /, *args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs +) -> T_Retval: + if sniffio.current_async_library() == "asyncio": + return await _asyncio_to_thread(func, *args, **kwargs) + + return await anyio.to_thread.run_sync( + functools.partial(func, *args, **kwargs), + ) + + # 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/api_resources/sandbox/test_employment.py b/tests/api_resources/sandbox/test_employment.py index 677cbc8e..368ae958 100644 --- a/tests/api_resources/sandbox/test_employment.py +++ b/tests/api_resources/sandbox/test_employment.py @@ -73,7 +73,7 @@ def test_method_update_with_all_params(self, client: Finch) -> None: manager={"id": "id"}, middle_name="middle_name", source_id="source_id", - start_date="3/4/2020", + start_date="start_date", title="title", ) assert_matches_type(EmploymentUpdateResponse, employment, path=["response"]) @@ -169,7 +169,7 @@ async def test_method_update_with_all_params(self, async_client: AsyncFinch) -> manager={"id": "id"}, middle_name="middle_name", source_id="source_id", - start_date="3/4/2020", + start_date="start_date", title="title", ) assert_matches_type(EmploymentUpdateResponse, employment, path=["response"]) diff --git a/tests/api_resources/sandbox/test_individual.py b/tests/api_resources/sandbox/test_individual.py index 1f2e1ca3..23239fc2 100644 --- a/tests/api_resources/sandbox/test_individual.py +++ b/tests/api_resources/sandbox/test_individual.py @@ -28,7 +28,7 @@ def test_method_update(self, client: Finch) -> None: def test_method_update_with_all_params(self, client: Finch) -> None: individual = client.sandbox.individual.update( individual_id="individual_id", - dob="12/20/1989", + dob="dob", emails=[ { "data": "data", @@ -108,7 +108,7 @@ async def test_method_update(self, async_client: AsyncFinch) -> None: async def test_method_update_with_all_params(self, async_client: AsyncFinch) -> None: individual = await async_client.sandbox.individual.update( individual_id="individual_id", - dob="12/20/1989", + dob="dob", emails=[ { "data": "data",