⬅ pytest_cases/pep492.py source

1 # Authors: Sylvain MARIE <sylvain.marie@se.com>
2 # + All contributors to <https://github.com/smarie/python-pytest-cases>
3 #
4 # License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
5  
6 # contains syntax illegal before PEP492 "Coroutines with async and await syntax"
7  
8 from makefun import wraps
9 from .fixture_core1_unions import is_used_request, NOT_USED
10  
11  
12 def _ignore_unused_coroutine_pep492(fixture_func, new_sig, func_needs_request):
13 @wraps(fixture_func, new_sig=new_sig)
14 async def wrapped_fixture_func(*args, **kwargs):
15 request = kwargs['request'] if func_needs_request else kwargs.pop('request')
16 if is_used_request(request):
17 return await fixture_func(*args, **kwargs)
18 else:
19 return NOT_USED
20  
21 return wrapped_fixture_func
22  
  • E302 Expected 2 blank lines, found 1
23 def _decorate_fixture_plus_coroutine_pep492(fixture_func, new_sig, map_arguments):
24 @wraps(fixture_func, new_sig=new_sig)
25 async def wrapped_fixture_func(*_args, **_kwargs):
26 if not is_used_request(_kwargs['request']):
27 return NOT_USED
28 else:
29 _args, _kwargs = map_arguments(*_args, **_kwargs)
30 return await fixture_func(*_args, **_kwargs)
31  
32 return wrapped_fixture_func
33  
34  
35 def _parametrize_plus_decorate_coroutine_pep492(
36 test_func,
37 new_sig,
38 fixture_union_name,
39 replace_paramfixture_with_values
40 ):
41 @wraps(test_func, new_sig=new_sig)
42 async def wrapped_test_func(*args, **kwargs): # noqa
43 if kwargs.get(fixture_union_name, None) is NOT_USED:
44 # TODO why this ? it is probably useless: this fixture
45 # is private and will never end up in another union
46 return NOT_USED
47 else:
48 replace_paramfixture_with_values(kwargs)
49 return await test_func(*args, **kwargs)
50  
51 return wrapped_test_func