9
9
import functools
10
10
import inspect
11
11
import socket
12
+ import sys
12
13
import warnings
13
14
from asyncio import AbstractEventLoop , AbstractEventLoopPolicy
14
15
from collections .abc import (
53
54
StashKey ,
54
55
)
55
56
57
+ if sys .version_info >= (3 , 10 ):
58
+ from typing import ParamSpec
59
+ else :
60
+ from typing_extensions import ParamSpec
61
+
62
+
56
63
_ScopeName = Literal ["session" , "package" , "module" , "class" , "function" ]
57
64
_T = TypeVar ("_T" )
58
-
59
- FixtureFunctionT = TypeVar (
60
- "FixtureFunctionT" ,
61
- bound = Union [Callable [..., Awaitable [Any ]], Callable [..., AsyncIterator [Any ]]],
62
- )
63
- FixtureFunctionU = TypeVar (
64
- "FixtureFunctionU" ,
65
- bound = Union [Callable [..., Awaitable [Any ]], Callable [..., AsyncIterator [Any ]]],
66
- )
67
- FixtureFunctionType = Union [
68
- Callable [..., Awaitable [Any ]], Callable [..., AsyncIterator [Any ]]
69
- ]
70
- FixtureFunctionMarker = Callable [[FixtureFunctionT ], FixtureFunctionT ]
65
+ _R = TypeVar ("_R" , bound = Union [Awaitable [Any ], AsyncIterator [Any ]])
66
+ _P = ParamSpec ("_P" )
67
+ FixtureFunction = Callable [_P , _R ]
71
68
72
69
73
70
class PytestAsyncioError (Exception ):
@@ -121,7 +118,7 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None
121
118
122
119
@overload
123
120
def fixture (
124
- fixture_function : FixtureFunctionT ,
121
+ fixture_function : FixtureFunction [ _P , _R ] ,
125
122
* ,
126
123
scope : _ScopeName | Callable [[str , Config ], _ScopeName ] = ...,
127
124
loop_scope : _ScopeName | None = ...,
@@ -133,7 +130,7 @@ def fixture(
133
130
| None
134
131
) = ...,
135
132
name : str | None = ...,
136
- ) -> FixtureFunctionT : ...
133
+ ) -> FixtureFunction [ _P , _R ] : ...
137
134
138
135
139
136
@overload
@@ -150,22 +147,25 @@ def fixture(
150
147
| None
151
148
) = ...,
152
149
name : str | None = None ,
153
- ) -> FixtureFunctionMarker [ FixtureFunctionU ]: ...
150
+ ) -> Callable [[ FixtureFunction [ _P , _R ]], FixtureFunction [ _P , _R ] ]: ...
154
151
155
152
156
153
def fixture (
157
- fixture_function : FixtureFunctionT | None = None ,
154
+ fixture_function : FixtureFunction [ _P , _R ] | None = None ,
158
155
loop_scope : _ScopeName | None = None ,
159
156
** kwargs : Any ,
160
- ) -> FixtureFunctionT | FixtureFunctionMarker [FixtureFunctionU ]:
157
+ ) -> (
158
+ FixtureFunction [_P , _R ]
159
+ | Callable [[FixtureFunction [_P , _R ]], FixtureFunction [_P , _R ]]
160
+ ):
161
161
if fixture_function is not None :
162
162
_make_asyncio_fixture_function (fixture_function , loop_scope )
163
163
return pytest .fixture (fixture_function , ** kwargs )
164
164
165
165
else :
166
166
167
167
@functools .wraps (fixture )
168
- def inner (fixture_function : FixtureFunctionU ) -> FixtureFunctionU :
168
+ def inner (fixture_function : FixtureFunction [ _P , _R ] ) -> FixtureFunction [ _P , _R ] :
169
169
return fixture (fixture_function , loop_scope = loop_scope , ** kwargs )
170
170
171
171
return inner
0 commit comments