agronholm / agronholm/anyio

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

オープン
#645 コメント 7 件 リアクション 0 件 担当者 0 名 GitHub で見る
bug
主要言語
Python
スター
2.5k
フォーク
260
平均マージ
2日 8時間
マージ済み PR(30日)
19

説明

### Things to check first

- [X] I have searched the existing issues and didn't find my bug already reported there

- [X] 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](https://anyio.readthedocs.io/en/stable/testing.html#specifying-the-backends-to-run-on) 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 =

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](https://github.com/agronholm/anyio/compare/3.7.1...4.1.0#diff-202c3fc4551e5a287df488a9bd96a25dcc1405ec135d5c7aceb6ef4c02755341L250) 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](https://anyio.readthedocs.io/en/stable/basics.html#backend-specific-options) 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`.

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。