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

[flake8-bugbear] Improve assert-raises-exception (B017) message #15389

Merged
merged 1 commit into from
Jan 10, 2025
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 @@ -30,33 +30,14 @@ use crate::checkers::ast::Checker;
/// ```
#[derive(ViolationMetadata)]
pub(crate) struct AssertRaisesException {
assertion: AssertionKind,
exception: ExceptionKind,
}

impl Violation for AssertRaisesException {
#[derive_message_formats]
fn message(&self) -> String {
let AssertRaisesException {
assertion,
exception,
} = self;
format!("`{assertion}({exception})` should be considered evil")
}
}

#[derive(Debug, PartialEq, Eq)]
enum AssertionKind {
AssertRaises,
PytestRaises,
}

impl fmt::Display for AssertionKind {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
AssertionKind::AssertRaises => fmt.write_str("assertRaises"),
AssertionKind::PytestRaises => fmt.write_str("pytest.raises"),
}
let AssertRaisesException { exception } = self;
format!("Do not assert blind exception: `{exception}`")
}
}

Expand Down Expand Up @@ -107,24 +88,19 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
_ => continue,
};

let assertion = if matches!(func.as_ref(), Expr::Attribute(ast::ExprAttribute { attr, .. }) if attr == "assertRaises")
{
AssertionKind::AssertRaises
} else if semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["pytest", "raises"]))
&& arguments.find_keyword("match").is_none()
if !(matches!(func.as_ref(), Expr::Attribute(ast::ExprAttribute { attr, .. }) if attr == "assertRaises")
|| semantic
.resolve_qualified_name(func)
.is_some_and(|qualified_name| {
matches!(qualified_name.segments(), ["pytest", "raises"])
})
&& arguments.find_keyword("match").is_none())
{
AssertionKind::PytestRaises
} else {
continue;
};

checker.diagnostics.push(Diagnostic::new(
AssertRaisesException {
assertion,
exception,
},
AssertRaisesException { exception },
item.range(),
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
snapshot_kind: text
---
B017.py:23:14: B017 `assertRaises(Exception)` should be considered evil
B017.py:23:14: B017 Do not assert blind exception: `Exception`
|
21 | class Foobar(unittest.TestCase):
22 | def evil_raises(self) -> None:
Expand All @@ -11,23 +11,23 @@ B017.py:23:14: B017 `assertRaises(Exception)` should be considered evil
24 | raise Exception("Evil I say!")
|

B017.py:27:14: B017 `assertRaises(BaseException)` should be considered evil
B017.py:27:14: B017 Do not assert blind exception: `BaseException`
|
26 | def also_evil_raises(self) -> None:
27 | with self.assertRaises(BaseException):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
28 | raise Exception("Evil I say!")
|

B017.py:45:10: B017 `pytest.raises(Exception)` should be considered evil
B017.py:45:10: B017 Do not assert blind exception: `Exception`
|
44 | def test_pytest_raises():
45 | with pytest.raises(Exception):
| ^^^^^^^^^^^^^^^^^^^^^^^^ B017
46 | raise ValueError("Hello")
|

B017.py:48:10: B017 `pytest.raises(Exception)` should be considered evil
B017.py:48:10: B017 Do not assert blind exception: `Exception`
|
46 | raise ValueError("Hello")
47 |
Expand All @@ -36,7 +36,7 @@ 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
B017.py:57:36: B017 Do not assert blind exception: `Exception`
|
55 | raise ValueError("This is also fine")
56 |
Expand Down
Loading