RUF076: should not fire in conftest.py by default (autouse is the standard pattern there)
documentation
## Summary
`RUF076` (pytest-fixture-autouse) discourages `autouse=True` in pytest fixtures, but fires in `conftest.py` files by default. This is problematic because `conftest.py` is precisely where `autouse=True` fixtures are most appropriate and widely used — it's the standard pytest pattern for applying shared setup across a test directory.
The rule's own documentation acknowledges this tension:
> This is a pedantic rule that restricts a valid pytest pattern.
And even suggests a workaround to *only* apply it in conftest.py:
```toml
[tool.ruff.lint.per-file-ignores]
"!**/conftest.py" = ["RUF076"]
```
But this inverts the correct default. The rule should **not** fire in `conftest.py` by default — or at minimum, there should be a configuration option to scope it to non-conftest files only. Autouse fixtures in conftest.py are the documented, recommended pytest approach for directory-scoped setup (e.g., environment variables, database cleanup, logging config).
## Reproduction
```python
# conftest.py
import pytest
@pytest.fixture(autouse=True)
def setup_test_env() -> None:
"""Standard conftest autouse fixture for test environment setup."""
...
```
```toml
[tool.ruff]
lint.extend-select = ["RUF"]
lint.preview = true
```
```
$ ruff check conftest.py
RUF076: Avoid using `autouse=True` in `pytest.fixture` decorators
```
## Expected behavior
Either:
1. `RUF076` should not fire in `conftest.py` by default (since that's where autouse is most appropriate), OR
2. The default should be inverted: only fire in conftest.py if explicitly configured, and fire in test files by default (where autouse is actually problematic)
## Ruff version
```
ruff 0.15.18
```
## Context
The original rule request (#12491) specifically called out conftest.py as problematic, but in practice, `autouse` in conftest is the standard pytest pattern documented at:
- https://docs.pytest.org/en/stable/how-to/fixtures.html#autouse-fixtures-fixtures-you-don-t-have-to-request
- https://docs.pytest.org/en/stable/how-to/fixtures.html#conftest-py-sharing-fixtures-across-tests
The rule is useful for discouraging autouse in individual test files (where it's truly confusing), but not in conftest.py where it's expected behavior.
2 条评论