Skip to content

Commit

Permalink
[deprecation] Remove duplicated utils typing guards check (#8475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas authored Mar 22, 2023
1 parent 1f2cf71 commit 25406f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 60 deletions.
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/8475.internal
Original file line number Diff line number Diff line change
@@ -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
43 changes: 0 additions & 43 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
33 changes: 16 additions & 17 deletions tests/checkers/unittest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,38 +413,37 @@ 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(
"""
import typing
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:
Expand Down

0 comments on commit 25406f7

Please sign in to comment.