GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

New issue
Copy link
New issue
Copy link
Closed
#13377
Closed
Bug in _pytest.compat.getfuncargnames() corrupts fixture handling for test methods w/ positional-only self parameter#13376
#13377
Copy link

Description

@purplezimmermann
purplezimmermann
opened on Apr 15, 2025
Issue body actions
  • 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'
  • Result:
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:

pytest/src/_pytest/compat.py

Lines 134 to 142 in 103b2b6

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:

pytest/src/_pytest/compat.py

Lines 149 to 157 in 103b2b6

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

  1. purplezimmermann
    added 12 commits that reference this issue on Apr 15, 2025

    Fix parameter handling of non-static test methods

    This issue will close once commit 1d10dfb is merged into the 'main' branch.
    1d10dfb

    Fix parameter handling of non-static test methods

    This issue will close once commit edaf3ef is merged into the 'main' branch.
    edaf3ef

    Fix parameter handling of non-static test methods

    This issue will close once commit c3423a8 is merged into the 'main' branch.
    c3423a8

    Fix parameter handling of non-static test methods

    This issue will close once commit 9edb5a6 is merged into the 'main' branch.
    9edb5a6

    Fix parameter handling of non-static test methods

    This issue will close once commit 9383685 is merged into the 'main' branch.
    9383685

    Fix parameter handling of non-static test methods

    This issue will close once commit 4bd2b0a is merged into the 'main' branch.
    4bd2b0a

    Fix parameter handling of non-static test methods

    This issue will close once commit a9f881d is merged into the 'main' branch.
    a9f881d

    Fix parameter handling of non-static test methods

    This issue will close once commit 4989399 is merged into the 'main' branch.
    4989399

    Fix parameter handling of non-static test methods

    This issue will close once commit c666e83 is merged into the 'main' branch.
    c666e83

    Fix parameter handling of non-static test methods

    This issue will close once commit a9e5dc7 is merged into the 'main' branch.
    a9e5dc7

    Fix parameter handling of non-static test methods

    This issue will close once commit f0df140 is merged into the 'main' branch.
    f0df140

    Fix parameter handling of non-static test methods

    This issue will close once commit da6a467 is merged into the 'main' branch.
    da6a467
  2. nicoddemus
    closed this as completedin #13377on Apr 18, 2025
  3. nicoddemus
    closed this as completedin 61c204aon Apr 18, 2025
  4. nicoddemus
    added 2 commits that reference this issue on Apr 19, 2025

    Fix handling of positional-only parameters in test methods (pytest-de…

    This issue will close once commit c9c9dc0 is merged into the 'main' branch.
    c9c9dc0

    Fix handling of positional-only parameters in test methods (#13377) (#…

    This issue will close once commit 7c34357 is merged into the 'main' branch.
    7c34357
  5. BahramF73
    added a commit that references this issue on Apr 20, 2025

    Fix handling of positional-only parameters in test methods (pytest-de…

    This issue will close once commit 61c204a is merged into the 'main' branch.
    61c204a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.