diff --git a/changelog.d/1394.changed.rst b/changelog.d/1394.changed.rst new file mode 100644 index 00000000..de757df7 --- /dev/null +++ b/changelog.d/1394.changed.rst @@ -0,0 +1,2 @@ +Only import ``asyncio.AbstractEventLoopPolicy`` for type checking to avoid raising +a DeprecationWarning. diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index a69350bd..5358d476 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -12,7 +12,7 @@ import sys import traceback import warnings -from asyncio import AbstractEventLoop, AbstractEventLoopPolicy +from asyncio import AbstractEventLoop from collections.abc import ( AsyncIterator, Awaitable, @@ -25,6 +25,7 @@ ) from types import AsyncGeneratorType, CoroutineType from typing import ( + TYPE_CHECKING, Any, Literal, ParamSpec, @@ -61,6 +62,11 @@ else: from typing_extensions import TypeIs +if TYPE_CHECKING: + # AbstractEventLoopPolicy is deprecated and scheduled for removal in Python 3.16 + # Import it for type checking only to avoid raising a DeprecationWarning. + from asyncio import AbstractEventLoopPolicy + _ScopeName = Literal["session", "package", "module", "class", "function"] _R = TypeVar("_R", bound=Awaitable[Any] | AsyncIterator[Any]) _P = ParamSpec("_P")