Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,17 @@ 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
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
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 @@ -200,10 +200,17 @@ 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
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
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
|
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
---
source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
---
PT012.py:42:5: PT012 `pytest.raises()` block should contain a single simple statement
|
41 | def test_error_multiple_statements():
42 | / with pytest.raises(AttributeError):
43 | | len([])
44 | | [].size
| |_______________^ PT012
|

PT012.py:48:5: PT012 `pytest.raises()` block should contain a single simple statement
|
47 | async def test_error_complex_statement():
48 | / with pytest.raises(AttributeError):
49 | | if True:
50 | | [].size
| |___________________^ PT012
51 |
52 | with pytest.raises(AttributeError):
|

PT012.py:52:5: PT012 `pytest.raises()` block should contain a single simple statement
|
50 | [].size
51 |
52 | / with pytest.raises(AttributeError):
53 | | for i in []:
54 | | [].size
| |___________________^ PT012
55 |
56 | with pytest.raises(AttributeError):
|

PT012.py:56:5: PT012 `pytest.raises()` block should contain a single simple statement
|
54 | [].size
55 |
56 | / with pytest.raises(AttributeError):
57 | | async for i in []:
58 | | [].size
| |___________________^ PT012
59 |
60 | with pytest.raises(AttributeError):
|

PT012.py:60:5: PT012 `pytest.raises()` block should contain a single simple statement
|
58 | [].size
59 |
60 | / with pytest.raises(AttributeError):
61 | | while True:
62 | | [].size
| |___________________^ PT012
63 |
64 | with pytest.raises(AttributeError):
|

PT012.py:64:5: PT012 `pytest.raises()` block should contain a single simple statement
|
62 | [].size
63 |
64 | / with pytest.raises(AttributeError):
65 | | async with context_manager_under_test():
66 | | if True:
67 | | raise Exception
| |_______________________________^ PT012
|

PT012.py:71:5: PT012 `pytest.raises()` block should contain a single simple statement
|
70 | def test_error_try():
71 | / with pytest.raises(AttributeError):
72 | | try:
73 | | [].size
74 | | except:
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
|
Loading
Loading