Skip to content

Commit

Permalink
Deprecate is_inside_lambda from utils (#5447)
Browse files Browse the repository at this point in the history
* Deprecate ``is_inside_lambda`` from utils

* Allow tuple and single type for ``get_node_first_ancestor_of_type``

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
DanielNoord and Pierre-Sassoulas committed Dec 3, 2021
1 parent 765a0b7 commit f89a337
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pylint/checkers/refactoring/recommendation_checker.py
Expand Up @@ -83,7 +83,7 @@ def _check_consider_iterating_dictionary(self, node: nodes.Call) -> None:
return
if node.func.attrname != "keys":
return
comp_ancestor = utils.get_node_first_ancestor_of_type(node, (nodes.Compare,))
comp_ancestor = utils.get_node_first_ancestor_of_type(node, nodes.Compare)
if (
isinstance(node.parent, (nodes.For, nodes.Comprehension))
or comp_ancestor
Expand Down
8 changes: 7 additions & 1 deletion pylint/checkers/utils.py
Expand Up @@ -64,6 +64,7 @@
import numbers
import re
import string
import warnings
from functools import lru_cache, partial
from typing import (
Callable,
Expand Down Expand Up @@ -294,6 +295,11 @@ class InferredTypeError(Exception):

def is_inside_lambda(node: nodes.NodeNG) -> bool:
"""Return whether the given node is inside a lambda"""
warnings.warn(
"utils.is_inside_lambda will be removed in favour of calling "
"utils.get_node_first_ancestor_of_type(x, nodes.Lambda) in pylint 3.0",
DeprecationWarning,
)
return any(isinstance(parent, nodes.Lambda) for parent in node.node_ancestors())


Expand Down Expand Up @@ -1696,7 +1702,7 @@ def returns_bool(node: nodes.NodeNG) -> bool:


def get_node_first_ancestor_of_type(
node: nodes.NodeNG, ancestor_type: Tuple[Type[T_Node]]
node: nodes.NodeNG, ancestor_type: Union[Type[T_Node], Tuple[Type[T_Node]]]
) -> Optional[T_Node]:
"""Return the first parent node that is any of the provided types (or None)"""
for ancestor in node.node_ancestors():
Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/variables.py
Expand Up @@ -1159,7 +1159,7 @@ def _check_consumer(

if (
is_recursive_klass
and utils.is_inside_lambda(node)
and utils.get_node_first_ancestor_of_type(node, nodes.Lambda)
and (
not utils.is_default_argument(node)
or node.scope().parent.scope() is not defframe
Expand Down
7 changes: 7 additions & 0 deletions tests/checkers/unittest_utils.py
Expand Up @@ -486,3 +486,10 @@ def test_is_empty_literal() -> None:
assert utils.is_empty_str_literal(string_node.value)
not_empty_string_node = astroid.extract_node("a = 'hello'")
assert not utils.is_empty_str_literal(not_empty_string_node.value)


def test_deprecation_is_inside_lambda() -> None:
"""Test that is_inside_lambda is throwing a DeprecationWarning"""
with pytest.warns(DeprecationWarning) as records:
utils.is_inside_lambda(nodes.NodeNG())
assert len(records) == 1

0 comments on commit f89a337

Please sign in to comment.