Skip to content

Commit b6fca52

Browse files
dylwil3ntBre
authored andcommitted
[flake8-bugbear] Stabilize support for non-context-manager calls in assert-raises-exception (B017) (#20274)
Introduced in #19063. Removed gating, updated tests. Not documented so docs are the same.
1 parent ac7f882 commit b6fca52

File tree

6 files changed

+39
-151
lines changed

6 files changed

+39
-151
lines changed

crates/ruff_linter/src/checkers/ast/analyze/expression.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use ruff_text_size::Ranged;
88

99
use crate::checkers::ast::Checker;
1010
use crate::preview::{
11-
is_assert_raises_exception_call_enabled, is_optional_as_none_in_union_enabled,
12-
is_unnecessary_default_type_args_stubs_enabled,
11+
is_optional_as_none_in_union_enabled, is_unnecessary_default_type_args_stubs_enabled,
1312
};
1413
use crate::registry::Rule;
1514
use crate::rules::{
@@ -1296,9 +1295,7 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
12961295
if checker.is_rule_enabled(Rule::NonOctalPermissions) {
12971296
ruff::rules::non_octal_permissions(checker, call);
12981297
}
1299-
if checker.is_rule_enabled(Rule::AssertRaisesException)
1300-
&& is_assert_raises_exception_call_enabled(checker.settings())
1301-
{
1298+
if checker.is_rule_enabled(Rule::AssertRaisesException) {
13021299
flake8_bugbear::rules::assert_raises_exception_call(checker, call);
13031300
}
13041301
}

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,6 @@ pub(crate) const fn is_safe_super_call_with_parameters_fix_enabled(
212212
settings.preview.is_enabled()
213213
}
214214

215-
// https://github.com/astral-sh/ruff/pull/19063
216-
pub(crate) const fn is_assert_raises_exception_call_enabled(settings: &LinterSettings) -> bool {
217-
settings.preview.is_enabled()
218-
}
219-
220215
// https://github.com/astral-sh/ruff/pull/19100
221216
pub(crate) const fn is_add_future_annotations_imports_enabled(settings: &LinterSettings) -> bool {
222217
settings.preview.is_enabled()

crates/ruff_linter/src/rules/flake8_bugbear/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ mod tests {
1616
use crate::settings::LinterSettings;
1717
use crate::test::test_path;
1818

19-
use crate::settings::types::PreviewMode;
20-
2119
use ruff_python_ast::PythonVersion;
2220

2321
#[test_case(Rule::AbstractBaseClassWithoutAbstractMethod, Path::new("B024.py"))]
@@ -177,23 +175,4 @@ mod tests {
177175
assert_diagnostics!(snapshot, diagnostics);
178176
Ok(())
179177
}
180-
181-
#[test_case(Rule::AssertRaisesException, Path::new("B017_0.py"))]
182-
#[test_case(Rule::AssertRaisesException, Path::new("B017_1.py"))]
183-
fn rules_preview(rule_code: Rule, path: &Path) -> Result<()> {
184-
let snapshot = format!(
185-
"preview__{}_{}",
186-
rule_code.noqa_code(),
187-
path.to_string_lossy()
188-
);
189-
let diagnostics = test_path(
190-
Path::new("flake8_bugbear").join(path).as_path(),
191-
&LinterSettings {
192-
preview: PreviewMode::Enabled,
193-
..LinterSettings::for_rule(rule_code)
194-
},
195-
)?;
196-
assert_diagnostics!(snapshot, diagnostics);
197-
Ok(())
198-
}
199178
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
33
---
4+
B017 Do not assert blind exception: `Exception`
5+
--> B017_1.py:20:9
6+
|
7+
18 | class Foobar(unittest.TestCase):
8+
19 | def call_form_raises(self) -> None:
9+
20 | self.assertRaises(Exception, something_else)
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
21 | self.assertRaises(BaseException, something_else)
12+
|
413

14+
B017 Do not assert blind exception: `BaseException`
15+
--> B017_1.py:21:9
16+
|
17+
19 | def call_form_raises(self) -> None:
18+
20 | self.assertRaises(Exception, something_else)
19+
21 | self.assertRaises(BaseException, something_else)
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
|
22+
23+
B017 Do not assert blind exception: `Exception`
24+
--> B017_1.py:25:5
25+
|
26+
24 | def test_pytest_call_form() -> None:
27+
25 | pytest.raises(Exception, something_else)
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
26 | pytest.raises(BaseException, something_else)
30+
|
31+
32+
B017 Do not assert blind exception: `BaseException`
33+
--> B017_1.py:26:5
34+
|
35+
24 | def test_pytest_call_form() -> None:
36+
25 | pytest.raises(Exception, something_else)
37+
26 | pytest.raises(BaseException, something_else)
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
27 |
40+
28 | pytest.raises(Exception, something_else, match="hello")
41+
|

crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B017_B017_0.py.snap

Lines changed: 0 additions & 79 deletions
This file was deleted.

crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B017_B017_1.py.snap

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)