From f89a3374ec7d49d2a984c90530758a506eaa4384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 3 Dec 2021 16:06:55 +0100 Subject: [PATCH] Deprecate ``is_inside_lambda`` from utils (#5447) * Deprecate ``is_inside_lambda`` from utils * Allow tuple and single type for ``get_node_first_ancestor_of_type`` Co-authored-by: Pierre Sassoulas --- pylint/checkers/refactoring/recommendation_checker.py | 2 +- pylint/checkers/utils.py | 8 +++++++- pylint/checkers/variables.py | 2 +- tests/checkers/unittest_utils.py | 7 +++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py index 98b2973d8e..5424a765ce 100644 --- a/pylint/checkers/refactoring/recommendation_checker.py +++ b/pylint/checkers/refactoring/recommendation_checker.py @@ -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 diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index c0bbe918e8..8103aea36f 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -64,6 +64,7 @@ import numbers import re import string +import warnings from functools import lru_cache, partial from typing import ( Callable, @@ -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()) @@ -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(): diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index ecc425313d..f5605260a4 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -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 diff --git a/tests/checkers/unittest_utils.py b/tests/checkers/unittest_utils.py index 2a4a73f80e..ed34a2b061 100644 --- a/tests/checkers/unittest_utils.py +++ b/tests/checkers/unittest_utils.py @@ -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