Skip to content

Commit b968fa0

Browse files
Deprecate redundant type checking guard utils (#8433)
1 parent b312b9a commit b968fa0

File tree

6 files changed

+54
-22
lines changed

6 files changed

+54
-22
lines changed

doc/whatsnew/fragments/8433.internal

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The following utilities are deprecated in favor of the more robust ``in_type_checking_block``
2+
and will be removed in pylint 3.0:
3+
4+
- ``is_node_in_guarded_import_block``
5+
- ``is_node_in_typing_guarded_import_block``
6+
- ``is_typing_guard``
7+
8+
``is_sys_guard`` is still available, which was part of ``is_node_in_guarded_import_block``.
9+
10+
Refs #8433

pylint/checkers/imports.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from pylint.checkers import BaseChecker, DeprecatedMixin
2222
from pylint.checkers.utils import (
2323
get_import_name,
24+
in_type_checking_block,
2425
is_from_fallback_block,
25-
is_node_in_guarded_import_block,
26-
is_typing_guard,
26+
is_sys_guard,
2727
node_ignores_exception,
2828
)
2929
from pylint.exceptions import EmptyReportError
@@ -154,9 +154,11 @@ def _ignore_import_failure(
154154
if submodule in ignored_modules:
155155
return True
156156

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):
160162
return True
161163

162164
return node_ignores_exception(node, ImportError)
@@ -578,7 +580,11 @@ def leave_module(self, node: nodes.Module) -> None:
578580
current_package
579581
and current_package != package
580582
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+
)
582588
):
583589
self.add_message("ungrouped-imports", node=import_node, args=package)
584590
current_package = package
@@ -884,10 +890,6 @@ def _add_imported_module(self, node: ImportNode, importedmodname: str) -> None:
884890
except ImportError:
885891
pass
886892

887-
in_type_checking_block = isinstance(node.parent, nodes.If) and is_typing_guard(
888-
node.parent
889-
)
890-
891893
if context_name == importedmodname:
892894
self.add_message("import-self", node=node)
893895

@@ -906,10 +908,9 @@ def _add_imported_module(self, node: ImportNode, importedmodname: str) -> None:
906908

907909
# update import graph
908910
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):
913914
self._excluded_edges[context_name].add(importedmodname)
914915

915916
def _check_preferred_module(self, node: ImportNode, mod_path: str) -> None:

pylint/checkers/utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numbers
1313
import re
1414
import string
15+
import warnings
1516
from collections import deque
1617
from collections.abc import Iterable, Iterator
1718
from functools import lru_cache, partial
@@ -1798,13 +1799,23 @@ def is_typing_guard(node: nodes.If) -> bool:
17981799
>>> if TYPE_CHECKING:
17991800
>>> from xyz import a
18001801
"""
1802+
warnings.warn(
1803+
"This method will be removed in pylint 3.0; use in_type_checking_block() instead.",
1804+
DeprecationWarning,
1805+
stacklevel=2,
1806+
) # pragma: no cover
18011807
return isinstance(
18021808
node.test, (nodes.Name, nodes.Attribute)
18031809
) and node.test.as_string().endswith("TYPE_CHECKING")
18041810

18051811

18061812
def is_node_in_typing_guarded_import_block(node: nodes.NodeNG) -> bool:
18071813
"""Return True if node is part for guarded `typing.TYPE_CHECKING` if block."""
1814+
warnings.warn(
1815+
"This method will be removed in pylint 3.0; use in_type_checking_block() instead.",
1816+
DeprecationWarning,
1817+
stacklevel=2,
1818+
) # pragma: no cover
18081819
return isinstance(node.parent, nodes.If) and is_typing_guard(node.parent)
18091820

18101821

@@ -1813,6 +1824,11 @@ def is_node_in_guarded_import_block(node: nodes.NodeNG) -> bool:
18131824
18141825
I.e. `sys.version_info` or `typing.TYPE_CHECKING`
18151826
"""
1827+
warnings.warn(
1828+
"This method will be removed in pylint 3.0; use in_type_checking_block() instead.",
1829+
DeprecationWarning,
1830+
stacklevel=2,
1831+
) # pragma: no cover
18161832
return isinstance(node.parent, nodes.If) and (
18171833
is_sys_guard(node.parent) or is_typing_guard(node.parent)
18181834
)

pylint/checkers/variables.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from pylint.checkers.utils import (
2828
in_type_checking_block,
2929
is_postponed_evaluation_enabled,
30+
is_sys_guard,
3031
)
3132
from pylint.constants import PY39_PLUS, TYPING_NEVER, TYPING_NORETURN
3233
from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE
@@ -1891,9 +1892,10 @@ def visit_import(self, node: nodes.Import) -> None:
18911892
# No need to verify this, since ImportError is already
18921893
# handled by the client code.
18931894
return
1894-
if utils.is_node_in_guarded_import_block(node) is True:
1895-
# Don't verify import if part of guarded import block
1896-
# I.e. `sys.version_info` or `typing.TYPE_CHECKING`
1895+
# Don't verify import if part of guarded import block
1896+
if in_type_checking_block(node):
1897+
return
1898+
if isinstance(node.parent, nodes.If) and is_sys_guard(node.parent):
18971899
return
18981900

18991901
for name, _ in node.names:
@@ -1913,9 +1915,11 @@ def visit_importfrom(self, node: nodes.ImportFrom) -> None:
19131915
# No need to verify this, since ImportError is already
19141916
# handled by the client code.
19151917
return
1916-
if utils.is_node_in_guarded_import_block(node) is True:
1917-
# Don't verify import if part of guarded import block
1918-
# I.e. `sys.version_info` or `typing.TYPE_CHECKING`
1918+
# Don't verify import if part of guarded import block
1919+
# I.e. `sys.version_info` or `typing.TYPE_CHECKING`
1920+
if in_type_checking_block(node):
1921+
return
1922+
if isinstance(node.parent, nodes.If) and is_sys_guard(node.parent):
19191923
return
19201924

19211925
name_parts = node.modname.split(".")

pylint/extensions/private_import.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, linter: PyLinter) -> None:
3939

4040
@utils.only_required_for_messages("import-private-name")
4141
def visit_import(self, node: nodes.Import) -> None:
42-
if utils.is_node_in_typing_guarded_import_block(node):
42+
if utils.in_type_checking_block(node):
4343
return
4444
names = [name[0] for name in node.names]
4545
private_names = self._get_private_imports(names)
@@ -56,7 +56,7 @@ def visit_import(self, node: nodes.Import) -> None:
5656

5757
@utils.only_required_for_messages("import-private-name")
5858
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
59-
if utils.is_node_in_typing_guarded_import_block(node):
59+
if utils.in_type_checking_block(node):
6060
return
6161
# Only check imported names if the module is external
6262
if self.same_root_dir(node, node.modname):

tests/checkers/unittest_utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ def test_if_sys_guard() -> None:
413413
assert utils.is_sys_guard(code[2]) is False
414414

415415

416+
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
416417
def test_if_typing_guard() -> None:
417418
code = astroid.extract_node(
418419
"""

0 commit comments

Comments
 (0)