Skip to content

Commit d6f3ae8

Browse files
Deprecate redundant type checking guard utils (#8433) (#8439)
(cherry picked from commit b968fa0) Co-authored-by: Jacob Walls <[email protected]>
1 parent 16dd28d commit d6f3ae8

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-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

+15
Original file line numberDiff line numberDiff line change
@@ -1850,13 +1850,23 @@ def is_typing_guard(node: nodes.If) -> bool:
18501850
>>> if TYPE_CHECKING:
18511851
>>> from xyz import a
18521852
"""
1853+
warnings.warn(
1854+
"This method will be removed in pylint 3.0; use in_type_checking_block() instead.",
1855+
DeprecationWarning,
1856+
stacklevel=2,
1857+
) # pragma: no cover
18531858
return isinstance(
18541859
node.test, (nodes.Name, nodes.Attribute)
18551860
) and node.test.as_string().endswith("TYPE_CHECKING")
18561861

18571862

18581863
def is_node_in_typing_guarded_import_block(node: nodes.NodeNG) -> bool:
18591864
"""Return True if node is part for guarded `typing.TYPE_CHECKING` if block."""
1865+
warnings.warn(
1866+
"This method will be removed in pylint 3.0; use in_type_checking_block() instead.",
1867+
DeprecationWarning,
1868+
stacklevel=2,
1869+
) # pragma: no cover
18601870
return isinstance(node.parent, nodes.If) and is_typing_guard(node.parent)
18611871

18621872

@@ -1865,6 +1875,11 @@ def is_node_in_guarded_import_block(node: nodes.NodeNG) -> bool:
18651875
18661876
I.e. `sys.version_info` or `typing.TYPE_CHECKING`
18671877
"""
1878+
warnings.warn(
1879+
"This method will be removed in pylint 3.0; use in_type_checking_block() instead.",
1880+
DeprecationWarning,
1881+
stacklevel=2,
1882+
) # pragma: no cover
18681883
return isinstance(node.parent, nodes.If) and (
18691884
is_sys_guard(node.parent) or is_typing_guard(node.parent)
18701885
)

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
@@ -414,6 +414,7 @@ def test_if_sys_guard() -> None:
414414
assert utils.is_sys_guard(code[2]) is False
415415

416416

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

0 commit comments

Comments
 (0)