@@ -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 ( ) ,
0 commit comments