mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
[flake8-bugbear
] Improve assert-raises-exception (B017) message (#15389)
This commit is contained in:
parent
3d9433ca66
commit
23ad319b55
2 changed files with 15 additions and 39 deletions
|
@ -30,33 +30,14 @@ use crate::checkers::ast::Checker;
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(ViolationMetadata)]
|
#[derive(ViolationMetadata)]
|
||||||
pub(crate) struct AssertRaisesException {
|
pub(crate) struct AssertRaisesException {
|
||||||
assertion: AssertionKind,
|
|
||||||
exception: ExceptionKind,
|
exception: ExceptionKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Violation for AssertRaisesException {
|
impl Violation for AssertRaisesException {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
let AssertRaisesException {
|
let AssertRaisesException { exception } = self;
|
||||||
assertion,
|
format!("Do not assert blind exception: `{exception}`")
|
||||||
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"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,24 +88,19 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
let assertion = if matches!(func.as_ref(), Expr::Attribute(ast::ExprAttribute { attr, .. }) if attr == "assertRaises")
|
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::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()
|
|
||||||
{
|
|
||||||
AssertionKind::PytestRaises
|
|
||||||
} else {
|
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
AssertRaisesException {
|
AssertRaisesException { exception },
|
||||||
assertion,
|
|
||||||
exception,
|
|
||||||
},
|
|
||||||
item.range(),
|
item.range(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
|
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
|
||||||
snapshot_kind: text
|
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):
|
21 | class Foobar(unittest.TestCase):
|
||||||
22 | def evil_raises(self) -> None:
|
22 | def evil_raises(self) -> None:
|
||||||
|
@ -11,7 +11,7 @@ B017.py:23:14: B017 `assertRaises(Exception)` should be considered evil
|
||||||
24 | raise Exception("Evil I say!")
|
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:
|
26 | def also_evil_raises(self) -> None:
|
||||||
27 | with self.assertRaises(BaseException):
|
27 | with self.assertRaises(BaseException):
|
||||||
|
@ -19,7 +19,7 @@ B017.py:27:14: B017 `assertRaises(BaseException)` should be considered evil
|
||||||
28 | raise Exception("Evil I say!")
|
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():
|
44 | def test_pytest_raises():
|
||||||
45 | with pytest.raises(Exception):
|
45 | with pytest.raises(Exception):
|
||||||
|
@ -27,7 +27,7 @@ B017.py:45:10: B017 `pytest.raises(Exception)` should be considered evil
|
||||||
46 | raise ValueError("Hello")
|
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")
|
46 | raise ValueError("Hello")
|
||||||
47 |
|
47 |
|
||||||
|
@ -36,7 +36,7 @@ B017.py:48:10: B017 `pytest.raises(Exception)` should be considered evil
|
||||||
49 | raise ValueError("Hello")
|
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")
|
55 | raise ValueError("This is also fine")
|
||||||
56 |
|
56 |
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue