GeezerVison is showing github.com — Bug in _pytest.compat.getfuncargnames() corrupts fixture handling for test methods w/ positional-only self parameter · Issue #13376 · pytest-dev/pytest
Bug in _pytest.compat.getfuncargnames() corrupts fixture handling for test methods w/ positional-only self parameter #13376
Description
- a detailed description of the bug or problem you are having
- Doesn't depend on specific packages [ ] output of
pip list from the virtual environment you are using
- Isn't related to specific Pytest versions [ ] pytest and operating system versions
- minimal example if possible
Basic example
Passing:
import pytest
@pytest.fixture
def value_fixture():
return 'value'
class TestClass:
def test_value(self, value_fixture):
assert value_fixture == 'value'
Failing:
import pytest
@pytest.fixture
def value_fixture():
return 'value'
class TestClass:
def test_value(self, /, value_fixture):
assert value_fixture == 'value'
pyfuncitem = <Function test_value>
@hookimpl(trylast=True)
def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
testfunction = pyfuncitem.obj
if is_async_function(testfunction):
async_fail(pyfuncitem.nodeid)
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
> result = testfunction(**testargs)
^^^^^^^^^^^^^^^^^^^^^^^^
E TypeError: TestClass.test_value() missing 1 required positional argument: 'value_fixture'
Reason
_pytest.compat.getfuncargnames() collects keyword-callable parameters as fixture-receiving arguments:
|
arg_names = tuple( |
|
p.name |
|
for p in parameters.values() |
|
if ( |
|
p.kind is Parameter.POSITIONAL_OR_KEYWORD |
|
or p.kind is Parameter.KEYWORD_ONLY |
|
) |
|
and p.default is Parameter.empty |
|
) |
Then it removes the first one – supposedly 'self' – if given function is a non-static method:
|
if ( |
|
# 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:] |
Yet, if 'self' is defined as positional only, it wasn't contained in arg_names before removal ...
Activity
added 12 commits that reference this issue
on Apr 15, 2025This issue will close once commit 1d10dfb is merged into the 'main' branch.1d10dfb This issue will close once commit edaf3ef is merged into the 'main' branch.edaf3ef This issue will close once commit c3423a8 is merged into the 'main' branch.c3423a8 This issue will close once commit 9edb5a6 is merged into the 'main' branch.9edb5a6 This issue will close once commit 9383685 is merged into the 'main' branch.9383685 This issue will close once commit 4bd2b0a is merged into the 'main' branch.4bd2b0a This issue will close once commit a9f881d is merged into the 'main' branch.a9f881d This issue will close once commit 4989399 is merged into the 'main' branch.4989399 This issue will close once commit c666e83 is merged into the 'main' branch.c666e83 This issue will close once commit a9e5dc7 is merged into the 'main' branch.a9e5dc7 This issue will close once commit f0df140 is merged into the 'main' branch.f0df140 This issue will close once commit da6a467 is merged into the 'main' branch.da6a467 added 2 commits that reference this issue
on Apr 19, 2025This issue will close once commit c9c9dc0 is merged into the 'main' branch.c9c9dc0 This issue will close once commit 7c34357 is merged into the 'main' branch.7c34357 added a commit that references this issue
on Apr 20, 2025This issue will close once commit 61c204a is merged into the 'main' branch.61c204a
Metadata
Metadata
Development
No branches or pull requestsIssue actions