Skip to content

Commit

Permalink
Avoid short circuiting B017 for multiple context managers (#13609)
Browse files Browse the repository at this point in the history
## Summary

fixes: #13603
  • Loading branch information
dhruvmanila authored Oct 3, 2024
1 parent c3b40da commit 7e3894f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ def test_pytest_raises():

with pytest.raises(Exception, match="hello"):
raise ValueError("This is also fine")

with contextlib.nullcontext(), pytest.raises(Exception):
raise ValueError("Multiple context managers")
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,27 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
range: _,
}) = &item.context_expr
else {
return;
continue;
};

if item.optional_vars.is_some() {
return;
continue;
}

let [arg] = &*arguments.args else {
return;
continue;
};

let semantic = checker.semantic();

let Some(builtin_symbol) = semantic.resolve_builtin_symbol(arg) else {
return;
continue;
};

let exception = match builtin_symbol {
"Exception" => ExceptionKind::Exception,
"BaseException" => ExceptionKind::BaseException,
_ => return,
_ => continue,
};

let assertion = if matches!(func.as_ref(), Expr::Attribute(ast::ExprAttribute { attr, .. }) if attr == "assertRaises")
Expand All @@ -117,7 +117,7 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
{
AssertionKind::PytestRaises
} else {
return;
continue;
};

checker.diagnostics.push(Diagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ B017.py:48:10: B017 `pytest.raises(Exception)` should be considered evil
49 | raise ValueError("Hello")
|


B017.py:57:36: B017 `pytest.raises(Exception)` should be considered evil
|
55 | raise ValueError("This is also fine")
56 |
57 | with contextlib.nullcontext(), pytest.raises(Exception):
| ^^^^^^^^^^^^^^^^^^^^^^^^ B017
58 | raise ValueError("Multiple context managers")
|

0 comments on commit 7e3894f

Please sign in to comment.