Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PT002 ~ PT005 docs #6521

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ impl AlwaysAutofixableViolation for PytestFixtureIncorrectParenthesesStyle {
}
}

/// ## What it does
/// Checks for `pytest.fixture` calls with positional arguments.
///
/// ## Why is this bad?
/// For clarity and consistency, prefer using keyword arguments to specify
/// fixture configuration.
///
/// ## Example
/// ```python
/// @pytest.fixture("module")
/// def my_fixture():
/// ...
/// ```
///
/// Use instead:
/// ```python
/// @pytest.fixture(scope="module")
/// def my_fixture():
/// ...
/// ```
///
/// ## References
/// - [`pytest` documentation: `@pytest.fixture` functions](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fixture)
#[violation]
pub struct PytestFixturePositionalArgs {
function: String,
Expand All @@ -87,6 +110,28 @@ impl Violation for PytestFixturePositionalArgs {
}
}

/// ## What it does
/// Checks for `pytest.fixture` calls with `scope="function"`.
///
/// ## Why is this bad?
/// `scope="function"` can be omitted, as it is the default.
///
/// ## Example
/// ```python
/// @pytest.fixture(scope="function")
/// def my_fixture():
/// ...
/// ```
///
/// Use instead:
/// ```python
/// @pytest.fixture()
/// def my_fixture():
/// ...
/// ```
///
/// ## References
/// - [`pytest` documentation: `@pytest.fixture` functions](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fixture)
#[violation]
pub struct PytestExtraneousScopeFunction;

Expand All @@ -101,11 +146,89 @@ impl AlwaysAutofixableViolation for PytestExtraneousScopeFunction {
}
}

/// ## What it does
/// Checks for `pytest` fixtures that do not return a value, but are not named
/// with a leading underscore.
///
/// ## Why is this bad?
/// By convention, fixtures that don't return a value should be named with a
/// leading underscore, while fixtures that do return a value should not.
///
/// This rule ignores abstract fixtures and generators.
///
/// ## Example
/// ```python
/// @pytest.fixture()
/// def patch_something(mocker):
/// mocker.patch("module.object")
///
///
/// @pytest.fixture()
/// def use_context():
/// with create_context():
/// yield
/// ```
///
/// Use instead:
/// ```python
/// @pytest.fixture()
/// def _patch_something(mocker):
/// mocker.patch("module.object")
///
///
/// @pytest.fixture()
/// def _use_context():
/// with create_context():
/// yield
/// ```
///
/// ## References
/// - [`pytest` documentation: `@pytest.fixture` functions](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fixture)
#[violation]
pub struct PytestMissingFixtureNameUnderscore {
function: String,
}

/// ## What it does
/// Checks for `pytest` fixtures that return a value, but are named with a
/// leading underscore.
///
/// ## Why is this bad?
/// By convention, fixtures that don't return a value should be named with a
/// leading underscore, while fixtures that do return a value should not.
///
/// This rule ignores abstract fixtures.
///
/// ## Example
/// ```python
/// @pytest.fixture()
/// def _some_object():
/// return SomeClass()
///
///
/// @pytest.fixture()
/// def _some_object_with_cleanup():
/// obj = SomeClass()
/// yield obj
/// obj.cleanup()
/// ```
///
/// Use instead:
/// ```python
/// @pytest.fixture()
/// def some_object():
/// return SomeClass()
///
///
/// @pytest.fixture()
/// def some_object_with_cleanup():
/// obj = SomeClass()
/// yield obj
/// obj.cleanup()
/// ```
///
/// ## References
/// - [`pytest` documentation: `@pytest.fixture` functions](https://docs.pytest.org/en/latest/reference/reference.html#pytest-fixture)
impl Violation for PytestMissingFixtureNameUnderscore {
#[derive_message_formats]
fn message(&self) -> String {
Expand Down