Skip to content

Instantly share code, notes, and snippets.

@needs
Last active May 25, 2024 00:56
Show Gist options
  • Save needs/2cf888b317d07c3bd78e71d95bc16f10 to your computer and use it in GitHub Desktop.
Save needs/2cf888b317d07c3bd78e71d95bc16f10 to your computer and use it in GitHub Desktop.

The issue

When a fixture is not used but needs to be included for some reasons, pylint will rightfully issue a warning. For instance, a fixture setup a specific database state and another fixture include it to ensure database is in a specific state.

@pytest.fixture
def init_database():
    """Setup database state"""
    ...

@pytest.fixture
def add_element(init_database):
    """Insert element into the database"""
    # init_database is never used.
    ...

Not working fix

It's tempting to use @pytest.mark.usefixtures() decorator but it cannot be used on fixtures.

Hacky fix

One could just disable "unused-argument" warning for this specific line like, so:

@pytest.fixture
def init_database():
    """Setup database state"""
    ...

@pytest.fixture
def add_element(init_database): #pylint: disable=W0613
    """Insert element into the database"""
    # init_database is never used.
    ...

But since it disables the warning for the whole line, any truly unused argument added later will not be reported by pylint.

Better fix

An argument starting with an underscore is not subject to the "unused-argument" warning. Hence having your fixture name starting with an underscore fixes the warning.

@pytest.fixture
def _init_database():
    """Setup database state"""
    ...

@pytest.fixture
def add_element(_init_database):
    """Insert element into the database"""
    # _init_database is never used.
    ...

Even better fix

Use a different name for the fixture and its implementation function, in order to avoid some other pylint warnings:

@pytest.fixture(name='_init_database')
def fixture_init_database():
    """Setup database state"""
    ...

@pytest.fixture(name='add_element')
def fixture_add_element(_init_database):
    """Insert element into the database"""
    # _init_database is never used.
    ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment