Skip to content
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

Backend options use_uvloop throws import error on test runner if uvloop is not installed #645

Open
2 tasks done
RubenRibGarcia opened this issue Dec 4, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@RubenRibGarcia
Copy link

Things to check first

  • 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.

@RubenRibGarcia RubenRibGarcia added the bug Something isn't working label Dec 4, 2023
@agronholm
Copy link
Owner

Are you saying that use_uvloop=True didn't crash previously when uvloop was not installed?

I read this multiple times and I'm still not sure what you're getting at.

@RubenRibGarcia
Copy link
Author

Yes, using use_uvloop=true without uvloop installed didn't crash previously

@agronholm
Copy link
Owner

I think it should crash though, as otherwise you might think you're running on uvloop although you're not.

@RubenRibGarcia
Copy link
Author

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 :) .

@agronholm
Copy link
Owner

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
Copy link
Author

If it's to keep this behaviour, I will open a PR to make it explicitly on the documentation about this

@agronholm
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants