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

Avoid short circuiting B017 for multiple context managers #13609

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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 @@ -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")
|
Loading