Skip to content

Commit

Permalink
Merge pull request #1009 from mdboom/deprecation-warnings
Browse files Browse the repository at this point in the history
avoid deprecation warnings, introduce helpers.
  • Loading branch information
hpk42 committed Sep 17, 2015
2 parents 6520cf0 + 7f71ce0 commit 3bcda48
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
19 changes: 16 additions & 3 deletions _pytest/python.py
Expand Up @@ -32,6 +32,19 @@
# The type of re.compile objects is not exposed in Python.
REGEX_TYPE = type(re.compile(''))


if hasattr(inspect, 'signature'):
def _format_args(func):
return str(inspect.signature(func))
else:
def _format_args(func):
return inspect.formatargspec(*inspect.getargspec(func))


def _has_positional_arg(func):
return func.__code__.co_argcount


def filter_traceback(entry):
return entry.path != cutdir1 and not entry.path.relto(cutdir2)

Expand Down Expand Up @@ -593,7 +606,7 @@ def setup(self):
#XXX: nose compat hack, move to nose plugin
# if it takes a positional arg, its probably a pytest style one
# so we pass the current module object
if inspect.getargspec(setup_module)[0]:
if _has_positional_arg(setup_module):
setup_module(self.obj)
else:
setup_module()
Expand All @@ -604,7 +617,7 @@ def setup(self):
#XXX: nose compat hack, move to nose plugin
# if it takes a positional arg, it's probably a pytest style one
# so we pass the current module object
if inspect.getargspec(fin)[0]:
if _has_positional_arg(fin):
finalizer = lambda: fin(self.obj)
else:
finalizer = fin
Expand Down Expand Up @@ -1587,7 +1600,7 @@ def _factorytraceback(self):
factory = fixturedef.func
fs, lineno = getfslineno(factory)
p = self._pyfuncitem.session.fspath.bestrelpath(fs)
args = inspect.formatargspec(*inspect.getargspec(factory))
args = _format_args(factory)
lines.append("%s:%d: def %s%s" %(
p, lineno, factory.__name__, args))
return lines
Expand Down
14 changes: 14 additions & 0 deletions testing/python/metafunc.py
Expand Up @@ -473,6 +473,20 @@ def test_3(self, arg, arg2):
*6 passed*
""")

def test_format_args(self):
def function1(): pass
assert funcargs._format_args(function1) == '()'

def function2(arg1): pass
assert funcargs._format_args(function2) == "(arg1)"

def function3(arg1, arg2="qwe"): pass
assert funcargs._format_args(function3) == "(arg1, arg2='qwe')"

def function4(arg1, *args, **kwargs): pass
assert funcargs._format_args(function4) == "(arg1, *args, **kwargs)"


class TestMetafuncFunctional:
def test_attributes(self, testdir):
p = testdir.makepyfile("""
Expand Down
5 changes: 4 additions & 1 deletion testing/test_runner.py
Expand Up @@ -349,7 +349,10 @@ def test_func():

@pytest.mark.parametrize('reporttype', reporttypes, ids=[x.__name__ for x in reporttypes])
def test_report_extra_parameters(reporttype):
args = py.std.inspect.getargspec(reporttype.__init__)[0][1:]
if hasattr(py.std.inspect, 'signature'):
args = list(py.std.inspect.signature(reporttype.__init__).parameters.keys())[1:]
else:
args = py.std.inspect.getargspec(reporttype.__init__)[0][1:]
basekw = dict.fromkeys(args, [])
report = reporttype(newthing=1, **basekw)
assert report.newthing == 1
Expand Down

0 comments on commit 3bcda48

Please sign in to comment.