Derived from the flake8-pytest-style linter.
What it does#
Checks for unnecessary
request.addfinalizer usages in
Why is this bad?#
pytest offers two ways to perform cleanup in fixture code. The first is
sequential (via the
yield statement), the second callback-based (via
The sequential approach is more readable and should be preferred, unless the fixture uses the "factory as fixture" pattern.
import pytest @pytest.fixture() def my_fixture(): resource = acquire_resource() yield resource resource.release() # "factory-as-fixture" pattern @pytest.fixture() def my_factory(request): def create_resource(arg): resource = acquire_resource(arg) request.addfinalizer(resource.release) return resource return create_resource