Backtestspytestrun-20261106-234554#6750
compat::getfuncargnames removes self from instance method twice by mistake
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches make the same root-cause change: skip stripping the first argument name when `inspect.ismethod(function)` is true, since a bound method's signature has already dropped self/cls. Placement differs (A checks it first, B inside the `cls and ...` branch) but they are behaviourally equivalent, and the independent tests confirm both fix every issue scenario. The differences are in tests and craft. Patch A adds a direct unit test of `getfuncargnames` with bound instance methods, a bound classmethod, and an unbound function with `cls` given, which exercises exactly the claim in the issue (the function strips self twice), plus a pytester integration test that covers both the `callobj` and no-`callobj` paths through a custom collector. Patch B adds a single pytester integration test that covers more method kinds (positional-only receiver, keyword-only fixture, classmethod, staticmethod), which is good coverage, but it does not test `getfuncargnames` itself. Patch A also explains the new condition with an inline comment in the style of the surrounding code. Patch B's changelog and AUTHORS entries follow pytest's PR conventions and are a genuine plus, but they do not outweigh A's more targeted unit test and clearer code comment. Neither has unrelated churn. A is modestly better.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
5m 58s
Wall-clock time in the container
Full test suite
No new failures
942 passed · 10 skipped · 952 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
81vs 49 merged
Changed lines, ours vs merged
The issue
View on GitHub<!--
Thanks for submitting an issue!
Here's a quick checklist for what to provide:
-->
- a detailed description of the bug or suggestion I have a pyfuncitem which is an instance method and its signature looks like this: def run_test(self, testbed_obj): <== This testbed_obj is a fixture When getfuncargnames got called to return the argnames, it removed this testbed_obj in line 168 of compat.py, which I think is wrong, because the parameter got from line 146 of compat.py has self removed from the signature.
- output of
pip listfrom the virtual environment you are using Package Version Location
astroid 1.4.9
atomicwrites 1.3.0
attrs 19.3.0
automatos-tool-library 15
avro 1.9.1
backports.functools-lru-cache 1.6.1
backports.shutil-get-terminal-size 1.0.0
backports.ssl-match-hostname 3.7.0.1
bcrypt 3.1.7
certifi 2019.11.28
cffi 1.13.2
chardet 3.0.4
colorama 0.4.3
configparser 4.0.2
confluent-kafka 1.3.0
contextlib2 0.6.0.post1
coverage 5.0.3
cryptography 2.8
decorator 4.4.1
dicttoxml 1.7.4
docker-py 1.10.6
docker-pycreds 0.4.0
enum34 1.1.6
filelock 3.0.12
flake8 3.6.0
FormEncode 1.3.1
funcsigs 1.0.2
future 0.18.2
futures 3.3.0
gitdb2 2.0.6
GitPython 2.1.14
httpretty 0.9.7
idna 2.8
importlib-metadata 1.5.0
ipaddress 1.0.23
isort 4.3.21
lazy-object-proxy 1.4.3
logutils 0.3.5
mccabe 0.6.1
mock 3.0.5
more-itertools 5.0.0
ndg-httpsclient 0.5.1
packaging 20.1
paramiko 2.7.1
pathlib2 2.3.5
pip 20.0.2
pluggy 0.13.1
py 1.8.1
pyasn1 0.4.8
pybase64 0.5.0
pycodestyle 2.4.0
pycparser 2.19
pycyclone 0.9.277
pyflakes 2.0.0
pylint 1.6.4
PyNaCl 1.3.0
pyOpenSSL 19.1.0
pyparsing 2.4.6
pysftp 0.2.9
pytest 4.6.9
pytest-html 1.22.0
pytest-metadata 1.8.0
python-nmap 0.6.1
pytz 2019.3
pyvmomi 6.7.3
PyYAML 5.3
redis 2.10.6
requests 2.22.0
requests-mock 1.6.0
requests-toolbelt 0.9.1
retrying 1.3.3
scandir 1.10.0
setuptools 44.0.0
setuptools-scm 3.2.0
six 1.14.0
smmap2 2.0.5
spur 0.3.21
suds 0.4
toml 0.10.0
tox 3.14.3
urllib3 1.23
uuid 1.30
validators 0.11.3
virtualenv 16.7.9
waiting 1.4.1
walrus 0.3.5
wcwidth 0.1.8
websocket-client 0.57.0
wheel 0.34.2
wrapt 1.11.2
xmltodict 0.12.0
zipp 1.1.0
We have some private Python packages that I don't want to list here.
- pytest and operating system versions pytest 4.6.9 Windows 10
- minimal example if possible
class MyTest(BaseTestCase):
def run_test(self, testbed_obj):
The two patches
src/_pytest/compat.pyindex 0e060f15d..365c2ca1d 100644@@ -158,12 +158,19 @@ def getfuncargnames( # If this function should be treated as a bound method even though # it's passed as an unbound method or function, and its first parameter # wasn't defined as positional only, remove the first parameter name.- if not any(p.kind is Parameter.POSITIONAL_ONLY for p in parameters) and (- # Not using `getattr` because we don't want to resolve the staticmethod.- # Not using `cls.__dict__` because we want to check the entire MRO.- cls- and not isinstance(- inspect.getattr_static(cls, name, default=None), staticmethod+ # Already-bound methods are excluded: their signature has already+ # dropped the first parameter, so removing it again would lose a real+ # argument.+ if (+ not inspect.ismethod(function)+ and not any(p.kind is Parameter.POSITIONAL_ONLY for p in parameters)+ and (+ # Not using `getattr` because we don't want to resolve the staticmethod.+ # Not using `cls.__dict__` because we want to check the entire MRO.+ cls+ and not isinstance(+ inspect.getattr_static(cls, name, default=None), staticmethod+ ) ) ): arg_names = arg_names[1:]testing/python/collect.pyindex dddd15b8f..f108af698 100644@@ -364,6 +364,47 @@ class TestFunction: assert isinstance(modcol, pytest.Module) assert hasattr(modcol.obj, "test_func") + @pytest.mark.parametrize("pass_callobj", [False, True])+ def test_method_function_without_fixtureinfo_requests_all_fixtures(+ self, pytester: Pytester, pass_callobj: bool+ ) -> None:+ """A method Function created without fixtureinfo (e.g. by a plugin)+ must request all of its arguments as fixtures, not drop the first one+ because ``self`` was removed twice."""+ pytester.makeconftest(+ f"""+ import pytest++ class MyClass(pytest.Class):+ def collect(self):+ kwargs = {{}}+ if {pass_callobj}:+ kwargs["callobj"] = self.obj().run_test+ return [+ pytest.Function.from_parent(self, name="run_test", **kwargs)+ ]++ def pytest_pycollect_makeitem(collector, name, obj):+ if name == "MyTest":+ return MyClass.from_parent(collector, name=name)+ """+ )+ pytester.makepyfile(+ """+ import pytest++ @pytest.fixture+ def testbed_obj():+ return 42++ class MyTest:+ def run_test(self, testbed_obj):+ assert testbed_obj == 42+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=1)+ @pytest.mark.filterwarnings("default") def test_function_as_object_instance_ignored(self, pytester: Pytester) -> None: pytester.makepyfile(testing/python/fixtures.pyindex bc7b5a40c..c8f47dd49 100644@@ -94,6 +94,27 @@ def test_getfuncargnames_staticmethod_inherited() -> None: assert getfuncargnames(B.static, cls=B) == ("arg1", "arg2") +def test_getfuncargnames_bound_methods_with_cls() -> None:+ """Already-bound methods must not lose another argument when ``cls`` is given."""++ class A:+ def f(self, arg1, arg2="hello"):+ raise NotImplementedError()++ def g(self, arg1, arg2):+ raise NotImplementedError()++ @classmethod+ def c(cls, arg1, arg2):+ raise NotImplementedError()++ assert getfuncargnames(A().f, name="f", cls=A) == ("arg1",)+ assert getfuncargnames(A().g, name="g", cls=A) == ("arg1", "arg2")+ assert getfuncargnames(A.c, name="c", cls=A) == ("arg1", "arg2")+ # Unbound functions still have their first parameter removed.+ assert getfuncargnames(A.g, name="g", cls=A) == ("arg1", "arg2")++ @pytest.mark.skipif( sys.version_info >= (3, 13), reason="""\
AUTHORSindex 026f6aed2..3d74b238d 100644@@ -347,6 +347,7 @@ Mike Lundy Mike Ma Milan Lesnek minbang930+Minh Dau Miro Hrončok Mulat Mekonen mrbean-bremenchangelog/6750.bugfix.rstnew file mode 100644index 000000000..74c008da1@@ -0,0 +1 @@+Fix fixture argument discovery for custom collectors that pass bound methods to :class:`pytest.Function`.src/_pytest/compat.pyindex 0e060f15d..1389c6bb6 100644@@ -162,6 +162,7 @@ def getfuncargnames( # Not using `getattr` because we don't want to resolve the staticmethod. # Not using `cls.__dict__` because we want to check the entire MRO. cls+ and not inspect.ismethod(function) and not isinstance( inspect.getattr_static(cls, name, default=None), staticmethod )testing/python/fixtures.pyindex bc7b5a40c..cb2f1daab 100644@@ -69,6 +69,52 @@ def test_getfuncargnames_methods(): assert getfuncargnames(A().k) == ("arg1", "arg2") +def test_bound_method_fixtures_custom_collector(pytester: Pytester) -> None:+ """Custom collectors can supply already-bound methods (#6750)."""+ pytester.makeconftest(+ """+ import pytest++ def pytest_pycollect_makeitem(collector, name, obj):+ if isinstance(collector, pytest.Class) and name.startswith("test_"):+ return pytest.Function.from_parent(+ collector,+ name=name,+ callobj=getattr(collector.newinstance(), name),+ )+ """+ )+ pytester.makepyfile(+ """+ import pytest++ @pytest.fixture+ def value():+ return 42++ class TestBound:+ def test_instance(self, value):+ assert value == 42++ def test_positional_only_receiver(self, /, value):+ assert value == 42++ def test_keyword_only_fixture(self, *, value):+ assert value == 42++ @classmethod+ def test_classmethod(cls, value):+ assert value == 42++ @staticmethod+ def test_staticmethod(value):+ assert value == 42+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=5)++ def test_getfuncargnames_staticmethod(): """Test getfuncargnames for staticmethods"""