diff options
author | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2023-03-12 13:40:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-12 13:40:16 +0100 |
commit | d6f3ae8d72e65e89560403ca2241ec604b418bdf (patch) | |
tree | 205c4ed9ac8142319c6f98b414f1101ab9acfffc /pylint/checkers | |
parent | 16dd28d1692e097a819d9d4bdf6f80c9744b460a (diff) | |
download | pylint-git-d6f3ae8d72e65e89560403ca2241ec604b418bdf.tar.gz |
Deprecate redundant type checking guard utils (#8433) (#8439)
(cherry picked from commit b968fa062f801626426ef399401c4530e13fcc49)
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Diffstat (limited to 'pylint/checkers')
-rw-r--r-- | pylint/checkers/imports.py | 29 | ||||
-rw-r--r-- | pylint/checkers/utils.py | 15 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 16 |
3 files changed, 40 insertions, 20 deletions
diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index ba526821a..dee12e591 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -21,9 +21,9 @@ from astroid.nodes._base_nodes import ImportNode from pylint.checkers import BaseChecker, DeprecatedMixin from pylint.checkers.utils import ( get_import_name, + in_type_checking_block, is_from_fallback_block, - is_node_in_guarded_import_block, - is_typing_guard, + is_sys_guard, node_ignores_exception, ) from pylint.exceptions import EmptyReportError @@ -154,9 +154,11 @@ def _ignore_import_failure( if submodule in ignored_modules: return True - if is_node_in_guarded_import_block(node): - # Ignore import failure if part of guarded import block - # I.e. `sys.version_info` or `typing.TYPE_CHECKING` + # Ignore import failure if part of guarded import block + # I.e. `sys.version_info` or `typing.TYPE_CHECKING` + if in_type_checking_block(node): + return True + if isinstance(node.parent, nodes.If) and is_sys_guard(node.parent): return True return node_ignores_exception(node, ImportError) @@ -578,7 +580,11 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): current_package and current_package != package and package in met - and is_node_in_guarded_import_block(import_node) is False + and not in_type_checking_block(import_node) + and not ( + isinstance(import_node.parent, nodes.If) + and is_sys_guard(import_node.parent) + ) ): self.add_message("ungrouped-imports", node=import_node, args=package) current_package = package @@ -884,10 +890,6 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): except ImportError: pass - in_type_checking_block = isinstance(node.parent, nodes.If) and is_typing_guard( - node.parent - ) - if context_name == importedmodname: self.add_message("import-self", node=node) @@ -906,10 +908,9 @@ 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) - or in_type_checking_block - ): + if not self.linter.is_message_enabled( + "cyclic-import", line=node.lineno + ) or in_type_checking_block(node): self._excluded_edges[context_name].add(importedmodname) def _check_preferred_module(self, node: ImportNode, mod_path: str) -> None: diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index c68f88388..771b21fff 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -1850,6 +1850,11 @@ def is_typing_guard(node: nodes.If) -> bool: >>> if TYPE_CHECKING: >>> from xyz import a """ + warnings.warn( + "This method will be removed in pylint 3.0; use in_type_checking_block() instead.", + DeprecationWarning, + stacklevel=2, + ) # pragma: no cover return isinstance( node.test, (nodes.Name, nodes.Attribute) ) and node.test.as_string().endswith("TYPE_CHECKING") @@ -1857,6 +1862,11 @@ def is_typing_guard(node: nodes.If) -> bool: def is_node_in_typing_guarded_import_block(node: nodes.NodeNG) -> bool: """Return True if node is part for guarded `typing.TYPE_CHECKING` if block.""" + warnings.warn( + "This method will be removed in pylint 3.0; use in_type_checking_block() instead.", + DeprecationWarning, + stacklevel=2, + ) # pragma: no cover return isinstance(node.parent, nodes.If) and is_typing_guard(node.parent) @@ -1865,6 +1875,11 @@ def is_node_in_guarded_import_block(node: nodes.NodeNG) -> bool: I.e. `sys.version_info` or `typing.TYPE_CHECKING` """ + warnings.warn( + "This method will be removed in pylint 3.0; use in_type_checking_block() instead.", + DeprecationWarning, + stacklevel=2, + ) # pragma: no cover return isinstance(node.parent, nodes.If) and ( is_sys_guard(node.parent) or is_typing_guard(node.parent) ) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 734231103..f55b71264 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -27,6 +27,7 @@ from pylint.checkers import BaseChecker, utils from pylint.checkers.utils import ( in_type_checking_block, is_postponed_evaluation_enabled, + is_sys_guard, ) from pylint.constants import PY39_PLUS, TYPING_NEVER, TYPING_NORETURN from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE @@ -1891,9 +1892,10 @@ class VariablesChecker(BaseChecker): # No need to verify this, since ImportError is already # handled by the client code. return - if utils.is_node_in_guarded_import_block(node) is True: - # Don't verify import if part of guarded import block - # I.e. `sys.version_info` or `typing.TYPE_CHECKING` + # Don't verify import if part of guarded import block + if in_type_checking_block(node): + return + if isinstance(node.parent, nodes.If) and is_sys_guard(node.parent): return for name, _ in node.names: @@ -1913,9 +1915,11 @@ class VariablesChecker(BaseChecker): # No need to verify this, since ImportError is already # handled by the client code. return - if utils.is_node_in_guarded_import_block(node) is True: - # Don't verify import if part of guarded import block - # I.e. `sys.version_info` or `typing.TYPE_CHECKING` + # Don't verify import if part of guarded import block + # I.e. `sys.version_info` or `typing.TYPE_CHECKING` + if in_type_checking_block(node): + return + if isinstance(node.parent, nodes.If) and is_sys_guard(node.parent): return name_parts = node.modname.split(".") |