diff --git a/doc/whatsnew/fragments/8475.internal b/doc/whatsnew/fragments/8475.internal new file mode 100644 index 0000000000..0451554e91 --- /dev/null +++ b/doc/whatsnew/fragments/8475.internal @@ -0,0 +1,5 @@ +Following a deprecation period, ``is_typing_guard``, ``is_node_in_typing_guarded_import_block`` and +``is_node_in_guarded_import_block``: from ``pylint.utils`` were removed: use a combination of +``is_sys_guard`` and ``in_type_checking_block`` instead. + +Refs #8475 diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index c99c904570..503e5d4195 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -12,7 +12,6 @@ import numbers import re import string -import warnings from collections import deque from collections.abc import Iterable, Iterator from functools import lru_cache, partial @@ -1792,48 +1791,6 @@ def is_sys_guard(node: nodes.If) -> bool: return False -def is_typing_guard(node: nodes.If) -> bool: - """Return True if IF stmt is a typing guard. - - >>> from typing import TYPE_CHECKING - >>> 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") - - -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) - - -def is_node_in_guarded_import_block(node: nodes.NodeNG) -> bool: - """Return True if node is part for guarded if block. - - 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) - ) - - def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool: """Check if the given variable name is reassigned in the same scope after the current node. diff --git a/tests/checkers/unittest_utils.py b/tests/checkers/unittest_utils.py index 196b8b5b07..f9178ce86e 100644 --- a/tests/checkers/unittest_utils.py +++ b/tests/checkers/unittest_utils.py @@ -413,7 +413,6 @@ def test_if_sys_guard() -> None: assert utils.is_sys_guard(code[2]) is False -@pytest.mark.filterwarnings("ignore::DeprecationWarning") def test_if_typing_guard() -> None: code = astroid.extract_node( """ @@ -421,30 +420,30 @@ def test_if_typing_guard() -> None: import typing as t from typing import TYPE_CHECKING - if typing.TYPE_CHECKING: #@ - pass + if typing.TYPE_CHECKING: + pass #@ - if t.TYPE_CHECKING: #@ - pass + if t.TYPE_CHECKING: + pass #@ - if TYPE_CHECKING: #@ - pass + if TYPE_CHECKING: + pass #@ - if typing.SOME_OTHER_CONST: #@ - pass + if typing.SOME_OTHER_CONST: + pass #@ """ ) assert isinstance(code, list) and len(code) == 4 - assert isinstance(code[0], nodes.If) - assert utils.is_typing_guard(code[0]) is True - assert isinstance(code[1], nodes.If) - assert utils.is_typing_guard(code[1]) is True - assert isinstance(code[2], nodes.If) - assert utils.is_typing_guard(code[2]) is True + assert isinstance(code[0], nodes.Pass) + assert utils.in_type_checking_block(code[0]) is True + assert isinstance(code[1], nodes.Pass) + assert utils.in_type_checking_block(code[1]) is True + assert isinstance(code[2], nodes.Pass) + assert utils.in_type_checking_block(code[2]) is True - assert isinstance(code[3], nodes.If) - assert utils.is_typing_guard(code[3]) is False + assert isinstance(code[3], nodes.Pass) + assert utils.in_type_checking_block(code[3]) is False def test_in_type_checking_block() -> None: