[ruff] Ban pytest autouse fixtures (RUF076) - #25477
Merged
Merged
Conversation
next release will be 0.15.16 not 0.15.15
Kilo59
commented
May 30, 2026
Kilo59
commented
May 30, 2026
ntBre
reviewed
Jun 1, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Thanks! This looks great to me overall, I just had a few minor suggestions. I'll also approve the CI workflow so we can get a sense for the ecosystem impact. I'm kind of expecting a lot of diagnostics.
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Centralizes the logic for identifying `pytest.fixture` decorators into a single helper function within `flake8_pytest_style`. This allows other rules, like `RUF076`, to consume the same helper, reducing code duplication.
Migrates the `pytest-fixture-autouse` (`RUF076`) test to the markdown format for improved documentation and snapshot testing. Updates the rule's documentation to clarify its pedantic nature and provide guidance on using `per-file-ignores` for targeted application, especially for `conftest.py` files.
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF076 | 1919 | 1919 | 0 | 0 | 0 |
ntBre
reviewed
Jun 4, 2026
ntBre
left a comment
Contributor
There was a problem hiding this comment.
Thank you! I had a few even smaller docs nits, and CI is flagging a docs formatting issue, but I think this is otherwise ready to go.
ruff] Ban pytest autouse fixtures (RUF076)
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
ntBre
added a commit
that referenced
this pull request
Jun 23, 2026
ntBre
added a commit
that referenced
this pull request
Jun 25, 2026
Summary -- This PR removes `RUF076` that was added in #25477 and closes #26178. It should also reopen #12491. The reason for the removal is that this rule is very opinionated. I knew that going in, but I failed to consider the effect of adding a pedantic rule to the `RUF` selector, which many preview users enable as a whole. The implementation was okay otherwise and can be reinstated once we have a better category for the rule, which #1774 should help with. We initially opted for a full revert, but marking the rule as removed both avoids an invalid configuration error for projects that already `ignore` the rule (although they will still see a warning) and prevents another rule in the future from reusing the code. I don't believe we've done this before, but we can also reinstate the rule simply by switching its metadata back from `removed_since` to `preview_since` (and updating the mdtests, which I also opted to preserve).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pytest.fixture(autouse=True)#12491This PR introduces a new Ruff-specific lint rule (
RUF076) in the preview namespace to banpytestfixtures that set the parameterautouse=Truein the decorator constructor.Implicitly running autouse fixtures is discouraged because it hides test dependencies and can introduce subtle side effects (especially when defined in shared
conftest.pyfiles), making the test suite harder to reason about and debug. Explicit parameter injection is preferred.Note
A default banning of all autouse fixtures is opinionated; we may want to wait for further configuration settings (such as filtering by scope or restricting to
conftest.pyfiles) and appropriate defaults before taking this rule out of preview.Examples
Basic
Advanced (Combining Fixtures)
Instead of using
autouse=Trueto avoid repetitive declarations in test parameters, multiple related fixtures can be aggregated into a single high-level fixture and requested explicitly:Future Enhancements
Developer
TODOcomments have been added to the checker function to consider adding settings for:conftest.pyfile.functionscope, which is the default and typically the most costly), supporting a whitelist or blacklist approach.References
pytestdocumentation: Sharing fixtures across classes, modules, packages or sessionpytestdocumentation: Fixtures can request other fixturesTest Plan
Added comprehensive test cases in a new test fixture file
RUF076.py. The rule has been registered underpreview_rulesincrates/ruff_linter/src/rules/ruff/mod.rsand verified using snapshot testing:We also ran:
cargo dev generate-allto generate updated linter schemas and documentation.cargo clippy -p ruff_linter --all-targets --all-features -- -D warningsto verify code quality.uvx prekon all changed files to ensure compliance with the repository's formatting/linting hooks.