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

[flake8-pytest-style] Do not emit diagnostics for empty for loops (PT012, PT031) #15542

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,44 @@ def test_error_try():
[].size
except:
raise


# https://github.com/astral-sh/ruff/issues/9730
def test_for_loops():

## Errors

with pytest.raises(RuntimeError):
for a in b:
print()

with pytest.raises(RuntimeError):
for a in b:
assert foo

with pytest.raises(RuntimeError):
async for a in b:
print()

with pytest.raises(RuntimeError):
async for a in b:
assert foo


## No errors in preview

with pytest.raises(RuntimeError):
for a in b:
pass

with pytest.raises(RuntimeError):
for a in b:
...

with pytest.raises(RuntimeError):
async for a in b:
pass

with pytest.raises(RuntimeError):
async for a in b:
...
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,44 @@ def test_error_try():
foo()
except:
raise


# https://github.com/astral-sh/ruff/issues/9730
def test_for_loops():

## Errors

with pytest.warns(RuntimeError):
for a in b:
print()

with pytest.warns(RuntimeError):
for a in b:
assert foo

with pytest.warns(RuntimeError):
async for a in b:
print()

with pytest.warns(RuntimeError):
async for a in b:
assert foo


## No errors in preview

with pytest.warns(RuntimeError):
for a in b:
pass

with pytest.warns(RuntimeError):
for a in b:
...

with pytest.warns(RuntimeError):
async for a in b:
pass

with pytest.warns(RuntimeError):
async for a in b:
...
32 changes: 31 additions & 1 deletion crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod tests {
use test_case::test_case;

use crate::registry::Rule;
use crate::settings::types::IdentifierPattern;
use crate::settings::types::{IdentifierPattern, PreviewMode};
use crate::test::test_path;
use crate::{assert_messages, settings};

Expand Down Expand Up @@ -358,6 +358,36 @@ mod tests {
Ok(())
}

#[test_case(
Rule::PytestRaisesWithMultipleStatements,
Path::new("PT012.py"),
Settings::default(),
"PT012_preview"
)]
#[test_case(
Rule::PytestWarnsWithMultipleStatements,
Path::new("PT031.py"),
Settings::default(),
"PT031_preview"
)]
fn test_pytest_style_preview(
rule_code: Rule,
path: &Path,
plugin_settings: Settings,
name: &str,
) -> Result<()> {
let diagnostics = test_path(
Path::new("flake8_pytest_style").join(path).as_path(),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
flake8_pytest_style: plugin_settings,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(name, diagnostics);
Ok(())
}

/// This test ensure that PT006 and PT007 don't conflict when both of them suggest a fix that
/// edits `argvalues` for `pytest.mark.parametrize`.
#[test]
Expand Down
16 changes: 15 additions & 1 deletion crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ use super::helpers::is_empty_or_null_string;
/// A `pytest.raises` context manager should only contain a single simple
/// statement that raises the expected exception.
///
/// In [preview], this rule allows `pytest.raises` bodies to contain `for`
/// loops with empty bodies (e.g., `pass` or `...` statements), to test
/// iterator behavior.
///
/// ## Example
/// ```python
/// import pytest
Expand All @@ -46,6 +50,8 @@ use super::helpers::is_empty_or_null_string;
///
/// ## References
/// - [`pytest` documentation: `pytest.raises`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[derive(ViolationMetadata)]
pub(crate) struct PytestRaisesWithMultipleStatements;

Expand Down Expand Up @@ -205,10 +211,18 @@ pub(crate) fn complex_raises(
// Check body for `pytest.raises` context manager
if raises_called {
let is_too_complex = if let [stmt] = body {
let in_preview = checker.settings.preview.is_enabled();

match stmt {
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
// Allow function and class definitions to test decorators
// Allow function and class definitions to test decorators.
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
// Allow empty `for` loops to test iterators.
Stmt::For(ast::StmtFor { body, .. }) if in_preview => match &body[..] {
[Stmt::Pass(_)] => false,
[Stmt::Expr(ast::StmtExpr { value, .. })] => !value.is_ellipsis_literal_expr(),
_ => true,
},
stmt => is_compound_statement(stmt),
}
} else {
Expand Down
18 changes: 16 additions & 2 deletions crates/ruff_linter/src/rules/flake8_pytest_style/rules/warns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ use super::helpers::is_empty_or_null_string;
/// A `pytest.warns` context manager should only contain a single
/// simple statement that triggers the expected warning.
///
/// In [preview], this rule allows `pytest.warns` bodies to contain `for`
/// loops with empty bodies (e.g., `pass` or `...` statements), to test
/// iterator behavior.
///
/// ## Example
/// ```python
/// import pytest
Expand All @@ -38,12 +42,14 @@ use super::helpers::is_empty_or_null_string;
///
/// def test_foo_warns():
/// setup()
/// with pytest.warning(Warning):
/// with pytest.warns(Warning):
/// foo()
/// ```
///
/// ## References
/// - [`pytest` documentation: `pytest.warns`](https://docs.pytest.org/en/latest/reference/reference.html#pytest-warns)
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[derive(ViolationMetadata)]
pub(crate) struct PytestWarnsWithMultipleStatements;

Expand Down Expand Up @@ -200,10 +206,18 @@ pub(crate) fn complex_warns(checker: &mut Checker, stmt: &Stmt, items: &[WithIte
// Check body for `pytest.warns` context manager
if warns_called {
let is_too_complex = if let [stmt] = body {
let in_preview = checker.settings.preview.is_enabled();

match stmt {
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
// Allow function and class definitions to test decorators
// Allow function and class definitions to test decorators.
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
// Allow empty `for` loops to test iterators.
Stmt::For(ast::StmtFor { body, .. }) if in_preview => match &body[..] {
[Stmt::Pass(_)] => false,
[Stmt::Expr(ast::StmtExpr { value, .. })] => !value.is_ellipsis_literal_expr(),
_ => true,
},
stmt => is_compound_statement(stmt),
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,95 @@ PT012.py:71:5: PT012 `pytest.raises()` block should contain a single simple stat
75 | | raise
| |_________________^ PT012
|

PT012.py:83:5: PT012 `pytest.raises()` block should contain a single simple statement
|
81 | ## Errors
82 |
83 | / with pytest.raises(RuntimeError):
84 | | for a in b:
85 | | print()
| |___________________^ PT012
86 |
87 | with pytest.raises(RuntimeError):
|

PT012.py:87:5: PT012 `pytest.raises()` block should contain a single simple statement
|
85 | print()
86 |
87 | / with pytest.raises(RuntimeError):
88 | | for a in b:
89 | | assert foo
| |______________________^ PT012
90 |
91 | with pytest.raises(RuntimeError):
|

PT012.py:91:5: PT012 `pytest.raises()` block should contain a single simple statement
|
89 | assert foo
90 |
91 | / with pytest.raises(RuntimeError):
92 | | async for a in b:
93 | | print()
| |___________________^ PT012
94 |
95 | with pytest.raises(RuntimeError):
|

PT012.py:95:5: PT012 `pytest.raises()` block should contain a single simple statement
|
93 | print()
94 |
95 | / with pytest.raises(RuntimeError):
96 | | async for a in b:
97 | | assert foo
| |______________________^ PT012
|

PT012.py:102:5: PT012 `pytest.raises()` block should contain a single simple statement
|
100 | ## No errors in preview
101 |
102 | / with pytest.raises(RuntimeError):
103 | | for a in b:
104 | | pass
| |________________^ PT012
105 |
106 | with pytest.raises(RuntimeError):
|

PT012.py:106:5: PT012 `pytest.raises()` block should contain a single simple statement
|
104 | pass
105 |
106 | / with pytest.raises(RuntimeError):
107 | | for a in b:
108 | | ...
| |_______________^ PT012
109 |
110 | with pytest.raises(RuntimeError):
|

PT012.py:110:5: PT012 `pytest.raises()` block should contain a single simple statement
|
108 | ...
109 |
110 | / with pytest.raises(RuntimeError):
111 | | async for a in b:
112 | | pass
| |________________^ PT012
113 |
114 | with pytest.raises(RuntimeError):
|

PT012.py:114:5: PT012 `pytest.raises()` block should contain a single simple statement
|
112 | pass
113 |
114 | / with pytest.raises(RuntimeError):
115 | | async for a in b:
116 | | ...
| |_______________^ PT012
|
Loading
Loading