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

pytest_cases.fixture unpack_into seems to invalidate scope argument #296

Open
lucapton opened this issue Mar 13, 2023 · 3 comments
Open

Comments

@lucapton
Copy link

The example below causes a ScopeMismatch error despite everything being session scoped.

import pytest
import pytest_cases


@pytest_cases.fixture(scope="session", unpack_into="a1, a2")
def a():
    return None, None


@pytest.fixture(scope="session")
def b(a1):
    return None


def test_b(b):
    pass

results in:

============================= test session starts ==============================
collecting ... collected 1 item

test_unpack_into_bug.py::test_b ERROR                                    [100%]
test setup failed
ScopeMismatch: You tried to access the function scoped fixture a1 with a session scoped request object, involved factories:
tests/test_unpack_into_bug.py:10:  def b(a1)
<makefun-gen-11>:1:  def a1(a, request)






========================= 4 warnings, 1 error in 0.44s =========================

Process finished with exit code 1

If you directly call pytest_cases.unpack_into("a1, a2", a) instead of the argument in pytest_cases.fixture, then there there is no issue as a1 and a2 are correctly scoped to session instead of function.

I'm getting these results using python 3.8.10, pytest 7.2.2, and pytest_cases 3.6.14 in an Ubuntu 20.04 docker container.

@smarie
Copy link
Owner

smarie commented Mar 13, 2023

Good catch @lucapton !
Looking at the source code, this is...normal :(

This is because my current code in _unpack_fixture only reuses the original fixture's scope if the provided source fixture is a fixture symbol.

https://github.com/smarie/python-pytest-cases/blob/main/src/pytest_cases/fixture_core1_unions.py#L511

When it is a string it uses 'function' scope. It does not try to ask pytest's fixture manager for help.

But fixing this would anyway not help in your situation, since the fixture a is not created yet when _unpack_fixture is called:

https://github.com/smarie/python-pytest-cases/blob/main/src/pytest_cases/fixture_core2.py#L410

So as a workaround I suggest to

  • modify _unpack_fixture to support an explicit scope: str = None argument, that would be used to override the scope explicitly when provided
  • modify _decorate_fixture_plus so that the scope is actually explicitly passed to _unpack_fixture when unpack_into is non-none

Would you like to try this in a PR ? Otherwise, I'll do it one of these days, this seems fairly straightforward so that I can squeeze it into my agenda.

@lucapton
Copy link
Author

I'm not familiar with the inner workings of pytest nor pytest_cases, so I don't think I can afford the time to do the PR, as much as I'd like to.

I want to clarify that pytest_cases.unpack_into("a1, a2", a) is a perfect workaround for the desired result I wanted. Still, the fact that fixtures created through pytest_cases.fixture(scope=x, unpack_into="y, z") don't have scope x but instead always function tripped me up for a while so I wanted to share both the issue and the workaround.

@smarie
Copy link
Owner

smarie commented Mar 17, 2023

Very clear @lucapton , thanks! I'll see what I can do one of these days

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

No branches or pull requests

2 participants