summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-12-03 16:06:55 +0100
committerGitHub <noreply@github.com>2021-12-03 16:06:55 +0100
commitf89a3374ec7d49d2a984c90530758a506eaa4384 (patch)
treed4aedc23f5dab203ea919f0549c2d499efb264c0
parent765a0b74bc5f2cface4595661f8832a3aebc68ba (diff)
downloadpylint-git-f89a3374ec7d49d2a984c90530758a506eaa4384.tar.gz
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 <pierre.sassoulas@gmail.com>
-rw-r--r--pylint/checkers/refactoring/recommendation_checker.py2
-rw-r--r--pylint/checkers/utils.py8
-rw-r--r--pylint/checkers/variables.py2
-rw-r--r--tests/checkers/unittest_utils.py7
4 files changed, 16 insertions, 3 deletions
diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py
index 98b2973d8..5424a765c 100644
--- a/pylint/checkers/refactoring/recommendation_checker.py
+++ b/pylint/checkers/refactoring/recommendation_checker.py
@@ -83,7 +83,7 @@ class RecommendationChecker(checkers.BaseChecker):
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 c0bbe918e..8103aea36 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -64,6 +64,7 @@ import itertools
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 ecc425313..f5605260a 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1159,7 +1159,7 @@ class VariablesChecker(BaseChecker):
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 2a4a73f80..ed34a2b06 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