Skip to content

Commit 6317aa0

Browse files
committed
added indicative error when parametrizing an argument with a default value
1 parent 2962c73 commit 6317aa0

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

_pytest/compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ def getfuncargnames(function, is_method=False, cls=None):
127127
return arg_names
128128

129129

130+
def getdefaultargnames(function):
131+
# Note: this code intentionally mirrors the code at the beginning of getfuncargnames,
132+
# to get the arguments which were excluded from its result because they had default values
133+
return tuple(p.name for p in signature(function).parameters.values()
134+
if (p.kind is Parameter.POSITIONAL_OR_KEYWORD or
135+
p.kind is Parameter.KEYWORD_ONLY) and
136+
p.default is not Parameter.empty)
137+
138+
130139
if _PY3:
131140
STRING_TYPES = bytes, str
132141
UNICODE_TYPES = str,

_pytest/python.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
isclass, isfunction, is_generator, ascii_escaped,
2626
REGEX_TYPE, STRING_TYPES, NoneType, NOTSET,
2727
get_real_func, getfslineno, safe_getattr,
28-
safe_str, getlocation, enum,
28+
safe_str, getlocation, enum, getdefaultargnames
2929
)
3030
from _pytest.outcomes import fail
3131
from _pytest.mark.structures import transfer_markers
@@ -797,13 +797,16 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
797797
valtypes = {}
798798
for arg in argnames:
799799
if arg not in self.fixturenames:
800-
if isinstance(indirect, (tuple, list)):
801-
name = 'fixture' if arg in indirect else 'argument'
800+
if arg in getdefaultargnames(self.function):
801+
raise ValueError("%r already takes an argument %r with a default value" % (self.function, arg))
802802
else:
803-
name = 'fixture' if indirect else 'argument'
804-
raise ValueError(
805-
"%r uses no %s %r" % (
806-
self.function, name, arg))
803+
if isinstance(indirect, (tuple, list)):
804+
name = 'fixture' if arg in indirect else 'argument'
805+
else:
806+
name = 'fixture' if indirect else 'argument'
807+
raise ValueError(
808+
"%r uses no %s %r" % (
809+
self.function, name, arg))
807810

808811
if indirect is True:
809812
valtypes = dict.fromkeys(argnames, "params")

changelog/3221.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a more indicative error message when parametrizing a function whose argument takes a default value.

testing/python/metafunc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,19 @@ def test_simple(x):
616616
"*uses no argument 'y'*",
617617
])
618618

619+
def test_parametrize_gives_indicative_error_on_function_with_default_argument(self, testdir):
620+
testdir.makepyfile("""
621+
import pytest
622+
623+
@pytest.mark.parametrize('x, y', [('a', 'b')])
624+
def test_simple(x, y=1):
625+
assert len(x) == 1
626+
""")
627+
result = testdir.runpytest("--collect-only")
628+
result.stdout.fnmatch_lines([
629+
"*already takes an argument 'y' with a default value",
630+
])
631+
619632
def test_addcalls_and_parametrize_indirect(self):
620633
def func(x, y):
621634
pass

0 commit comments

Comments
 (0)