Skip to content

[flake8-pytest-style] Add an autofix for PT020 - #27993

Merged
ntBre merged 5 commits into
astral-sh:mainfrom
Pierre-Sassoulas:pt020-autofix
Aug 31, 2026
Merged

ntBre merged 5 commits into
astral-sh:mainfrom
Pierre-Sassoulas:pt020-autofix

Conversation

@Pierre-Sassoulas

Copy link
Copy Markdown
Contributor

Summary

Following discussion in pytest-dev/pytest#14755. where we consider deleting the function in pytest 10. It's better if an autofix exists. I marked it as safe becuase fixture is equivalent afaiu. (Probably better to wait for a decision in the other issue to document it in this PR if it's removed in pytest 10)

Test Plan

Took example with NPY003's implementation, tested alias for yield_fixture and pytest, and various imports, used bare, with parens, with args, with trailing comment, and inside a class.

`pytest.yield_fixture` is a plain alias for `pytest.fixture`, so the rule
can offer the rewrite rather than only reporting it.

Following discussion in pytest-dev/pytest#14755. where
we consider deleting the function in pytest 10. It's better if an autofix exists.
I marked it as safe becuase 'fixture' is equivalent afaiu.
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 23, 2026 21:59
@astral-sh-bot

astral-sh-bot Bot commented Aug 23, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i love this variant

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This looks good to me overall, just a small versioning issue and a potential edge case in the fix safety.

Comment thread crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs Outdated
@ntBre ntBre added fixes Related to suggested fixes for violations preview Related to preview mode features labels Aug 28, 2026
Adding a fix to an existing stable rule is a breaking change under the
versioning policy, so the fix is only offered when preview is enabled.

Review feedback from astral-sh#27993.
The fix replaces the decorator's reference, which can span comments in
exotic cases, e.g.

    @(
        pytest
        # comment
        .yield_fixture
    )
    def example(): ...

Mark the fix as unsafe when comments fall in the replaced range; a
trailing comment after the decorator is unaffected and stays safe.

Review feedback from astral-sh#27993.
Spell out why the alias exists and what happens to it: `pytest.fixture`
has supported `yield` since pytest 3.0, the alias has been deprecated
since 6.2, and pytest-dev/pytest#14755 slated it for removal in pytest 10,
so it now raises `PytestRemovedIn10Warning`.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@ntBre
ntBre merged commit fd9e796 into astral-sh:main Aug 31, 2026
49 checks passed
@Pierre-Sassoulas
Pierre-Sassoulas deleted the pt020-autofix branch August 31, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants