Skip to content

AttributeError: 'NotSetType' object has no attribute 'id' when using empty params list #13401

@steadyfirmness

Description

@steadyfirmness

Steps to Reproduce:

  1. Create a pytest fixture with an empty params list and an ids function that references an attribute of the param, e.g.:
@pytest.fixture(params=[], ids=lambda x: x.id)
def empty_param(request):
    return request.param

2.Write a test function that uses this fixture:

def test_empty_param(empty_param):
    pass

3.Run the test using the pytest command:

pytest test_foo.py::test_empty_param

Expected Behavior:

The test should either be skipped or provide a more informative error message indicating that the params list is empty.

Actual Behavior:

The test raises an AttributeError: 'NotSetType' object has no attribute 'id' error.

Error Output

tests/test_foo.py:None (tests/test_foo.py)
test_foo.py:163: in <lambda>
    @pytest.fixture(params=[], ids=lambda x: x.id)
E   AttributeError: 'NotSetType' object has no attribute 'id'

The above exception was the direct cause of the following exception:
../venv/lib/python3.12/site-packages/pluggy/_hooks.py:513: in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
...
E   ValueError: tests/test_foo.py::test_empty_param: error raised while trying to determine id of parameter 'empty_param' at position 0
collected 0 items / 1 error

Environment:

pytest version: 8.3.4
Python version: 3.12
Operating System: macOS

Possible Solution:

Provide a more informative error message or skip the test when the params list is empty. Alternatively, document the expected behavior when using an empty params list with a custom ids function.

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