21
21
from pylint .checkers import BaseChecker , DeprecatedMixin
22
22
from pylint .checkers .utils import (
23
23
get_import_name ,
24
+ in_type_checking_block ,
24
25
is_from_fallback_block ,
25
- is_node_in_guarded_import_block ,
26
- is_typing_guard ,
26
+ is_sys_guard ,
27
27
node_ignores_exception ,
28
28
)
29
29
from pylint .exceptions import EmptyReportError
@@ -154,9 +154,11 @@ def _ignore_import_failure(
154
154
if submodule in ignored_modules :
155
155
return True
156
156
157
- if is_node_in_guarded_import_block (node ):
158
- # Ignore import failure if part of guarded import block
159
- # I.e. `sys.version_info` or `typing.TYPE_CHECKING`
157
+ # Ignore import failure if part of guarded import block
158
+ # I.e. `sys.version_info` or `typing.TYPE_CHECKING`
159
+ if in_type_checking_block (node ):
160
+ return True
161
+ if isinstance (node .parent , nodes .If ) and is_sys_guard (node .parent ):
160
162
return True
161
163
162
164
return node_ignores_exception (node , ImportError )
@@ -578,7 +580,11 @@ def leave_module(self, node: nodes.Module) -> None:
578
580
current_package
579
581
and current_package != package
580
582
and package in met
581
- and is_node_in_guarded_import_block (import_node ) is False
583
+ and not in_type_checking_block (import_node )
584
+ and not (
585
+ isinstance (import_node .parent , nodes .If )
586
+ and is_sys_guard (import_node .parent )
587
+ )
582
588
):
583
589
self .add_message ("ungrouped-imports" , node = import_node , args = package )
584
590
current_package = package
@@ -884,10 +890,6 @@ def _add_imported_module(self, node: ImportNode, importedmodname: str) -> None:
884
890
except ImportError :
885
891
pass
886
892
887
- in_type_checking_block = isinstance (node .parent , nodes .If ) and is_typing_guard (
888
- node .parent
889
- )
890
-
891
893
if context_name == importedmodname :
892
894
self .add_message ("import-self" , node = node )
893
895
@@ -906,10 +908,9 @@ def _add_imported_module(self, node: ImportNode, importedmodname: str) -> None:
906
908
907
909
# update import graph
908
910
self .import_graph [context_name ].add (importedmodname )
909
- if (
910
- not self .linter .is_message_enabled ("cyclic-import" , line = node .lineno )
911
- or in_type_checking_block
912
- ):
911
+ if not self .linter .is_message_enabled (
912
+ "cyclic-import" , line = node .lineno
913
+ ) or in_type_checking_block (node ):
913
914
self ._excluded_edges [context_name ].add (importedmodname )
914
915
915
916
def _check_preferred_module (self , node : ImportNode , mod_path : str ) -> None :
0 commit comments