-
-
Notifications
You must be signed in to change notification settings - Fork 330
[V3] Function signatures for the sync API #1803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
To add some extra context. The sync approach we're using came from zarrita which came from fsspec which was written by @martindurant 🙌 . There is some black magic in there, no doubt -- but it does seem to work. cc @dstansby who has been doing lots of work on the Zarr v3 type checkers |
to summarize some comversation from #1804, one approach would be to import all of this stuff from fsspec, and implement as little as possible within @martindurant suggested this idea |
This issue is for discussions of the v3 API used for calling async functions from synchronous functions. (Not to be confused with the API for synchronizing the state of storage across multiple workers). I couldn't find a previous issue for discussing the sync api in v3; happy to close this if one already exists.
In v3 we synchronize async code with the Sync mixin class
Sync
has a.sync
method, which just wraps thesync
function defined in the same module.The body of
sync
looks like this:This function in turn relies on
_runner
, which among its arguments takes a list, which it will mutate. Notably,_runner
returnsNone
--_runner
does its work by mutating one of its arguments.Is there a way to get the same behavior with a conventional function that returns a value? I.e., without something like
_runner
mutating one of its arguments. Becausesync
relies on_runner
mutating theresult_box
variable, and becauseresult_box
is initialized to[None]
, it becomes impossible to distinguish "_runner ran a function that returnedNone
" from "_runner never ran anything".The type annotation we want for
sync
would be something likedef sync(coro: Coroutine[Any, Any, T], loop: asyncio.AbstractEventLoop | None = None) -> T
, but the use ofresult_box
without some way to ensure that we never return its initial value ofNone
forces us to instead have a return type ofT | None
, which isn't really what we want fromsync
-- it should only ever return the result of calling the coroutine, never a dummy value.I got something to work that avoids the need for
result_box
, which I will open in a companion PR to this issue.The text was updated successfully, but these errors were encountered: