Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if len(ms) > 0:
final_values[i] = make_marked_parameter_value((final_values[i],), marks=ms)
if len(final_values) != len(final_ids):
raise ValueError("Internal error related to fixture parametrization- please report")
# (4) wrap the fixture function so as to remove the parameter names and add 'request' if needed
all_param_names = tuple(v for pnames in params_names_or_name_combinations for v in pnames)
# --create the new signature that we want to expose to pytest
old_sig = signature(fixture_func)
for p in all_param_names:
if p not in old_sig.parameters:
raise ValueError("parameter '%s' not found in fixture signature '%s%s'"
"" % (p, fixture_func.__name__, old_sig))
new_sig = remove_signature_parameters(old_sig, *all_param_names)
# add request if needed
func_needs_request = 'request' in old_sig.parameters
if not func_needs_request:
new_sig = add_signature_parameters(new_sig, first=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
# --common routine used below. Fills kwargs with the appropriate names and values from fixture_params
def _map_arguments(*_args, **_kwargs):
request = _kwargs['request'] if func_needs_request else _kwargs.pop('request')
# populate the parameters
if len(params_names_or_name_combinations) == 1:
_params = [request.param] # remove the simplification
else:
_params = request.param
for p_names, fixture_param_value in zip(params_names_or_name_combinations, _params):
if len(p_names) == 1:
# TO DO if fixtures_to_union has length 1, simplify ? >> No, we leave such "optimization" to the end user
# Finally create a "main" fixture with a unique name for this test function
# note: the function automatically registers it in the module
# note 2: idstyle is set to None because we provide an explicit enough list of ids
big_param_fixture = _fixture_union(caller_module, fixture_union_name, fixtures_to_union, idstyle=None,
ids=fixtures_to_union_names_for_ids, hook=hook)
# --create the new test function's signature that we want to expose to pytest
# it is the same than existing, except that we want to replace all parameters with the new fixture
# first check where we should insert the new parameters (where is the first param we remove)
for _first_idx, _n in enumerate(old_sig.parameters):
if _n in all_param_names:
break
# then remove all parameters that will be replaced by the new fixture
new_sig = remove_signature_parameters(old_sig, *all_param_names)
# finally insert the new fixture in that position. Indeed we can not insert first or last, because
# 'self' arg (case of test class methods) should stay first and exec order should be preserved when possible
new_sig = add_signature_parameters(new_sig, custom_idx=_first_idx,
custom=Parameter(fixture_union_name, kind=Parameter.POSITIONAL_OR_KEYWORD))
# --Finally create the fixture function, a wrapper of user-provided fixture with the new signature
def replace_paramfixture_with_values(kwargs):
# remove the created fixture value
encompassing_fixture = kwargs.pop(fixture_union_name)
# and add instead the parameter values
if nb_params > 1:
for i, p in enumerate(all_param_names):
kwargs[p] = encompassing_fixture[i]
else:
kwargs[all_param_names[0]] = encompassing_fixture
# return
print("Creating final union fixture %r with alternatives %r" % (fixture_union_name, fix_alternatives))
# note: the function automatically registers it in the module
_make_fixture_union(caller_module, name=fixture_union_name, hook=hook, caller=parametrize_plus,
fix_alternatives=fix_alternatives, unique_fix_alt_names=fix_alt_names,
ids=explicit_ids_to_use or ids or ParamIdMakers.get(idstyle))
# --create the new test function's signature that we want to expose to pytest
# it is the same than existing, except that we want to replace all parameters with the new fixture
# first check where we should insert the new parameters (where is the first param we remove)
_first_idx = -1
for _first_idx, _n in enumerate(old_sig.parameters):
if _n in argnames:
break
# then remove all parameters that will be replaced by the new fixture
new_sig = remove_signature_parameters(old_sig, *argnames)
# finally insert the new fixture in that position. Indeed we can not insert first or last, because
# 'self' arg (case of test class methods) should stay first and exec order should be preserved when possible
new_sig = add_signature_parameters(new_sig, custom_idx=_first_idx,
custom=Parameter(fixture_union_name,
kind=Parameter.POSITIONAL_OR_KEYWORD))
if debug:
print("Creating final test function wrapper with signature %s%s" % (test_func_name, new_sig))
# --Finally create the fixture function, a wrapper of user-provided fixture with the new signature
def replace_paramfixture_with_values(kwargs): # noqa
# remove the created fixture value
encompassing_fixture = kwargs.pop(fixture_union_name)
# and add instead the parameter values
if nb_params > 1:
for i, p in enumerate(argnames): # noqa
if len(ms) > 0:
final_values[i] = make_marked_parameter_value(final_values[i], marks=ms)
if len(final_values) != len(final_ids):
raise ValueError("Internal error related to fixture parametrization- please report")
# (4) wrap the fixture function so as to remove the parameter names and add 'request' if needed
all_param_names = tuple(v for l in params_names_or_name_combinations for v in l)
# --create the new signature that we want to expose to pytest
old_sig = signature(fixture_func)
for p in all_param_names:
if p not in old_sig.parameters:
raise ValueError("parameter '%s' not found in fixture signature '%s%s'"
"" % (p, fixture_func.__name__, old_sig))
new_sig = remove_signature_parameters(old_sig, *all_param_names)
# add request if needed
func_needs_request = 'request' in old_sig.parameters
if not func_needs_request:
new_sig = add_signature_parameters(new_sig, first=Parameter('request', kind=Parameter.POSITIONAL_OR_KEYWORD))
# --common routine used below. Fills kwargs with the appropriate names and values from fixture_params
def _get_arguments(*args, **kwargs):
request = kwargs['request'] if func_needs_request else kwargs.pop('request')
# populate the parameters
if len(params_names_or_name_combinations) == 1:
_params = [request.param] # remove the simplification
else:
_params = request.param
for p_names, fixture_param_value in zip(params_names_or_name_combinations, _params):
if len(p_names) == 1:
implementors_signature = signature(impl_function)
# determine the mode (nested, flat, double-flat) and check signature
mode, injected_name, contains_varpositional, injected_pos, \
injected_arg, f_args_name, f_kwargs_name = extract_mode_info(implementors_signature, flat_mode_decorated_name)
# create the signature of the decorator function to create, according to mode
if mode is None:
# *nested: keep the signature 'as is'
exposed_signature = implementors_signature
function_for_metadata = impl_function
nested_impl_function = impl_function
elif mode is DECORATED: # flat mode
# use the same signature, but remove the injected arg.
exposed_signature = remove_signature_parameters(implementors_signature, injected_name)
# use the original function for the docstring/module metadata
function_for_metadata = impl_function
# generate the corresponding nested decorator
nested_impl_function = make_nested_impl_for_flat_mode(exposed_signature, impl_function, injected_name,
injected_pos)
elif mode is WRAPPED:
# *double-flat: the same signature, but we remove the injected args.
args_to_remove = (injected_name,) + ((f_args_name,) if f_args_name is not None else ()) \
+ ((f_kwargs_name,) if f_kwargs_name is not None else ())
exposed_signature = remove_signature_parameters(implementors_signature, *args_to_remove)
# use the original function for the docstring/module metadata
function_for_metadata = impl_function