You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Backend options use_uvloop throws import error on test runner if uvloop is not installed #645
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
AnyIO version
4.1
Python version
3.11
What happened?
Hello !!
I'm working on a library using anyio to support asynchronous programming without depending explicitly on asyncio or trio. I'm opening my library to support not only from 3.7 but start supporting the newer 4 version.
While executing the tests with pytest (using tox to help me test for v3 and v4) and following the suggestion on the documentation to setup on pytest multiples backends (in my case, I have asyncio and asyncio+uvloop), when running the asyncio+uvloop it throws a exception
self = <anyio._backends._asyncio.TestRunner object at 0x7f79941defe0>
def __init__(
self,
*,
debug: bool | None = None,
use_uvloop: bool = False,
loop_factory: Callable[[], AbstractEventLoop] | None = None,
) -> None:
if use_uvloop and loop_factory is None:
> import uvloop
E ModuleNotFoundError: No module named 'uvloop'
I was making a little investigation between v3 and v4 code and I found that the code that made possible to use use_uvloop as a backend config, while not crashing if uvloop is present, or not, while fallback to "probably" the asyncio event loop stopped working.
It seems that this error happens only on test run, while executing some examples with production-ready code, everything is working as expected.
With this, I'm not sure if this was supposed to happen between v3 and v4 of anyio, but based on the documentation about backend_options is not clear to me if that behaviour is to keep, or since the major bump, it should have another behaviour.
How can we reproduce the bug?
The way to reproduce this behaviour is to have distinct environments, one with anyio v3 while the other with anyio v4.
In both environments, they don't have any dependency which install uvloop.
I share the same opinion, but from the documentation and anyio v3 code perspective it seems that it was OK to "think" that we were running on uvloop, when executing the tests for different backends.
That is why I'm still not sure if this kind of change was supposed to happen between anyio v3 and v4, just because I couldn't found the issue talking about this change of behaviour :) .
The documentation does use the words "if available", hinting that it shouldn't produce an error if uvloop isn't available. On the other hand, it says "this is a shorthand for passing loop_factory=uvloop.new_event_loop". The two statements are incompatible. I'm not sure which way to go.
Activity
agronholm commentedon Dec 4, 2023
Are you saying that
use_uvloop=Truedidn't crash previously whenuvloopwas not installed?I read this multiple times and I'm still not sure what you're getting at.
RubenRibGarcia commentedon Dec 4, 2023
Yes, using
use_uvloop=truewithoutuvloopinstalled didn't crash previouslyagronholm commentedon Dec 4, 2023
I think it should crash though, as otherwise you might think you're running on uvloop although you're not.
RubenRibGarcia commentedon Dec 4, 2023
I share the same opinion, but from the documentation and
anyiov3 code perspective it seems that it was OK to "think" that we were running onuvloop, when executing the tests for different backends.That is why I'm still not sure if this kind of change was supposed to happen between
anyiov3 and v4, just because I couldn't found the issue talking about this change of behaviour :) .agronholm commentedon Dec 4, 2023
There were tons of changes from v3 to v4, and it's possible that a nuance like this slipped through w/o being explicitly documented in the changelog.
RubenRibGarcia commentedon Dec 7, 2023
If it's to keep this behaviour, I will open a PR to make it explicitly on the documentation about this
agronholm commentedon Dec 9, 2023
The documentation does use the words "if available", hinting that it shouldn't produce an error if
uvloopisn't available. On the other hand, it says "this is a shorthand for passingloop_factory=uvloop.new_event_loop". The two statements are incompatible. I'm not sure which way to go.Clarify optional event loop dependencies in the docs
Clarify optional event loop dependencies in the docs (#1370)