Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error using parametrize with default arguments #3221

Closed
ndevenish opened this issue Feb 14, 2018 · 5 comments
Closed

Error using parametrize with default arguments #3221

ndevenish opened this issue Feb 14, 2018 · 5 comments
Labels
status: help wanted developers would like help from experts on this topic topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed type: question general question, might be closed after 2 weeks of inactivity

Comments

@ndevenish
Copy link
Contributor

With pytest 3.4.0:

import pytest

@pytest.mark.parametrize("something", [True, False])
def test_something(something=False):
  pass

You get the error:

../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:200: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:378: in _genfunctions
    self.ihook.pytest_generate_tests(metafunc=metafunc)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:617: in __call__
    return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:222: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/pluggy/__init__.py:216: in <lambda>
    firstresult=hook.spec_opts.get('firstresult'),
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:125: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs)
../../base/Python.framework/Versions/2.7/lib/python2.7/site-packages/_pytest/python.py:805: in parametrize
    self.function, name, arg))
E   ValueError: <function test_something at 0x10e4969b0> uses no argument 'something'

It seems pytest is separating the concept of normal python arguments and 'fixtures' here, only classifying only default-free arguments as potential fixtures - and then only considering 'fixtures' as valid targets for parametrisation - but the error message completely obfuscates this: by any regular python interpretation the function definitely has an argument 'something'.

The documentation on this - https://docs.pytest.org/en/latest/parametrize.html doesn't show this as an example but also doesn't mention that it's a constraint; it's only through reading the pytest source that I could work out what the problem was.

Related: The wording of this was changed to 'argument' from 'fixture' in #1539 which makes sense in the context of that error, but only confuses this one.

@pytestbot pytestbot added the topic: parametrize related to @pytest.mark.parametrize label Feb 14, 2018
@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Feb 15, 2018
@RonnyPfannschmidt
Copy link
Member

pytest doesnt consider arguments with default values as targets for fixtures/parameterization

@ndevenish
Copy link
Contributor Author

Yes, that is the root cause. But this isn't a question; my point is that the error is completely confusing and the solution non-obvious unless you read the source code:

pytest doesn't consider arguments with default values

and then complains that there is no argument.

So I'd consider it either a bug with error messages, argument-as-potential-fixture classification (although I assume this was rigorously discussed in the past and is like this for a reason), or a documentation problem.

@RonnyPfannschmidt
Copy link
Member

thanks for expressing what you need for better understanding of the issue
i consider it an issue with the error message as well

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed status: help wanted developers would like help from experts on this topic labels Feb 15, 2018
@feuillemorte
Copy link
Contributor

As I can see pytest ignores arguments with default values:

pytest/_pytest/compat.py

Lines 112 to 115 in e7bcc85

arg_names = tuple(p.name for p in signature(function).parameters.values()
if (p.kind is Parameter.POSITIONAL_OR_KEYWORD or
p.kind is Parameter.KEYWORD_ONLY) and
p.default is Parameter.empty)

Do we need to raise exception here? In this case all arguments with default value will catch this error

@RonnyPfannschmidt
Copy link
Member

that function in particular is working exactly as documented, so just raising an error there is not a good way to sort it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted developers would like help from experts on this topic topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants