fastapi / asyncer

Asyncer, async and await, focused on developer experience.
https://asyncer.tiangolo.com/
MIT License
1.62k stars 57 forks source link

Correctly determine AnyIO support kwargs for run_sync #224

Open Stealthii opened 2 days ago

Stealthii commented 2 days ago

202 introduces an issue on py38 and py39 with an earlier version of importlib.metadata not detecting versions in certain virtual environments. It also uses string comparisons:

Traceback (most recent call last):
  File "example/lib64/python3.8/site-packages/asyncer/__init__.py", line 3, in <module>
    from ._main import PendingValueException as PendingValueException
  File "example/lib64/python3.8/site-packages/asyncer/_main.py", line 17, in <module>
    from asyncer._compat import run_sync
  File "example/lib64/python3.8/site-packages/asyncer/_compat.py", line 16, in <module>
    if ANYIO_VERSION >= "4.1.0":
TypeError: '>=' not supported between instances of 'NoneType' and 'str'

This PR addresses the issue by using inspect to determine anyio's supported kwargs for the run_sync method.

Stealthii commented 2 days ago

This bug affects v0.0.8 (the latest version) only.

The PR also contains as prior solution to correctly detect AnyIO package version using importlib-metadata and numeric comparison for Python < 3.10, however this was ultimately replaced with the kwargs check.

We should avoid relying on importlib.metadata outside of build environments as it relies heavily on discoverable dist-info or egg-info metadata, which may not be available in non-virtualenv setups (such as system-level packages).