From 12acd191e1eb5bba25b4bd2850f45ac5a8a4b9ed Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 17 Sep 2023 11:18:56 -0400 Subject: [PATCH] Remove parentheses when rewriting assert calls to statements (#7464) --- .../fixtures/flake8_pytest_style/PT009.py | 6 +++++ .../flake8_pytest_style/rules/assertion.rs | 9 +++++++- ...es__flake8_pytest_style__tests__PT009.snap | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT009.py b/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT009.py index 128c6b0325729..15863added8e7 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT009.py +++ b/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT009.py @@ -92,3 +92,9 @@ def test_fail_unless_equal(self): def test_fail_if_equal(self): self.failIfEqual(1, 2) # Error + + +# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459517 +(self.assertTrue( + "piAx_piAy_beta[r][x][y] = {17}".format( + self.model.piAx_piAy_beta[r][x][y]))) diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs index e8b5428c13bf3..b3de92653322d 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs @@ -10,6 +10,7 @@ use libcst_native::{ use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::helpers::Truthiness; +use ruff_python_ast::parenthesize::parenthesized_range; use ruff_python_ast::visitor::Visitor; use ruff_python_ast::{ self as ast, Arguments, BoolOp, ExceptHandler, Expr, Keyword, Stmt, UnaryOp, @@ -293,7 +294,13 @@ pub(crate) fn unittest_assertion( if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) { diagnostic.set_fix(Fix::suggested(Edit::range_replacement( checker.generator().stmt(&stmt), - expr.range(), + parenthesized_range( + expr.into(), + checker.semantic().current_statement().into(), + checker.indexer().comment_ranges(), + checker.locator().contents(), + ) + .unwrap_or(expr.range()), ))); } } diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT009.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT009.snap index ff04ae6a491c8..e3c46d173c8f0 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT009.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT009.snap @@ -637,5 +637,27 @@ PT009.py:94:9: PT009 [*] Use a regular `assert` instead of unittest-style `failI 93 93 | def test_fail_if_equal(self): 94 |- self.failIfEqual(1, 2) # Error 94 |+ assert 1 != 2 # Error +95 95 | +96 96 | +97 97 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459517 + +PT009.py:98:2: PT009 [*] Use a regular `assert` instead of unittest-style `assertTrue` + | + 97 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459517 + 98 | (self.assertTrue( + | ^^^^^^^^^^^^^^^ PT009 + 99 | "piAx_piAy_beta[r][x][y] = {17}".format( +100 | self.model.piAx_piAy_beta[r][x][y]))) + | + = help: Replace `assertTrue(...)` with `assert ...` + +ℹ Suggested fix +95 95 | +96 96 | +97 97 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459517 +98 |-(self.assertTrue( +99 |- "piAx_piAy_beta[r][x][y] = {17}".format( +100 |- self.model.piAx_piAy_beta[r][x][y]))) + 98 |+assert "piAx_piAy_beta[r][x][y] = {17}".format(self.model.piAx_piAy_beta[r][x][y])