Skip to content

Commit 44a8d1c

Browse files
authored
Add PT021, PT022 and PT023 docs (#6143)
1 parent 88b984e commit 44a8d1c

File tree

5 files changed

+113
-7
lines changed

5 files changed

+113
-7
lines changed

crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Violation for PytestCompositeAssertion {
9696
/// ```
9797
///
9898
/// ## References
99-
/// - [API Reference: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
99+
/// - [`pytest` documentation: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
100100
#[violation]
101101
pub struct PytestAssertInExcept {
102102
name: String,

crates/ruff/src/rules/flake8_pytest_style/rules/fail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use super::helpers::{is_empty_or_null_string, is_pytest_fail};
4545
/// ```
4646
///
4747
/// ## References
48-
/// - [API Reference: `pytest.fail`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fail)
48+
/// - [`pytest` documentation: `pytest.fail`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fail)
4949
#[violation]
5050
pub struct PytestFailWithoutMessage;
5151

crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use super::helpers::{
5353
/// - `flake8-pytest-style.fixture-parentheses`
5454
///
5555
/// ## References
56-
/// - [API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
56+
/// - [`pytest` documentation: API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
5757
#[violation]
5858
pub struct PytestFixtureIncorrectParenthesesStyle {
5959
expected: Parentheses,
@@ -169,7 +169,7 @@ impl Violation for PytestIncorrectFixtureNameUnderscore {
169169
/// ```
170170
///
171171
/// ## References
172-
/// - [API Reference: `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures)
172+
/// - [`pytest` documentation: `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures)
173173
#[violation]
174174
pub struct PytestFixtureParamWithoutValue {
175175
name: String,
@@ -217,7 +217,7 @@ impl Violation for PytestFixtureParamWithoutValue {
217217
/// ```
218218
///
219219
/// ## References
220-
/// - [`yield_fixture` functions](https://docs.pytest.org/en/latest/yieldfixture.html)
220+
/// - [`pytest` documentation: `yield_fixture` functions](https://docs.pytest.org/en/latest/yieldfixture.html)
221221
#[violation]
222222
pub struct PytestDeprecatedYieldFixture;
223223

@@ -228,6 +228,49 @@ impl Violation for PytestDeprecatedYieldFixture {
228228
}
229229
}
230230

231+
/// ## What it does
232+
/// Checks for unnecessary `request.addfinalizer` usages in `pytest` fixtures.
233+
///
234+
/// ## Why is this bad?
235+
/// `pytest` offers two ways to perform cleanup in fixture code. The first is
236+
/// sequential (via the `yield` statement), the second callback-based (via
237+
/// `request.addfinalizer`).
238+
///
239+
/// The sequential approach is more readable and should be preferred, unless
240+
/// the fixture uses the "factory as fixture" pattern.
241+
///
242+
/// ## Example
243+
/// ```python
244+
/// @pytest.fixture()
245+
/// def my_fixture(request):
246+
/// resource = acquire_resource()
247+
/// request.addfinalizer(resource.release)
248+
/// return resource
249+
/// ```
250+
///
251+
/// Use instead:
252+
/// ```python
253+
/// @pytest.fixture()
254+
/// def my_fixture():
255+
/// resource = acquire_resource()
256+
/// yield resource
257+
/// resource.release()
258+
///
259+
///
260+
/// # "factory-as-fixture" pattern
261+
/// @pytest.fixture()
262+
/// def my_factory(request):
263+
/// def create_resource(arg):
264+
/// resource = acquire_resource(arg)
265+
/// request.addfinalizer(resource.release)
266+
/// return resource
267+
///
268+
/// return create_resource
269+
/// ```
270+
///
271+
/// ## References
272+
/// - [`pytest` documentation: Adding finalizers directly](https://docs.pytest.org/en/latest/how-to/fixtures.html#adding-finalizers-directly)
273+
/// - [`pytest` documentation: Factories as fixtures](https://docs.pytest.org/en/latest/how-to/fixtures.html#factories-as-fixtures)
231274
#[violation]
232275
pub struct PytestFixtureFinalizerCallback;
233276

@@ -237,7 +280,39 @@ impl Violation for PytestFixtureFinalizerCallback {
237280
format!("Use `yield` instead of `request.addfinalizer`")
238281
}
239282
}
240-
283+
/// ## What it does
284+
/// Checks for unnecessary `yield` expressions in `pytest` fixtures.
285+
///
286+
/// ## Why is this bad?
287+
/// In `pytest` fixtures, the `yield` expression should only be used for fixtures
288+
/// that include teardown code, to clean up the fixture after the test function
289+
/// has finished executing.
290+
///
291+
/// ## Example
292+
/// ```python
293+
/// @pytest.fixture()
294+
/// def my_fixture():
295+
/// resource = acquire_resource()
296+
/// yield resource
297+
/// ```
298+
///
299+
/// Use instead:
300+
/// ```python
301+
/// @pytest.fixture()
302+
/// def my_fixture_with_teardown():
303+
/// resource = acquire_resource()
304+
/// yield resource
305+
/// resource.release()
306+
///
307+
///
308+
/// @pytest.fixture()
309+
/// def my_fixture_without_teardown():
310+
/// resource = acquire_resource()
311+
/// return resource
312+
/// ```
313+
///
314+
/// ## References
315+
/// - [`pytest` documentation: Teardown/Cleanup](https://docs.pytest.org/en/latest/how-to/fixtures.html#teardown-cleanup-aka-fixture-finalization)
241316
#[violation]
242317
pub struct PytestUselessYieldFixture {
243318
name: String,

crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,37 @@ use crate::registry::{AsRule, Rule};
99

1010
use super::helpers::get_mark_decorators;
1111

12+
/// ## What it does
13+
/// Checks for parameter-free `@pytest.mark.<marker>()` decorators with or
14+
/// without parentheses, depending on the `flake8-pytest-style.mark-parentheses`
15+
/// setting.
16+
///
17+
/// ## Why is this bad?
18+
/// If a `@pytest.mark.<marker>()` doesn't take any arguments, the parentheses are
19+
/// optional.
20+
///
21+
/// Either removing those unnecessary parentheses _or_ requiring them for all
22+
/// fixtures is fine, but it's best to be consistent.
23+
///
24+
/// ## Example
25+
/// ```python
26+
/// @pytest.mark.foo
27+
/// def test_something():
28+
/// ...
29+
/// ```
30+
///
31+
/// Use instead:
32+
/// ```python
33+
/// @pytest.mark.foo()
34+
/// def test_something():
35+
/// ...
36+
/// ```
37+
///
38+
/// ## Options
39+
/// - `flake8-pytest-style.mark-parentheses`
40+
///
41+
/// ## References
42+
/// - [`pytest` documentation: Marks](https://docs.pytest.org/en/latest/reference/reference.html#marks)
1243
#[violation]
1344
pub struct PytestIncorrectMarkParenthesesStyle {
1445
mark_name: String,

crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Violation for PytestRaisesTooBroad {
6666
/// ```
6767
///
6868
/// ## References
69-
/// - [API Reference: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
69+
/// - [`pytest` documentation: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
7070
#[violation]
7171
pub struct PytestRaisesWithoutException;
7272

0 commit comments

Comments
 (0)