11"""pytest-asyncio implementation."""
22
3+ from __future__ import annotations
4+
35import asyncio
46import contextlib
57import enum
2224 Any ,
2325 Callable ,
2426 Literal ,
25- Optional ,
2627 TypeVar ,
2728 Union ,
2829 overload ,
@@ -110,41 +111,41 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None
110111def fixture (
111112 fixture_function : FixtureFunction ,
112113 * ,
113- scope : "Union[ _ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
114- loop_scope : Union [ _ScopeName , None ] = ...,
115- params : Optional [ Iterable [object ]] = ...,
114+ scope : _ScopeName | Callable [[str , Config ], _ScopeName ] = ...,
115+ loop_scope : _ScopeName | None = ...,
116+ params : Iterable [object ] | None = ...,
116117 autouse : bool = ...,
117- ids : Union [
118- Iterable [Union [ str , float , int , bool , None ]],
119- Callable [[Any ], Optional [ object ]],
120- None ,
121- ] = ...,
122- name : Optional [ str ] = ...,
118+ ids : (
119+ Iterable [str | float | int | bool | None ]
120+ | Callable [[Any ], object | None ]
121+ | None
122+ ) = ...,
123+ name : str | None = ...,
123124) -> FixtureFunction : ...
124125
125126
126127@overload
127128def fixture (
128129 fixture_function : None = ...,
129130 * ,
130- scope : "Union[ _ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
131- loop_scope : Union [ _ScopeName , None ] = ...,
132- params : Optional [ Iterable [object ]] = ...,
131+ scope : _ScopeName | Callable [[str , Config ], _ScopeName ] = ...,
132+ loop_scope : _ScopeName | None = ...,
133+ params : Iterable [object ] | None = ...,
133134 autouse : bool = ...,
134- ids : Union [
135- Iterable [Union [ str , float , int , bool , None ]],
136- Callable [[Any ], Optional [ object ]],
137- None ,
138- ] = ...,
139- name : Optional [ str ] = None ,
135+ ids : (
136+ Iterable [str | float | int | bool | None ]
137+ | Callable [[Any ], object | None ]
138+ | None
139+ ) = ...,
140+ name : str | None = None ,
140141) -> FixtureFunctionMarker : ...
141142
142143
143144def fixture (
144- fixture_function : Optional [ FixtureFunction ] = None ,
145- loop_scope : Union [ _ScopeName , None ] = None ,
145+ fixture_function : FixtureFunction | None = None ,
146+ loop_scope : _ScopeName | None = None ,
146147 ** kwargs : Any ,
147- ) -> Union [ FixtureFunction , FixtureFunctionMarker ] :
148+ ) -> FixtureFunction | FixtureFunctionMarker :
148149 if fixture_function is not None :
149150 _make_asyncio_fixture_function (fixture_function , loop_scope )
150151 return pytest .fixture (fixture_function , ** kwargs )
@@ -163,9 +164,7 @@ def _is_asyncio_fixture_function(obj: Any) -> bool:
163164 return getattr (obj , "_force_asyncio_fixture" , False )
164165
165166
166- def _make_asyncio_fixture_function (
167- obj : Any , loop_scope : Union [_ScopeName , None ]
168- ) -> None :
167+ def _make_asyncio_fixture_function (obj : Any , loop_scope : _ScopeName | None ) -> None :
169168 if hasattr (obj , "__func__" ):
170169 # instance method, check the function object
171170 obj = obj .__func__
@@ -288,7 +287,7 @@ def _add_kwargs(
288287 return ret
289288
290289
291- def _perhaps_rebind_fixture_func (func : _T , instance : Optional [ Any ] ) -> _T :
290+ def _perhaps_rebind_fixture_func (func : _T , instance : Any | None ) -> _T :
292291 if instance is not None :
293292 # The fixture needs to be bound to the actual request.instance
294293 # so it is bound to the same object as the test method.
@@ -388,9 +387,7 @@ class PytestAsyncioFunction(Function):
388387 """Base class for all test functions managed by pytest-asyncio."""
389388
390389 @classmethod
391- def item_subclass_for (
392- cls , item : Function , /
393- ) -> Union [type ["PytestAsyncioFunction" ], None ]:
390+ def item_subclass_for (cls , item : Function , / ) -> type [PytestAsyncioFunction ] | None :
394391 """
395392 Returns a subclass of PytestAsyncioFunction if there is a specialized subclass
396393 for the specified function item.
@@ -525,10 +522,8 @@ def runtest(self) -> None:
525522# see https://github.com/pytest-dev/pytest/issues/11307
526523@pytest .hookimpl (specname = "pytest_pycollect_makeitem" , tryfirst = True )
527524def pytest_pycollect_makeitem_preprocess_async_fixtures (
528- collector : Union [pytest .Module , pytest .Class ], name : str , obj : object
529- ) -> Union [
530- pytest .Item , pytest .Collector , list [Union [pytest .Item , pytest .Collector ]], None
531- ]:
525+ collector : pytest .Module | pytest .Class , name : str , obj : object
526+ ) -> pytest .Item | pytest .Collector | list [pytest .Item | pytest .Collector ] | None :
532527 """A pytest hook to collect asyncio coroutines."""
533528 if not collector .funcnamefilter (name ):
534529 return None
@@ -540,20 +535,17 @@ def pytest_pycollect_makeitem_preprocess_async_fixtures(
540535# see https://github.com/pytest-dev/pytest/issues/11307
541536@pytest .hookimpl (specname = "pytest_pycollect_makeitem" , hookwrapper = True )
542537def pytest_pycollect_makeitem_convert_async_functions_to_subclass (
543- collector : Union [ pytest .Module , pytest .Class ] , name : str , obj : object
538+ collector : pytest .Module | pytest .Class , name : str , obj : object
544539) -> Generator [None , pluggy .Result , None ]:
545540 """
546541 Converts coroutines and async generators collected as pytest.Functions
547542 to AsyncFunction items.
548543 """
549544 hook_result = yield
550545 try :
551- node_or_list_of_nodes : Union [
552- pytest .Item ,
553- pytest .Collector ,
554- list [Union [pytest .Item , pytest .Collector ]],
555- None ,
556- ] = hook_result .get_result ()
546+ node_or_list_of_nodes : (
547+ pytest .Item | pytest .Collector | list [pytest .Item | pytest .Collector ] | None
548+ ) = hook_result .get_result ()
557549 except BaseException as e :
558550 hook_result .force_exception (e )
559551 return
@@ -592,7 +584,7 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
592584
593585# A stack used to push package-scoped loops during collection of a package
594586# and pop those loops during collection of a Module
595- __package_loop_stack : list [Union [ FixtureFunctionMarker , FixtureFunction ] ] = []
587+ __package_loop_stack : list [FixtureFunctionMarker | FixtureFunction ] = []
596588
597589
598590@pytest .hookimpl
@@ -868,7 +860,7 @@ def _provide_clean_event_loop() -> None:
868860
869861
870862def _get_event_loop_no_warn (
871- policy : Optional [ AbstractEventLoopPolicy ] = None ,
863+ policy : AbstractEventLoopPolicy | None = None ,
872864) -> asyncio .AbstractEventLoop :
873865 with warnings .catch_warnings ():
874866 warnings .simplefilter ("ignore" , DeprecationWarning )
@@ -879,7 +871,7 @@ def _get_event_loop_no_warn(
879871
880872
881873@pytest .hookimpl (tryfirst = True , hookwrapper = True )
882- def pytest_pyfunc_call (pyfuncitem : Function ) -> Optional [ object ] :
874+ def pytest_pyfunc_call (pyfuncitem : Function ) -> object | None :
883875 """
884876 Pytest hook called before a test case is run.
885877
@@ -999,7 +991,7 @@ def _get_marked_loop_scope(asyncio_marker: Mark) -> _ScopeName:
999991 return scope
1000992
1001993
1002- def _retrieve_scope_root (item : Union [ Collector , Item ] , scope : str ) -> Collector :
994+ def _retrieve_scope_root (item : Collector | Item , scope : str ) -> Collector :
1003995 node_type_by_scope = {
1004996 "class" : Class ,
1005997 "module" : Module ,
0 commit comments