From 96d2ca0bda8d53e2813c68121dbdf7884cf3a25c Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Wed, 26 Jul 2023 12:45:57 +0900 Subject: [PATCH] Allow pytest.raises body to contain a single func or class definition (#6083) --- .../fixtures/flake8_pytest_style/PT012.py | 10 ++ .../rules/flake8_pytest_style/rules/raises.rs | 2 + ...es__flake8_pytest_style__tests__PT012.snap | 100 +++++++++--------- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT012.py b/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT012.py index 050ad053ec..fefd2942dc 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT012.py +++ b/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT012.py @@ -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([]) diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs index 50766758fc..8b476d4764 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/raises.rs @@ -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 { diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT012.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT012.snap index 7d7bcc3881..d96e181a2e 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT012.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT012.snap @@ -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 |