Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions changelog/13377.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Fixed handling of test methods with special parameter syntax.

Now, methods are supported that formally define ``self`` as positional-only
and/or fixture parameters as keyword-only, e.g.:

.. code-block:: python

class TestClass:

def test_method(self, /, *, fixture): ...

Before, this led to:

.. code-block:: python-console

pyfuncitem = <Function test_method>

@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_method() missing 1 required positional argument: 'fixture'
Comment thread
nicoddemus marked this conversation as resolved.
Outdated
10 changes: 5 additions & 5 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def getfuncargnames(
# creates a tuple of the names of the parameters that don't have
# defaults.
try:
parameters = signature(function).parameters
parameters = signature(function).parameters.values()
except (ValueError, TypeError) as e:
from _pytest.outcomes import fail

Expand All @@ -133,7 +133,7 @@ def getfuncargnames(

arg_names = tuple(
p.name
for p in parameters.values()
for p in parameters
if (
p.kind is Parameter.POSITIONAL_OR_KEYWORD
or p.kind is Parameter.KEYWORD_ONLY
Expand All @@ -144,9 +144,9 @@ def getfuncargnames(
name = function.__name__

# If this function should be treated as a bound method even though
# it's passed as an unbound method or function, remove the first
# parameter name.
if (
# 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
Expand Down
16 changes: 16 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,23 @@ class A:
def f(self, arg1, arg2="hello"):
raise NotImplementedError()

def g(self, /, arg1, arg2="hello"):
raise NotImplementedError()

def h(self, *, arg1, arg2="hello"):
raise NotImplementedError()

def j(self, arg1, *, arg2, arg3="hello"):
raise NotImplementedError()

def k(self, /, arg1, *, arg2, arg3="hello"):
raise NotImplementedError()

assert getfuncargnames(A().f) == ("arg1",)
assert getfuncargnames(A().g) == ("arg1",)
assert getfuncargnames(A().h) == ("arg1",)
assert getfuncargnames(A().j) == ("arg1", "arg2")
assert getfuncargnames(A().k) == ("arg1", "arg2")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we couldn't add example here?

        def l(self, /, *, arg1, arg2="hello"):
            raise NotImplementedError()

Is it redundant?


def test_getfuncargnames_staticmethod():
Expand Down