Skip to content

Commit

Permalink
Fix and test search_ancestor
Browse files Browse the repository at this point in the history
  • Loading branch information
gousaiyang committed May 5, 2021
1 parent f976c88 commit 4398d2a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parso/tree.py
Expand Up @@ -17,7 +17,7 @@ def search_ancestor(node: 'NodeOrLeaf', *node_types: str) -> 'Optional[BaseNode]
"""
warnings.warn("parso.tree.search_ancestor() is deprecated, "
"use NodeOrLeaf.search_ancestor() instead", DeprecationWarning)
return node.search_ancestor()
return node.search_ancestor(*node_types)


class NodeOrLeaf:
Expand Down
27 changes: 27 additions & 0 deletions test/test_parser_tree.py
Expand Up @@ -6,6 +6,7 @@

from parso import parse
from parso.python import tree
from parso.tree import search_ancestor


class TestsFunctionAndLambdaParsing:
Expand Down Expand Up @@ -239,3 +240,29 @@ def test_with_stmt_get_test_node_from_name():
for name in with_stmt.get_defined_names(include_setitem=True)
]
assert tests == ["A", "B", "C", "D"]


sample_module = parse('x + y')
sample_node = sample_module.children[0]
sample_leaf = sample_node.children[0]


@pytest.mark.parametrize(
'node,node_types,expected_ancestor', [
(sample_module, ('file_input',), None),
(sample_node, ('arith_expr',), None),
(sample_node, ('file_input', 'eval_input'), sample_module),
(sample_leaf, ('name',), None),
(sample_leaf, ('arith_expr',), sample_node),
(sample_leaf, ('file_input',), sample_module),
(sample_leaf, ('file_input', 'arith_expr'), sample_node),
(sample_leaf, ('shift_expr',), None),
(sample_leaf, ('name', 'shift_expr',), None),
(sample_leaf, (), None),
]
)
def test_search_ancestor(node, node_types, expected_ancestor):
assert node.search_ancestor(*node_types) is expected_ancestor
with pytest.warns(DeprecationWarning):
ancestor = search_ancestor(node, *node_types)
assert ancestor is expected_ancestor

0 comments on commit 4398d2a

Please sign in to comment.