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

Allow pytest.raises body to contain a single func or class definition #6083

Merged
merged 1 commit into from
Jul 26, 2023
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
10 changes: 10 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pytest_style/PT012.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ def test_ok_complex_single_call():
)


def test_ok_func_and_class():
with pytest.raises(AttributeError):
class A:
pass

with pytest.raises(AttributeError):
def f():
pass


def test_error_multiple_statements():
with pytest.raises(AttributeError):
len([])
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ pub(crate) fn complex_raises(
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
is_non_trivial_with_body(body)
}
// Allow function and class definitions to test decorators
Stmt::ClassDef(_) | Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => false,
stmt => is_compound_statement(stmt),
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
---
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
---
PT012.py:32:5: PT012 `pytest.raises()` block should contain a single simple statement
PT012.py:42:5: PT012 `pytest.raises()` block should contain a single simple statement
|
31 | def test_error_multiple_statements():
32 | with pytest.raises(AttributeError):
41 | def test_error_multiple_statements():
42 | with pytest.raises(AttributeError):
| _____^
33 | | len([])
34 | | [].size
43 | | len([])
44 | | [].size
| |_______________^ PT012
|

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

PT012.py:42:5: PT012 `pytest.raises()` block should contain a single simple statement
PT012.py:52:5: PT012 `pytest.raises()` block should contain a single simple statement
|
40 | [].size
41 |
42 | with pytest.raises(AttributeError):
50 | [].size
51 |
52 | with pytest.raises(AttributeError):
| _____^
43 | | for i in []:
44 | | [].size
53 | | for i in []:
54 | | [].size
| |___________________^ PT012
45 |
46 | with pytest.raises(AttributeError):
55 |
56 | with pytest.raises(AttributeError):
|

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

PT012.py:50:5: PT012 `pytest.raises()` block should contain a single simple statement
PT012.py:60:5: PT012 `pytest.raises()` block should contain a single simple statement
|
48 | [].size
49 |
50 | with pytest.raises(AttributeError):
58 | [].size
59 |
60 | with pytest.raises(AttributeError):
| _____^
51 | | while True:
52 | | [].size
61 | | while True:
62 | | [].size
| |___________________^ PT012
53 |
54 | with pytest.raises(AttributeError):
63 |
64 | with pytest.raises(AttributeError):
|

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

PT012.py:61:5: PT012 `pytest.raises()` block should contain a single simple statement
PT012.py:71:5: PT012 `pytest.raises()` block should contain a single simple statement
|
60 | def test_error_try():
61 | with pytest.raises(AttributeError):
70 | def test_error_try():
71 | with pytest.raises(AttributeError):
| _____^
62 | | try:
63 | | [].size
64 | | except:
65 | | raise
72 | | try:
73 | | [].size
74 | | except:
75 | | raise
| |_________________^ PT012
|

Expand Down
Loading