pydantic-core icon indicating copy to clipboard operation
pydantic-core copied to clipboard

Work with Python coroutine in Rust?

Open kc0506 opened this issue 1 year ago • 5 comments

I am wondering if there is anyway to deal with Python coroutine in pydantic_core. I found the async-await section of PyO3 docs, but the feature seems not enabled for pydantic_core. Is there any other workarounds that is equivalent to async def and await in Python?

Context

I am suspecting the return_validator logic in pydantic._validate_call is actually a duplicate of the similar logic in call.rs. I tried just remove the Python part and every thing worked fine except for async function, which is currently working because of pydantic/pydantic#7046. The approach taken was to wrap an async function to await the coroutine:

async def return_val_wrapper(aw: Awaitable[Any]) -> None:
    return validator.validate_python(await aw)

self.__return_pydantic_validator__ = return_val_wrapper

Now that I want to remove the return_validator logic in Python and keep the Rust side, I will have to move this wrapper into call.rs, which is the reason I am opening this issue.

kc0506 avatar Sep 26 '24 13:09 kc0506