From a81ee25ca32cf19710c5940011674163e8c53f60 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 28 Jun 2026 10:14:32 -0500 Subject: [PATCH 1/4] Change diagnostic fix methods to unsafe edits --- .../src/rules/flake8_pytest_style/rules/fixture.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs index 0b2be586d7adf..13a340cb049b2 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs @@ -840,9 +840,9 @@ fn check_fixture_returns(checker: &Checker, name: &str, body: &[Stmt], returns: )) }); if let Some(return_type_edit) = return_type_edit { - diagnostic.set_fix(Fix::safe_edits(yield_edit, [return_type_edit])); + diagnostic.set_fix(Fix::unsafe_edits(yield_edit, [return_type_edit])); } else { - diagnostic.set_fix(Fix::safe_edit(yield_edit)); + diagnostic.set_fix(Fix::unsafe_edit(yield_edit)); } } } From c88539d3caa31a5e9e26a05554910202f227fff2 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 28 Jun 2026 10:15:54 -0500 Subject: [PATCH 2/4] Update ruff_linter__rules__flake8_pytest_style__tests__PT022.snap --- ...es__flake8_pytest_style__tests__PT022.snap | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap index 0d4813e1873cd..6f01f365958e3 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap @@ -1,55 +1,54 @@ --- source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs --- -PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` - --> PT022.py:17:5 +PT022.py:15:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` | -15 | def error(): -16 | resource = acquire_resource() -17 | yield resource - | ^^^^^^^^^^^^^^ +13 | @pytest.fixture() +14 | def error(): +15 | yield resource + | ^^^^^ PT022 +16 | +17 | | help: Replace `yield` with `return` | -16 | resource = acquire_resource() - - yield resource -17 + return resource -18 | +13 | @pytest.fixture() +14 | def error(): +15 - yield resource +15 + return resource | +note: This is an unsafe fix and may change runtime behavior -PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` - --> PT022.py:37:5 +PT022.py:35:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` | -35 | def error() -> typing.Generator[typing.Any, None, None]: -36 | resource = acquire_resource() -37 | yield resource - | ^^^^^^^^^^^^^^ +33 | @pytest.fixture +34 | def error(): +35 | yield resource + | ^^^^^ PT022 +36 | +37 | | help: Replace `yield` with `return` | -34 | @pytest.fixture() - - def error() -> typing.Generator[typing.Any, None, None]: -35 + def error() -> typing.Any: -36 | resource = acquire_resource() - - yield resource -37 + return resource -38 | +33 | @pytest.fixture +34 | def error(): +35 - yield resource +35 + return resource | +note: This is an unsafe fix and may change runtime behavior -PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` - --> PT022.py:43:5 +PT022.py:41:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` | -41 | def error() -> Generator[Resource, None, None]: -42 | resource = acquire_resource() -43 | yield resource - | ^^^^^^^^^^^^^^ +39 | @pytest.yield_fixture +40 | def error(): +41 | yield resource + | ^^^^^ PT022 | help: Replace `yield` with `return` | -40 | @pytest.fixture() - - def error() -> Generator[Resource, None, None]: -41 + def error() -> Resource: -42 | resource = acquire_resource() - - yield resource -43 + return resource +39 | @pytest.yield_fixture +40 | def error(): +41 - yield resource +41 + return resource | +note: This is an unsafe fix and may change runtime behavior From d7a2fd0925f134479d81a00cf260394cee975595 Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 28 Jun 2026 10:57:46 -0500 Subject: [PATCH 3/4] Update ruff_linter__rules__flake8_pytest_style__tests__PT022.snap --- ...es__flake8_pytest_style__tests__PT022.snap | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap index 6f01f365958e3..e6a67bed5ed68 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT022.snap @@ -1,54 +1,54 @@ --- source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs --- -PT022.py:15:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` +PT022.py:17:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` | -13 | @pytest.fixture() -14 | def error(): -15 | yield resource +15 | def error(): +16 | resource = acquire_resource() +17 | yield resource | ^^^^^ PT022 -16 | -17 | +18 | +19 | | help: Replace `yield` with `return` | -13 | @pytest.fixture() -14 | def error(): -15 - yield resource -15 + return resource +15 | def error(): +16 | resource = acquire_resource() +17 - yield resource +17 + return resource | note: This is an unsafe fix and may change runtime behavior -PT022.py:35:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` +PT022.py:37:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` | -33 | @pytest.fixture -34 | def error(): -35 | yield resource +35 | @pytest.fixture +36 | def error(): +37 | yield resource | ^^^^^ PT022 -36 | -37 | +38 | +39 | | help: Replace `yield` with `return` | -33 | @pytest.fixture -34 | def error(): -35 - yield resource -35 + return resource +35 | @pytest.fixture +36 | def error(): +37 - yield resource +37 + return resource | note: This is an unsafe fix and may change runtime behavior -PT022.py:41:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` +PT022.py:43:5: PT022 [*] No teardown in fixture `error`, use `return` instead of `yield` | -39 | @pytest.yield_fixture -40 | def error(): -41 | yield resource +41 | @pytest.yield_fixture +42 | def error(): +43 | yield resource | ^^^^^ PT022 | help: Replace `yield` with `return` | -39 | @pytest.yield_fixture -40 | def error(): -41 - yield resource -41 + return resource +41 | @pytest.yield_fixture +42 | def error(): +43 - yield resource +43 + return resource | note: This is an unsafe fix and may change runtime behavior From 435ed987a81ccd7802c7a61b8bd92671cf606497 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Wed, 29 Jul 2026 15:58:26 -0400 Subject: [PATCH 4/4] document fix safety --- .../src/rules/flake8_pytest_style/rules/fixture.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs index 13a340cb049b2..285ea7d35a8f3 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs @@ -518,6 +518,11 @@ impl Violation for PytestFixtureFinalizerCallback { /// return resource /// ``` /// +/// ## Fix safety +/// +/// This rule's fix is always marked unsafe because removing the `yield` can change the behavior of +/// code that relies on implicit cleanup, such as when a value is garbage-collected. +/// /// ## References /// - [`pytest` documentation: Teardown/Cleanup](https://docs.pytest.org/en/latest/how-to/fixtures.html#teardown-cleanup-aka-fixture-finalization) #[derive(ViolationMetadata)]