Skip to content

Commit 402dbaa

Browse files
author
Sylvain MARIE
committed
Dependency to decorator has been dropped and replaced with makefun. Fixed #25
1 parent dc197b7 commit 402dbaa

File tree

5 files changed

+32
-299
lines changed

5 files changed

+32
-299
lines changed

ci_tools/requirements-conda.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pandoc
55
pypandoc
66

77
# --- to install
8-
decorator
98

109
# --- to run the tests
1110
# (pip) pytest$PYTEST_VERSION

ci_tools/requirements-pip.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pytest-runner
33

44
# -- to install
5+
makefun
56

67
# --- to generate the reports (see scripts in ci_tools, called by .travis)
78
pytest-html$PYTEST_HTML_VERSION

pytest_cases/decorator_hack.py

Lines changed: 0 additions & 275 deletions
This file was deleted.

pytest_cases/main.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
from distutils.version import LooseVersion
88
from inspect import getmembers, isgeneratorfunction, getmodule
99

10+
from makefun import with_signature, add_signature_parameters, remove_signature_parameters
11+
12+
try: # python 3.3+
13+
from inspect import signature, Parameter
14+
except ImportError:
15+
from funcsigs import signature, Parameter
16+
1017
from pytest_cases.common import yield_fixture, get_pytest_parametrize_marks
11-
from pytest_cases.decorator_hack import my_decorate
1218

1319
try: # type hints, python 3+
1420
from typing import Callable, Union, Optional, Any, Tuple, List, Dict, Iterable
@@ -379,50 +385,52 @@ def _param_fixture(request):
379385
old_parameter_names = tuple(v for l in params_map.values() for v in l)
380386

381387
# common routine used below. Fills kwargs with the appropriate names and values from fixture_params
382-
def _get_arguments(fixture_params, args_and_kwargs):
383-
# unpack the underlying function's args/kwargs
384-
args = args_and_kwargs.pop('args')
385-
kwargs = args_and_kwargs.pop('kwargs')
386-
if len(args_and_kwargs) > 0:
387-
raise ValueError("Internal error - please file an issue in the github project page")
388-
389-
# fill the kwargs with additional arguments by using mapping
388+
def _get_arguments(*args, **kwargs):
389+
# kwargs contains the generated fixture names so we have to remove them.
390+
# For each generated fixture, there are one or several parameters to inject in kwargs
390391
i = 0
391392
for new_p_name in new_parameter_names:
393+
fixture_param_value = kwargs.pop(new_p_name)
392394
if len(params_map[new_p_name]) == 1:
393-
kwargs[params_map[new_p_name][0]] = fixture_params[i]
395+
# a single parameter for that generated ficture (@pytest.mark.parametrize with a single name)
396+
kwargs[params_map[new_p_name][0]] = fixture_param_value
394397
i += 1
395398
else:
396-
# unpack several
397-
for old_p_name, old_p_value in zip(params_map[new_p_name], fixture_params[i]):
399+
# several parameters for that generated fixture (@pytest.mark.parametrize with several names)
400+
# unpack all of them and inject them in the kwargs
401+
for old_p_name, old_p_value in zip(params_map[new_p_name], fixture_param_value):
398402
kwargs[old_p_name] = old_p_value
399403
i += 1
400404

401405
return args, kwargs
402406

407+
# create the new signature that we want to expose to pytest
408+
old_sig = signature(fixture_func)
409+
new_sig = remove_signature_parameters(old_sig, *old_parameter_names)
410+
# add them in reversed order so as to match the same test order than in pytest.
411+
new_sig = add_signature_parameters(new_sig, first=(Parameter(n, kind=Parameter.POSITIONAL_OR_KEYWORD)
412+
for n in reversed(new_parameter_names)))
413+
414+
# Finally create the fixture function, a wrapper of user-provided fixture with the new signature
403415
if not isgeneratorfunction(fixture_func):
404416
# normal function with return statement
405-
def wrapper(f, *fixture_params, **args_and_kwargs):
406-
args, kwargs = _get_arguments(fixture_params, args_and_kwargs)
417+
@with_signature(new_sig)
418+
def wrapped_fixture_func(*args, **kwargs):
419+
args, kwargs = _get_arguments(*args, **kwargs)
407420
return fixture_func(*args, **kwargs)
408421

409-
wrapped_fixture_func = my_decorate(fixture_func, wrapper,
410-
additional_args=new_parameter_names, removed_args=old_parameter_names)
411-
412422
# transform the created wrapper into a fixture
413423
fixture_decorator = pytest.fixture(scope=scope, params=params, autouse=autouse, ids=ids, **kwargs)
414424
return fixture_decorator(wrapped_fixture_func)
415425

416426
else:
417427
# generator function (with a yield statement)
418-
def wrapper(f, *fixture_params, **args_and_kwargs):
419-
args, kwargs = _get_arguments(fixture_params, args_and_kwargs)
428+
@with_signature(new_sig)
429+
def wrapped_fixture_func(*args, **kwargs):
430+
args, kwargs = _get_arguments(*args, **kwargs)
420431
for res in fixture_func(*args, **kwargs):
421432
yield res
422433

423-
wrapped_fixture_func = my_decorate(fixture_func, wrapper,
424-
additional_args=new_parameter_names, removed_args=old_parameter_names)
425-
426434
# transform the created wrapper into a fixture
427435
fixture_decorator = yield_fixture(scope=scope, params=params, autouse=autouse, ids=ids, **kwargs)
428436
return fixture_decorator(wrapped_fixture_func)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
here = path.abspath(path.dirname(__file__))
1212

1313
# *************** Dependencies *********
14-
INSTALL_REQUIRES = ['decorator', 'functools32;python_version<"3.2"', 'funcsigs;python_version<"3.3"']
14+
INSTALL_REQUIRES = ['makefun', 'functools32;python_version<"3.2"', 'funcsigs;python_version<"3.3"']
1515
DEPENDENCY_LINKS = []
1616
SETUP_REQUIRES = ['pytest-runner', 'setuptools_scm', 'pypandoc', 'pandoc']
1717
TESTS_REQUIRE = ['pytest', 'pytest-logging', 'pytest-cov', 'pytest-steps']

0 commit comments

Comments
 (0)