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 — parametrize with scope breaks fixture dependencies · Issue #13248 · pytest-dev/pytest

parametrize with scope breaks fixture dependencies #13248

New issue
Copy link
New issue
Copy link
Closed
#13249
Closed
parametrize with scope breaks fixture dependencies#13248
#13249
Copy link
Labels
topic: fixturesanything involving fixtures directly or indirectly

Description

@Anton3
Anton3
opened on Feb 24, 2025
Issue body actions

Reproducer:

import pytest


@pytest.fixture(scope="session")
def my_fixture(request):
    return getattr(request, "param", None)


@pytest.fixture(scope="session")
def another_fixture(my_fixture):
    return my_fixture


@pytest.mark.parametrize("my_fixture", ["a"], indirect=True, scope="function")
def test_foo(another_fixture):
    pass

Produces the following error:

ScopeMismatch: You tried to access the function scoped fixture my_fixture with a session scoped request object. Requesting fixture stack:

def another_fixture(my_fixture):

Requested fixture:
my_fixture

parametrize with scope is sometimes important for test isolation:
#9287 (comment)

The bug is not reproduced consistently, it may be hidden if the fixture is already evaluated by a function-scoped fixture. For example:

import pytest


@pytest.fixture(scope="session")
def my_fixture(request):
    return getattr(request, "param", None)


@pytest.fixture(scope="session")
def another_fixture(my_fixture):
    return my_fixture


# autouse to make this fixture run first.
@pytest.fixture(scope="function", autouse=True)
def hack(my_fixture):
    pass


@pytest.mark.parametrize("my_fixture", ["a"], indirect=True, scope="function")
def test_foo(hack, another_fixture):
    pass

This does not return any errors, works as expected.

Activity

  1. RonnyPfannschmidt commented on Feb 24, 2025

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Feb 24, 2025
    Member
    More actions

    both variants should error - the fixture is defined as session scope and the parameterize is not allowed to change that

  2. Anton3 commented on Feb 24, 2025

    @Anton3
    Anton3
    on Feb 24, 2025
    ContributorAuthor
    More actions

    @RonnyPfannschmidt From the documentation of scope:

            :param scope:
                If specified it denotes the scope of the parameters.
                The scope is used for grouping tests by parameter instances.
                It will also override any fixture-function defined scope, allowing
                to set a dynamic scope using test context or configuration.
    

    I've found it very useful to narrow the fixture's scope this way to prevent side effects of a test (or a group of tests) with specific parameter value from leaking into other tests.

    It completely solves #9287, in particular.

  3. Anton3 commented on Feb 24, 2025

    @Anton3
    Anton3
    on Feb 24, 2025
    ContributorAuthor
    More actions

    scope parameter in parametrize should only affect caching behavior and should not affect actual rules by which fixtures are allowed to depend on each other. This is exactly what the PR fixes

  4. Anton3 commented on Feb 24, 2025

    @Anton3
    Anton3
    on Feb 24, 2025
    ContributorAuthor
    More actions

    Notice the _check_scope call earlier in the same function:
    https://github.com/pytest-dev/pytest/blob/8.3.x/src/_pytest/fixtures.py#L560

    The form of late _check_scope call is quite obviously an oversight

  5. Zac-HD
    added
    topic: fixturesanything involving fixtures directly or indirectly
    on Mar 9, 2025
  6. RonnyPfannschmidt
    closed this as completedin #13249on Mar 9, 2025
  7. luketainton
    added a commit that references this issue on Jan 5, 2026

    chore(deps): update dependency pytest to v9 (#305)

    4b75154
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

    topic: fixturesanything involving fixtures directly or indirectly

    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.