Skip to content

Commit 25217dd

Browse files
committed
Add docs
1 parent 992c655 commit 25217dd

3 files changed

Lines changed: 41 additions & 14 deletions

File tree

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use super::helpers::is_empty_or_null_string;
2020
/// A `pytest.raises` context manager should only contain a single simple
2121
/// statement that raises the expected exception.
2222
///
23+
/// In [preview], this rule allows `pytest.raises` bodies to contain `for`
24+
/// loops with empty bodies (e.g., `pass` or `...` statements), to test
25+
/// iterator behavior.
26+
///
2327
/// ## Example
2428
/// ```python
2529
/// import pytest
@@ -46,6 +50,8 @@ use super::helpers::is_empty_or_null_string;
4650
///
4751
/// ## References
4852
/// - [`pytest` documentation: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
53+
///
54+
/// [preview]: https://docs.astral.sh/ruff/preview/
4955
#[derive(ViolationMetadata)]
5056
pub(crate) struct PytestRaisesWithMultipleStatements;
5157

@@ -209,13 +215,21 @@ pub(crate) fn complex_raises(
209215

210216
match stmt {
211217
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
212-
// Allow function and class definitions to test decorators
218+
// Allow function and class definitions to test decorators.
213219
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
214-
Stmt::For(ast::StmtFor { body, .. }) if in_preview => match &body[..] {
215-
[Stmt::Pass(_)] => false,
216-
[Stmt::Expr(ast::StmtExpr { value, .. })] => !value.is_ellipsis_literal_expr(),
217-
_ => true,
218-
},
220+
// Allow empty `for` loops to test iterators.
221+
Stmt::For(ast::StmtFor { body, .. }) if in_preview => {
222+
!body.iter().all(|stmt| match stmt {
223+
Stmt::Pass(_) => true,
224+
Stmt::Expr(ast::StmtExpr { value, range: _ }) => {
225+
matches!(
226+
value.as_ref(),
227+
Expr::StringLiteral(_) | Expr::EllipsisLiteral(_)
228+
)
229+
}
230+
_ => false,
231+
})
232+
}
219233
stmt => is_compound_statement(stmt),
220234
}
221235
} else {

crates/ruff_linter/src/rules/flake8_pytest_style/rules/warns.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use super::helpers::is_empty_or_null_string;
2020
/// A `pytest.warns` context manager should only contain a single
2121
/// simple statement that triggers the expected warning.
2222
///
23+
/// In [preview], this rule allows `pytest.warns` bodies to contain `for`
24+
/// loops with empty bodies (e.g., `pass` or `...` statements), to test
25+
/// iterator behavior.
26+
///
2327
/// ## Example
2428
/// ```python
2529
/// import pytest
@@ -38,12 +42,14 @@ use super::helpers::is_empty_or_null_string;
3842
///
3943
/// def test_foo_warns():
4044
/// setup()
41-
/// with pytest.warning(Warning):
45+
/// with pytest.warns(Warning):
4246
/// foo()
4347
/// ```
4448
///
4549
/// ## References
4650
/// - [`pytest` documentation: `pytest.warns`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-warns)
51+
///
52+
/// [preview]: https://docs.astral.sh/ruff/preview/
4753
#[derive(ViolationMetadata)]
4854
pub(crate) struct PytestWarnsWithMultipleStatements;
4955

@@ -204,13 +210,21 @@ pub(crate) fn complex_warns(checker: &mut Checker, stmt: &Stmt, items: &[WithIte
204210

205211
match stmt {
206212
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
207-
// Allow function and class definitions to test decorators
213+
// Allow function and class definitions to test decorators.
208214
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
209-
Stmt::For(ast::StmtFor { body, .. }) if in_preview => match &body[..] {
210-
[Stmt::Pass(_)] => false,
211-
[Stmt::Expr(ast::StmtExpr { value, .. })] => !value.is_ellipsis_literal_expr(),
212-
_ => true,
213-
},
215+
// Allow empty `for` loops to test iterators.
216+
Stmt::For(ast::StmtFor { body, .. }) if in_preview => {
217+
!body.iter().all(|stmt| match stmt {
218+
Stmt::Pass(_) => true,
219+
Stmt::Expr(ast::StmtExpr { value, range: _ }) => {
220+
matches!(
221+
value.as_ref(),
222+
Expr::StringLiteral(_) | Expr::EllipsisLiteral(_)
223+
)
224+
}
225+
_ => false,
226+
})
227+
}
214228
stmt => is_compound_statement(stmt),
215229
}
216230
} else {

ruff.schema.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)