Skip to content

Commit

Permalink
Remove parentheses when rewriting assert calls to statements (#7464)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 17, 2023
1 parent 64b929b commit 12acd19
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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])))
9 changes: 8 additions & 1 deletion crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])


0 comments on commit 12acd19

Please sign in to comment.