Skip to content

Commit 7e06bb6

Browse files
committed
visitor: Remove visitor_keys argument
Replicates graphql/graphql-js@b67ed17
1 parent 3855d79 commit 7e06bb6

File tree

2 files changed

+3
-53
lines changed

2 files changed

+3
-53
lines changed

src/graphql/language/visitor.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ class Stack(NamedTuple):
213213
prev: Any # 'Stack' (python/mypy/issues/731)
214214

215215

216-
def visit(
217-
root: Node,
218-
visitor: Visitor,
219-
visitor_keys: Optional[Dict[str, Tuple[str, ...]]] = None,
220-
) -> Any:
216+
def visit(root: Node, visitor: Visitor) -> Any:
221217
"""Visit each node in an AST.
222218
223219
:func:`~.visit` will walk through an AST using a depth-first traversal, calling the
@@ -232,16 +228,11 @@ def visit(
232228
When using :func:`~.visit` to edit an AST, the original AST will not be modified,
233229
and a new version of the AST with the changes applied will be returned from the
234230
visit function.
235-
236-
To customize the node attributes to be used for traversal, you can provide a
237-
dictionary visitor_keys mapping node kinds to node attributes.
238231
"""
239232
if not isinstance(root, Node):
240233
raise TypeError(f"Not an AST Node: {inspect(root)}.")
241234
if not isinstance(visitor, Visitor):
242235
raise TypeError(f"Not an AST Visitor: {inspect(visitor)}.")
243-
if visitor_keys is None:
244-
visitor_keys = QUERY_DOCUMENT_KEYS
245236
stack: Any = None
246237
in_array = isinstance(root, list)
247238
keys: Tuple[Node, ...] = (root,)
@@ -340,7 +331,7 @@ def visit(
340331
else:
341332
stack = Stack(in_array, idx, keys, edits, stack)
342333
in_array = isinstance(node, list)
343-
keys = node if in_array else visitor_keys.get(node.kind, ())
334+
keys = node if in_array else QUERY_DOCUMENT_KEYS.get(node.kind, ())
344335
idx = -1
345336
edits = []
346337
if parent:

tests/language/test_visitor.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from copy import copy
22
from functools import partial
3-
from typing import cast, Dict, List, Optional, Tuple
3+
from typing import cast, List, Optional
44

55
from pytest import mark, raises
66

@@ -18,7 +18,6 @@
1818
ParallelVisitor,
1919
Visitor,
2020
)
21-
from graphql.language.visitor import QUERY_DOCUMENT_KEYS
2221
from graphql.pyutils import FrozenList
2322

2423
from ..fixtures import kitchen_sink_query # noqa: F401
@@ -1019,46 +1018,6 @@ def leave(node, *_args):
10191018
["leave", "document", None],
10201019
]
10211020

1022-
def does_traverse_unknown_node_with_visitor_keys():
1023-
custom_query_document_keys: Dict[str, Tuple[str, ...]] = {
1024-
**QUERY_DOCUMENT_KEYS,
1025-
"custom_field": ("name", "selection_set"),
1026-
}
1027-
visited = []
1028-
1029-
class TestVisitor(Visitor):
1030-
@staticmethod
1031-
def enter(node, *_args):
1032-
visited.append(["enter", node.kind, get_value(node)])
1033-
1034-
@staticmethod
1035-
def leave(node, *_args):
1036-
visited.append(["leave", node.kind, get_value(node)])
1037-
1038-
visit(custom_ast, TestVisitor(), custom_query_document_keys)
1039-
assert visited == [
1040-
["enter", "document", None],
1041-
["enter", "operation_definition", None],
1042-
["enter", "selection_set", None],
1043-
["enter", "field", None],
1044-
["enter", "name", "a"],
1045-
["leave", "name", "a"],
1046-
["leave", "field", None],
1047-
["enter", "custom_field", None],
1048-
["enter", "name", "b"],
1049-
["leave", "name", "b"],
1050-
["enter", "selection_set", None],
1051-
["enter", "custom_field", None],
1052-
["enter", "name", "c"],
1053-
["leave", "name", "c"],
1054-
["leave", "custom_field", None],
1055-
["leave", "selection_set", None],
1056-
["leave", "custom_field", None],
1057-
["leave", "selection_set", None],
1058-
["leave", "operation_definition", None],
1059-
["leave", "document", None],
1060-
]
1061-
10621021
def cannot_define_visitor_with_unknown_ast_nodes():
10631022
with raises(TypeError) as exc_info:
10641023

0 commit comments

Comments
 (0)