diff options
author | Marc Mueller <30130371+cdce8p@users.noreply.github.com> | 2021-07-19 21:57:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-19 21:57:12 +0200 |
commit | 6a166250526b1c6da25c639327197e3ad05709f4 (patch) | |
tree | fd52e0f5ed6e3a6f9c9a6396f68ed1fa3e5c36e9 /pylint/checkers/imports.py | |
parent | 69504647937092e9f5d9dcc645fb12cfd5633b6a (diff) | |
download | pylint-git-6a166250526b1c6da25c639327197e3ad05709f4.tar.gz |
Fix cyclic import with TYPE_CHECKING (#4703)
* Fix cyclic-import with TYPE_CHECKING
* Use new astroid helper method
Diffstat (limited to 'pylint/checkers/imports.py')
-rw-r--r-- | pylint/checkers/imports.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index 090b24057..4be36ed71 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -813,7 +813,9 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): self.add_message("import-error", args=repr(dotted_modname), node=importnode) return None - def _add_imported_module(self, node, importedmodname): + def _add_imported_module( + self, node: Union[astroid.Import, astroid.ImportFrom], importedmodname: str + ) -> None: """notify an imported module, used to analyze dependencies""" module_file = node.root().file context_name = node.root().name @@ -826,6 +828,10 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): except ImportError: pass + in_type_checking_block = ( + isinstance(node.parent, astroid.If) and node.parent.is_typing_guard() + ) + if context_name == importedmodname: self.add_message("import-self", node=node) @@ -845,7 +851,10 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): # update import graph self.import_graph[context_name].add(importedmodname) - if not self.linter.is_message_enabled("cyclic-import", line=node.lineno): + if ( + not self.linter.is_message_enabled("cyclic-import", line=node.lineno) + or in_type_checking_block + ): self._excluded_edges[context_name].add(importedmodname) def _check_preferred_module(self, node, mod_path): |