Skip to content

[deprecation] Remove all duplicated typing guard check functions #8475

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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