Skip to content

Commit 6d6e35d

Browse files
authored
[flake8-pytest-style] Avoid duplicate PT017 diagnostics (#27918)
Fixes #27870
1 parent 80b4891 commit 6d6e35d

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT017.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ def test_error():
1717
something()
1818
except Exception as e:
1919
assert e.message, "blah blah"
20+
21+
22+
def test_error_with_multiple_exception_references():
23+
try:
24+
something()
25+
except Exception as e:
26+
assert len(e.args) == 1, e.args

‎crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,19 @@ impl Violation for PytestUnittestAssertion {
235235
/// the exception name.
236236
struct ExceptionHandlerVisitor<'a, 'b> {
237237
exception_name: &'a str,
238-
current_assert: Option<&'a Stmt>,
238+
/// The `assert` statement that is currently being visited, if it has not been reported yet.
239+
///
240+
/// This is set to `None` as soon as a diagnostic is reported, so that an `assert` that refers
241+
/// to the exception more than once is only reported once.
242+
pending_assert: Option<&'a Stmt>,
239243
checker: &'a Checker<'b>,
240244
}
241245

242246
impl<'a, 'b> ExceptionHandlerVisitor<'a, 'b> {
243247
const fn new(checker: &'a Checker<'b>, exception_name: &'a str) -> Self {
244248
Self {
245249
exception_name,
246-
current_assert: None,
250+
pending_assert: None,
247251
checker,
248252
}
249253
}
@@ -253,9 +257,9 @@ impl<'a> Visitor<'a> for ExceptionHandlerVisitor<'a, '_> {
253257
fn visit_stmt(&mut self, stmt: &'a Stmt) {
254258
match stmt {
255259
Stmt::Assert(_) => {
256-
self.current_assert = Some(stmt);
260+
self.pending_assert = Some(stmt);
257261
visitor::walk_stmt(self, stmt);
258-
self.current_assert = None;
262+
self.pending_assert = None;
259263
}
260264
_ => visitor::walk_stmt(self, stmt),
261265
}
@@ -264,15 +268,16 @@ impl<'a> Visitor<'a> for ExceptionHandlerVisitor<'a, '_> {
264268
fn visit_expr(&mut self, expr: &'a Expr) {
265269
match expr {
266270
Expr::Name(ast::ExprName { id, .. }) => {
267-
if let Some(current_assert) = self.current_assert {
268-
if id.as_str() == self.exception_name {
269-
self.checker.report_diagnostic(
270-
PytestAssertInExcept {
271-
name: id.to_string(),
272-
},
273-
current_assert.range(),
274-
);
275-
}
271+
if let Some(pending_assert) = self.pending_assert
272+
&& id.as_str() == self.exception_name
273+
{
274+
self.checker.report_diagnostic(
275+
PytestAssertInExcept {
276+
name: id.to_string(),
277+
},
278+
pending_assert.range(),
279+
);
280+
self.pending_assert = None;
276281
}
277282
}
278283
_ => visitor::walk_expr(self, expr),

‎crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT017.snap‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ PT017 Found assertion on exception `e` in `except` block, use `pytest.raises()`
88
18 | except Exception as e:
99
19 | assert e.message, "blah blah"
1010
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
12+
PT017 Found assertion on exception `e` in `except` block, use `pytest.raises()` instead
13+
--> PT017.py:26:9
14+
|
15+
24 | something()
16+
25 | except Exception as e:
17+
26 | assert len(e.args) == 1, e.args
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)