Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parametrizing a test with an empty list calls ids callback with the private constant NOTSET #13031

Open
mon opened this issue Dec 6, 2024 · 6 comments · May be fixed by #13073
Open

Parametrizing a test with an empty list calls ids callback with the private constant NOTSET #13031

mon opened this issue Dec 6, 2024 · 6 comments · May be fixed by #13073

Comments

@mon
Copy link

mon commented Dec 6, 2024

I parametrize tests based on a user-provided list. Sometimes this list is empty, which disables some tests as a result. However, I would expect the ids callback that I provide to not be called at all in that case, but it is actually called with the private NOTSET sentinel. As this is not exported, I can't easily check for it in my ids function and return a useless value to match it.

Reproduction:

conftest.py

import logging

def idmap(obj):
    logging.info(obj)

def pytest_generate_tests(metafunc):
    if "arg" in metafunc.fixturenames:
        metafunc.parametrize(
            "arg", [], ids=idmap
        )

test_demo.py

def test_demo(arg):
    pass
$: pytest --collect-only --log-cli-level=DEBUG
...
INFO     root:conftest.py:4 NotSetType.token
...

Python: 3.10.15
pytest: 8.3.4
OS: macOS Sonoma 14.6.1

@RonnyPfannschmidt
Copy link
Member

For empty id lists we generate a single test with a warning

@mon
Copy link
Author

mon commented Dec 6, 2024

Yes, which is reasonable - but I can't detect this in my ID mapping function because it's a hidden type. Perhaps the sentinel can just be exported? Or don't call the ID map for the "no parametrization" case.

I can make a PR in either case!

@mon
Copy link
Author

mon commented Dec 6, 2024

I don't quite understand - I don't get access to the ParameterSet, and thus can't see its marks. I simply get NOTSET passed to the ids callback. Because NOTSET is internal to Pytest, I cannot compare the arg against it nor type annotate my ids function to accept it.

@RonnyPfannschmidt
Copy link
Member

indeed - we should use a string that indicates it

@RonnyPfannschmidt
Copy link
Member

i have a wip pr that sets the string NOTSET as direct id to avoid this - 'll polish it a litlle and push

RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this issue Dec 17, 2024
…ter list stand-in

this ensures we dont invoke idfunc with the internal NOTSET enum token
RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this issue Dec 18, 2024
…ter list stand-in

this ensures we dont invoke idfunc with the internal NOTSET enum token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants