Skip to content

Commit

Permalink
Add PT021, PT022 and PT023 docs (#6143)
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy authored Aug 1, 2023
1 parent 88b984e commit 44a8d1c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Violation for PytestCompositeAssertion {
/// ```
///
/// ## References
/// - [API Reference: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
/// - [`pytest` documentation: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
#[violation]
pub struct PytestAssertInExcept {
name: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_pytest_style/rules/fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use super::helpers::{is_empty_or_null_string, is_pytest_fail};
/// ```
///
/// ## References
/// - [API Reference: `pytest.fail`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fail)
/// - [`pytest` documentation: `pytest.fail`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fail)
#[violation]
pub struct PytestFailWithoutMessage;

Expand Down
83 changes: 79 additions & 4 deletions crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use super::helpers::{
/// - `flake8-pytest-style.fixture-parentheses`
///
/// ## References
/// - [API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
/// - [`pytest` documentation: API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
#[violation]
pub struct PytestFixtureIncorrectParenthesesStyle {
expected: Parentheses,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Violation for PytestIncorrectFixtureNameUnderscore {
/// ```
///
/// ## References
/// - [API Reference: `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures)
/// - [`pytest` documentation: `pytest.mark.usefixtures`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-usefixtures)
#[violation]
pub struct PytestFixtureParamWithoutValue {
name: String,
Expand Down Expand Up @@ -217,7 +217,7 @@ impl Violation for PytestFixtureParamWithoutValue {
/// ```
///
/// ## References
/// - [`yield_fixture` functions](https://docs.pytest.org/en/latest/yieldfixture.html)
/// - [`pytest` documentation: `yield_fixture` functions](https://docs.pytest.org/en/latest/yieldfixture.html)
#[violation]
pub struct PytestDeprecatedYieldFixture;

Expand All @@ -228,6 +228,49 @@ impl Violation for PytestDeprecatedYieldFixture {
}
}

/// ## What it does
/// Checks for unnecessary `request.addfinalizer` usages in `pytest` fixtures.
///
/// ## Why is this bad?
/// `pytest` offers two ways to perform cleanup in fixture code. The first is
/// sequential (via the `yield` statement), the second callback-based (via
/// `request.addfinalizer`).
///
/// The sequential approach is more readable and should be preferred, unless
/// the fixture uses the "factory as fixture" pattern.
///
/// ## Example
/// ```python
/// @pytest.fixture()
/// def my_fixture(request):
/// resource = acquire_resource()
/// request.addfinalizer(resource.release)
/// return resource
/// ```
///
/// Use instead:
/// ```python
/// @pytest.fixture()
/// def my_fixture():
/// resource = acquire_resource()
/// yield resource
/// resource.release()
///
///
/// # "factory-as-fixture" pattern
/// @pytest.fixture()
/// def my_factory(request):
/// def create_resource(arg):
/// resource = acquire_resource(arg)
/// request.addfinalizer(resource.release)
/// return resource
///
/// return create_resource
/// ```
///
/// ## References
/// - [`pytest` documentation: Adding finalizers directly](https://docs.pytest.org/en/latest/how-to/fixtures.html#adding-finalizers-directly)
/// - [`pytest` documentation: Factories as fixtures](https://docs.pytest.org/en/latest/how-to/fixtures.html#factories-as-fixtures)
#[violation]
pub struct PytestFixtureFinalizerCallback;

Expand All @@ -237,7 +280,39 @@ impl Violation for PytestFixtureFinalizerCallback {
format!("Use `yield` instead of `request.addfinalizer`")
}
}

/// ## What it does
/// Checks for unnecessary `yield` expressions in `pytest` fixtures.
///
/// ## Why is this bad?
/// In `pytest` fixtures, the `yield` expression should only be used for fixtures
/// that include teardown code, to clean up the fixture after the test function
/// has finished executing.
///
/// ## Example
/// ```python
/// @pytest.fixture()
/// def my_fixture():
/// resource = acquire_resource()
/// yield resource
/// ```
///
/// Use instead:
/// ```python
/// @pytest.fixture()
/// def my_fixture_with_teardown():
/// resource = acquire_resource()
/// yield resource
/// resource.release()
///
///
/// @pytest.fixture()
/// def my_fixture_without_teardown():
/// resource = acquire_resource()
/// return resource
/// ```
///
/// ## References
/// - [`pytest` documentation: Teardown/Cleanup](https://docs.pytest.org/en/latest/how-to/fixtures.html#teardown-cleanup-aka-fixture-finalization)
#[violation]
pub struct PytestUselessYieldFixture {
name: String,
Expand Down
31 changes: 31 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/marks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ use crate::registry::{AsRule, Rule};

use super::helpers::get_mark_decorators;

/// ## What it does
/// Checks for parameter-free `@pytest.mark.<marker>()` decorators with or
/// without parentheses, depending on the `flake8-pytest-style.mark-parentheses`
/// setting.
///
/// ## Why is this bad?
/// If a `@pytest.mark.<marker>()` doesn't take any arguments, the parentheses are
/// optional.
///
/// Either removing those unnecessary parentheses _or_ requiring them for all
/// fixtures is fine, but it's best to be consistent.
///
/// ## Example
/// ```python
/// @pytest.mark.foo
/// def test_something():
/// ...
/// ```
///
/// Use instead:
/// ```python
/// @pytest.mark.foo()
/// def test_something():
/// ...
/// ```
///
/// ## Options
/// - `flake8-pytest-style.mark-parentheses`
///
/// ## References
/// - [`pytest` documentation: Marks](https://docs.pytest.org/en/latest/reference/reference.html#marks)
#[violation]
pub struct PytestIncorrectMarkParenthesesStyle {
mark_name: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Violation for PytestRaisesTooBroad {
/// ```
///
/// ## References
/// - [API Reference: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
/// - [`pytest` documentation: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
#[violation]
pub struct PytestRaisesWithoutException;

Expand Down

0 comments on commit 44a8d1c

Please sign in to comment.