Skip to content

Commit 5ba556b

Browse files
committed
flake8-bandit: skip S408/S409 inside TYPE_CHECKING blocks
1 parent d24b56f commit 5ba556b

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
from xml.dom.minidom import Element

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ mod tests {
6767
#[test_case(Rule::SuspiciousXmlExpatImport, Path::new("S407.pyi"))]
6868
#[test_case(Rule::SuspiciousXmlMinidomImport, Path::new("S408.py"))]
6969
#[test_case(Rule::SuspiciousXmlMinidomImport, Path::new("S408.pyi"))]
70+
#[test_case(Rule::SuspiciousXmlMinidomImport, Path::new("S408_type_checking.py"))]
7071
#[test_case(Rule::SuspiciousXmlPulldomImport, Path::new("S409.py"))]
7172
#[test_case(Rule::SuspiciousXmlPulldomImport, Path::new("S409.pyi"))]
7273
#[test_case(Rule::SuspiciousLxmlImport, Path::new("S410.py"))]

crates/ruff_linter/src/rules/flake8_bandit/rules/suspicious_imports.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
370370
return;
371371
}
372372

373+
// Imports inside `if TYPE_CHECKING:` are not executed at runtime.
374+
let is_type_checking_block = checker.semantic().in_type_checking_block();
375+
373376
match stmt {
374377
Stmt::Import(ast::StmtImport { names, .. }) => {
375378
for name in names {
@@ -397,10 +400,16 @@ pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
397400
checker.report_diagnostic_if_enabled(SuspiciousXmlExpatImport, name.range);
398401
}
399402
"xml.dom.minidom" => {
403+
if is_type_checking_block {
404+
continue;
405+
}
400406
checker
401407
.report_diagnostic_if_enabled(SuspiciousXmlMinidomImport, name.range);
402408
}
403409
"xml.dom.pulldom" => {
410+
if is_type_checking_block {
411+
continue;
412+
}
404413
checker
405414
.report_diagnostic_if_enabled(SuspiciousXmlPulldomImport, name.range);
406415
}
@@ -482,12 +491,18 @@ pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
482491
);
483492
}
484493
"minidom" => {
494+
if is_type_checking_block {
495+
continue;
496+
}
485497
checker.report_diagnostic_if_enabled(
486498
SuspiciousXmlMinidomImport,
487499
identifier.range(),
488500
);
489501
}
490502
"pulldom" => {
503+
if is_type_checking_block {
504+
continue;
505+
}
491506
checker.report_diagnostic_if_enabled(
492507
SuspiciousXmlPulldomImport,
493508
identifier.range(),
@@ -502,12 +517,18 @@ pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
502517
.report_diagnostic_if_enabled(SuspiciousXmlExpatImport, identifier.range());
503518
}
504519
"xml.dom.minidom" => {
520+
if is_type_checking_block {
521+
return;
522+
}
505523
checker.report_diagnostic_if_enabled(
506524
SuspiciousXmlMinidomImport,
507525
identifier.range(),
508526
);
509527
}
510528
"xml.dom.pulldom" => {
529+
if is_type_checking_block {
530+
return;
531+
}
511532
checker.report_diagnostic_if_enabled(
512533
SuspiciousXmlPulldomImport,
513534
identifier.range(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
assertion_line: 98
4+
---
5+

0 commit comments

Comments
 (0)