Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't raise PT017 if exception is used in assertion message #11873

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def test_ok():
assert e.value.message


def allow_exception_in_message():
x = 0
try:
1 / x
except ZeroDivisionError as e:
assert x == 0, e
Comment on lines +15 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this feels like exactly the kind of thing the rule is trying to catch, rather than something we should avoid emitting a diagnostic for: I commented at #11869 (comment). If you want to use the value of the exception in the assertion message, you can do that using pytest.raises():

def allow_exception_in_message():
    x = 0
    with pytest.raises(ZeroDivisionError) as exc_info:
        1 / x
    assert x == 0, exc_info.value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards agreeing with Alex here



def test_error():
try:
something()
Expand Down
28 changes: 16 additions & 12 deletions crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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,
self as ast, Arguments, BoolOp, ExceptHandler, Expr, Keyword, Stmt, StmtAssert, UnaryOp,
};
use ruff_python_ast::{visitor, whitespace};
use ruff_python_codegen::Stylist;
Expand Down Expand Up @@ -226,27 +226,31 @@ impl<'a> ExceptionHandlerVisitor<'a> {
impl<'a> Visitor<'a> for ExceptionHandlerVisitor<'a> {
fn visit_stmt(&mut self, stmt: &'a Stmt) {
match stmt {
Stmt::Assert(_) => {
Stmt::Assert(StmtAssert { test, .. }) => {
self.current_assert = Some(stmt);
visitor::walk_stmt(self, stmt);

self.visit_expr(test);

self.current_assert = None;
}
_ => visitor::walk_stmt(self, stmt),
}
}

fn visit_expr(&mut self, expr: &'a Expr) {
let Some(current_assert) = self.current_assert else {
return;
};

match expr {
Expr::Name(ast::ExprName { id, .. }) => {
if let Some(current_assert) = self.current_assert {
if id.as_str() == self.exception_name {
self.errors.push(Diagnostic::new(
PytestAssertInExcept {
name: id.to_string(),
},
current_assert.range(),
));
}
if id.as_str() == self.exception_name {
self.errors.push(Diagnostic::new(
PytestAssertInExcept {
name: id.to_string(),
},
current_assert.range(),
));
}
}
_ => visitor::walk_expr(self, expr),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
---
PT017.py:19:9: PT017 Found assertion on exception `e` in `except` block, use `pytest.raises()` instead
PT017.py:27:9: PT017 Found assertion on exception `e` in `except` block, use `pytest.raises()` instead
|
17 | something()
18 | except Exception as e:
19 | assert e.message, "blah blah"
25 | something()
26 | except Exception as e:
27 | assert e.message, "blah blah"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT017
|


Loading