diff --git a/changelog.d/+167df28d.downstream.rst b/changelog.d/+167df28d.downstream.rst new file mode 100644 index 00000000..c69e5c99 --- /dev/null +++ b/changelog.d/+167df28d.downstream.rst @@ -0,0 +1 @@ +Extend dependency on typing-extensions>=4.12 from Python<3.10 to Python<3.13. diff --git a/pyproject.toml b/pyproject.toml index d3c5b6bf..4f78afb9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ dynamic = [ dependencies = [ "backports-asyncio-runner>=1.1,<2; python_version<'3.11'", "pytest>=8.2,<9", - "typing-extensions>=4.12; python_version<'3.10'", + "typing-extensions>=4.12; python_version<'3.13'", ] optional-dependencies.docs = [ "sphinx>=5.3", diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 916b5c9b..38b3e28a 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -28,7 +28,6 @@ Literal, TypeVar, Union, - cast, overload, ) @@ -60,6 +59,11 @@ else: from backports.asyncio.runner import Runner +if sys.version_info >= (3, 13): + from typing import TypeIs +else: + from typing_extensions import TypeIs + _ScopeName = Literal["session", "package", "module", "class", "function"] _R = TypeVar("_R", bound=Union[Awaitable[Any], AsyncIterator[Any]]) _P = ParamSpec("_P") @@ -628,14 +632,9 @@ def _set_event_loop(loop: AbstractEventLoop | None) -> None: @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_pyfunc_call(pyfuncitem: Function) -> object | None: - """ - Pytest hook called before a test case is run. - - Wraps marked tests in a synchronous function - where the wrapped test coroutine is executed in an event loop. - """ + """Pytest hook called before a test case is run.""" if pyfuncitem.get_closest_marker("asyncio") is not None: - if isinstance(pyfuncitem, PytestAsyncioFunction): + if is_async_test(pyfuncitem): asyncio_mode = _get_asyncio_mode(pyfuncitem.config) for fixname, fixtures in pyfuncitem._fixtureinfo.name2fixturedefs.items(): # name2fixturedefs is a dict between fixture name and a list of matching @@ -698,7 +697,6 @@ def pytest_runtest_setup(item: pytest.Item) -> None: marker = item.get_closest_marker("asyncio") if marker is None or not is_async_test(item): return - item = cast(PytestAsyncioFunction, item) runner_fixture_id = f"_{item.loop_scope}_scoped_runner" fixturenames = item.fixturenames # type: ignore[attr-defined] if runner_fixture_id not in fixturenames: @@ -831,7 +829,7 @@ def event_loop_policy() -> AbstractEventLoopPolicy: return _get_event_loop_policy() -def is_async_test(item: Item) -> bool: +def is_async_test(item: Item) -> TypeIs[PytestAsyncioFunction]: """Returns whether a test item is a pytest-asyncio test""" return isinstance(item, PytestAsyncioFunction)