Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FixtureFunction TypeAlias and TypeVars #1045

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changelog
0.26.0 (UNRELEASED)
===================
- Adds configuration option that sets default event loop scope for all testss `#793 <https://github.com/pytest-dev/pytest-asyncio/issues/793>`_
- Improved type annotations for ``pytest_asyncio.fixture`` `#1045 <https://github.com/pytest-dev/pytest-asyncio/pull/1045>`_
- Added ``typing-extensions`` as additional dependency for Python ``<3.10`` `#1045 <https://github.com/pytest-dev/pytest-asyncio/pull/1045>`_


0.25.2 (2025-01-08)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dynamic = [

dependencies = [
"pytest>=8.2,<9",
"typing-extensions>=4.12; python_version<'3.10'",
]
optional-dependencies.docs = [
"sphinx>=5.3",
Expand Down
36 changes: 20 additions & 16 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import functools
import inspect
import socket
import sys
import warnings
from asyncio import AbstractEventLoop, AbstractEventLoopPolicy
from collections.abc import (
Expand Down Expand Up @@ -53,17 +54,17 @@
StashKey,
)

if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
from typing_extensions import ParamSpec


_ScopeName = Literal["session", "package", "module", "class", "function"]
_T = TypeVar("_T")

SimpleFixtureFunction = TypeVar(
"SimpleFixtureFunction", bound=Callable[..., Awaitable[object]]
)
FactoryFixtureFunction = TypeVar(
"FactoryFixtureFunction", bound=Callable[..., AsyncIterator[object]]
)
FixtureFunction = Union[SimpleFixtureFunction, FactoryFixtureFunction]
FixtureFunctionMarker = Callable[[FixtureFunction], FixtureFunction]
_R = TypeVar("_R", bound=Union[Awaitable[Any], AsyncIterator[Any]])
_P = ParamSpec("_P")
FixtureFunction = Callable[_P, _R]


class PytestAsyncioError(Exception):
Expand Down Expand Up @@ -117,7 +118,7 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None

@overload
def fixture(
fixture_function: FixtureFunction,
fixture_function: FixtureFunction[_P, _R],
*,
scope: _ScopeName | Callable[[str, Config], _ScopeName] = ...,
loop_scope: _ScopeName | None = ...,
Expand All @@ -129,7 +130,7 @@ def fixture(
| None
) = ...,
name: str | None = ...,
) -> FixtureFunction: ...
) -> FixtureFunction[_P, _R]: ...


@overload
Expand All @@ -146,22 +147,25 @@ def fixture(
| None
) = ...,
name: str | None = None,
) -> FixtureFunctionMarker: ...
) -> Callable[[FixtureFunction[_P, _R]], FixtureFunction[_P, _R]]: ...


def fixture(
fixture_function: FixtureFunction | None = None,
fixture_function: FixtureFunction[_P, _R] | None = None,
loop_scope: _ScopeName | None = None,
**kwargs: Any,
) -> FixtureFunction | FixtureFunctionMarker:
) -> (
FixtureFunction[_P, _R]
| Callable[[FixtureFunction[_P, _R]], FixtureFunction[_P, _R]]
):
if fixture_function is not None:
_make_asyncio_fixture_function(fixture_function, loop_scope)
return pytest.fixture(fixture_function, **kwargs)

else:

@functools.wraps(fixture)
def inner(fixture_function: FixtureFunction) -> FixtureFunction:
def inner(fixture_function: FixtureFunction[_P, _R]) -> FixtureFunction[_P, _R]:
return fixture(fixture_function, loop_scope=loop_scope, **kwargs)

return inner
Expand Down Expand Up @@ -679,7 +683,7 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(

# A stack used to push package-scoped loops during collection of a package
# and pop those loops during collection of a Module
__package_loop_stack: list[FixtureFunctionMarker | FixtureFunction] = []
__package_loop_stack: list[Callable[..., Any]] = []


@pytest.hookimpl
Expand Down
Loading