Merge pull request #164 from The-Compiler/argspec

Use inspect.signature() if it's available.
This commit is contained in:
Oleg Pidsadnyi 2015-12-16 13:39:47 +01:00
commit a8a7f17006
3 changed files with 29 additions and 4 deletions

View File

@ -36,6 +36,7 @@ from .steps import (
recreate_function,
)
from .types import GIVEN
from .utils import get_args
if six.PY3: # pragma: no cover
import runpy
@ -125,7 +126,7 @@ def _execute_step_function(request, scenario, step, step_func):
kw["step_func_args"] = {}
try:
# Get the step argument values.
kwargs = dict((arg, request.getfuncargvalue(arg)) for arg in inspect.getargspec(step_func).args)
kwargs = dict((arg, request.getfuncargvalue(arg)) for arg in get_args(step_func))
kw["step_func_args"] = kwargs
request.config.hook.pytest_bdd_before_step_call(**kw)
@ -204,7 +205,7 @@ def get_fixture(caller_module, fixture, path=None):
"""Get first conftest module from given one."""
def call_fixture(function):
args = []
if "request" in inspect.getargspec(function).args:
if "request" in get_args(function):
args = [FakeRequest(module=caller_module)]
return function(*args)
@ -240,7 +241,7 @@ def _get_scenario_decorator(feature, feature_name, scenario, scenario_name, call
g.update(locals())
args = inspect.getargspec(_pytestbdd_function).args
args = get_args(_pytestbdd_function)
function_args = list(args)
for arg in scenario.get_example_params():
if arg not in function_args:

View File

@ -46,6 +46,7 @@ from .exceptions import (
StepError,
)
from .parsers import get_parser
from .utils import get_args
def get_step_fixture_name(name, type_, encoding=None):
@ -215,7 +216,7 @@ def recreate_function(func, module=None, name=None, add_args=[], firstlineno=Non
if six.PY3:
argnames.insert(1, "co_kwonlyargcount")
for arg in inspect.getargspec(func).args:
for arg in get_args(func):
if arg in add_args:
add_args.remove(arg)

23
pytest_bdd/utils.py Normal file
View File

@ -0,0 +1,23 @@
"""Various utility functions."""
import inspect
def get_args(func):
"""Get a list of argument names for a function.
This is a wrapper around inspect.getargspec/inspect.signature because
getargspec got deprecated in Python 3.5 and signature isn't available on
Python 2.
:param func: The function to inspect.
:return: A list of argument names.
:rtype: list
"""
if hasattr(inspect, 'signature'):
params = inspect.signature(func).parameters.values()
return [param.name for param in params
if param.kind == param.POSITIONAL_OR_KEYWORD]
else:
return inspect.getargspec(func).args