Skip to content

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

Closed

Description

@RubenRibGarcia

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.

Activity

  1. added
    bug
    on Dec 4, 2023
  2. agronholm commented on Dec 4, 2023

    @agronholm
    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.

  3. RubenRibGarcia commented on Dec 4, 2023

    @RubenRibGarcia
    Author

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

  4. agronholm commented on Dec 4, 2023

    @agronholm
    Owner

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

  5. RubenRibGarcia commented on Dec 4, 2023

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

  6. agronholm commented on Dec 4, 2023

    @agronholm
    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.

  7. RubenRibGarcia commented on Dec 7, 2023

    @RubenRibGarcia
    Author

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

  8. agronholm commented on Dec 9, 2023

    @agronholm
    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.

  9. added a commit that references this issue on Oct 4, 2026
    b53d4b6
  10. added a commit that references this issue on Oct 10, 2026
    4dc7cbf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions