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

This commit is contained in:
Harutaka Kawamura 2023-07-26 12:45:57 +09:00 committed by GitHub
parent 62f821daaa
commit 96d2ca0bda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 50 deletions

View file

@ -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(): def test_error_multiple_statements():
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
len([]) len([])

View file

@ -135,6 +135,8 @@ pub(crate) fn complex_raises(
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => { | Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
is_non_trivial_with_body(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), stmt => is_compound_statement(stmt),
} }
} else { } else {

View file

@ -1,88 +1,88 @@
--- ---
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs 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(): 41 | def test_error_multiple_statements():
32 | with pytest.raises(AttributeError): 42 | with pytest.raises(AttributeError):
| _____^ | _____^
33 | | len([]) 43 | | len([])
34 | | [].size 44 | | [].size
| |_______________^ PT012 | |_______________^ 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(): 47 | async def test_error_complex_statement():
38 | with pytest.raises(AttributeError): 48 | with pytest.raises(AttributeError):
| _____^ | _____^
39 | | if True: 49 | | if True:
40 | | [].size 50 | | [].size
| |___________________^ PT012 | |___________________^ PT012
41 | 51 |
42 | with pytest.raises(AttributeError): 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 50 | [].size
41 | 51 |
42 | with pytest.raises(AttributeError): 52 | with pytest.raises(AttributeError):
| _____^ | _____^
43 | | for i in []: 53 | | for i in []:
44 | | [].size 54 | | [].size
| |___________________^ PT012 | |___________________^ PT012
45 | 55 |
46 | with pytest.raises(AttributeError): 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 54 | [].size
45 | 55 |
46 | with pytest.raises(AttributeError): 56 | with pytest.raises(AttributeError):
| _____^ | _____^
47 | | async for i in []: 57 | | async for i in []:
48 | | [].size 58 | | [].size
| |___________________^ PT012 | |___________________^ PT012
49 | 59 |
50 | with pytest.raises(AttributeError): 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 58 | [].size
49 | 59 |
50 | with pytest.raises(AttributeError): 60 | with pytest.raises(AttributeError):
| _____^ | _____^
51 | | while True: 61 | | while True:
52 | | [].size 62 | | [].size
| |___________________^ PT012 | |___________________^ PT012
53 | 63 |
54 | with pytest.raises(AttributeError): 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 62 | [].size
53 | 63 |
54 | with pytest.raises(AttributeError): 64 | with pytest.raises(AttributeError):
| _____^ | _____^
55 | | async with context_manager_under_test(): 65 | | async with context_manager_under_test():
56 | | if True: 66 | | if True:
57 | | raise Exception 67 | | raise Exception
| |_______________________________^ PT012 | |_______________________________^ 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(): 70 | def test_error_try():
61 | with pytest.raises(AttributeError): 71 | with pytest.raises(AttributeError):
| _____^ | _____^
62 | | try: 72 | | try:
63 | | [].size 73 | | [].size
64 | | except: 74 | | except:
65 | | raise 75 | | raise
| |_________________^ PT012 | |_________________^ PT012
| |