Skip to content

Work with Python coroutine in Rust? #1468

@kc0506

Description

@kc0506

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions