Skip to content

pytest should use the inspect.signature algorithm to determine fixtures to use #2267

@anntzer

Description

@anntzer

pytest 3.0.6 on python 3.6, Arch Linux (distro packages).

Currently, pytest follows the entire __wrapped__ chain and uses low-level code introspection to determine the signature of the test function, and thus the fixtures to use (https://github.com/pytest-dev/pytest/blob/master/_pytest/compat.py#L84). It would be preferable if it used the algorithm of inspect.signature (follow the __wrapped__ chain until either reaching an object with a __signature__ attribute (use the attribute in that case), or the bottom of the chain) instead.

This would allow things such as

import functools
import inspect
import pytest


def decorator(func):
    @functools.wraps(func)
    def tester(x):
        assert func()
    tester.__signature__ = inspect.signature(tester, follow_wrapped=False)
    print(inspect.signature(tester))  # prints "(x)"
    return tester


@pytest.mark.parametrize("x", [1])
@decorator
def test_1(*args):
    return args

(i.e., where the wrapper code calls the wrapped function but does the asserts on its return value) to work (currently, this fails at collection time with ValueError: <function test_1 at 0x7fbb804841e0> uses no argument 'x').

An example use case of this pattern is matplotlib's image comparison test suite, which looks like

@image_comparison([image_filenames])
def test_foo():
    # plotting commands

which runs some plotting commands and then compares the results to the image files specified by the image_comparison decorator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature-branchnew feature or API change, should be merged into features branchtype: refactoringinternal improvements to the code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions