From 35b4466bcbc0690ee9c534067ee5041e416c97ff Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Mon, 15 Jul 2024 02:45:05 -0700 Subject: [PATCH] Avoid use of global variable. PiperOrigin-RevId: 652414420 --- pytype/pytd/parse/node.py | 64 ++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/pytype/pytd/parse/node.py b/pytype/pytd/parse/node.py index f51ba3aef..7a8e4336d 100644 --- a/pytype/pytd/parse/node.py +++ b/pytype/pytd/parse/node.py @@ -99,36 +99,32 @@ def Visit(self, visitor, *args, **kwargs): Returns: Transformed version of this node. """ - return _Visit(self, visitor, *args, **kwargs) + return _Visit(self, visitor, set(), *args, **kwargs) def Replace(self, **kwargs): return msgspec.structs.replace(self, **kwargs) -# The set of visitor names currently being processed. -_visiting = set() - - -def _Visit(node, visitor, *args, **kwargs): +def _Visit(node, visitor, visiting, *args, **kwargs): """Visit the node.""" name = type(visitor).__name__ - recursive = name in _visiting - _visiting.add(name) + recursive = name in visiting + visiting.add(name) start = metrics.get_cpu_clock() try: - return _VisitNode(node, visitor, *args, **kwargs) + return _VisitNode(node, visitor, visiting, *args, **kwargs) finally: if not recursive: - _visiting.remove(name) + visiting.remove(name) elapsed = metrics.get_cpu_clock() - start metrics.get_metric("visit_" + name, metrics.Distribution).add(elapsed) - if _visiting: + if visiting: metrics.get_metric( "visit_nested_" + name, metrics.Distribution).add(elapsed) -def _VisitNode(node, visitor, *args, **kwargs): +def _VisitNode(node, visitor, visiting, *args, **kwargs): """Transform a node and all its children using a visitor. This will iterate over all children of this node, and also process certain @@ -136,29 +132,29 @@ def _VisitNode(node, visitor, *args, **kwargs): elements visited, or primitive types, which will be returned as-is. Args: - node: The node to transform. Either an actual instance of Node, or a - tuple found while scanning a node tree, or any other type (which will - be returned unmodified). + node: The node to transform. Either an actual instance of Node, or a tuple + found while scanning a node tree, or any other type (which will be + returned unmodified). visitor: The visitor to apply. If this visitor has a "Visit" method, - with the name of the Node class, a callback will be triggered, - and the transformed version of this node will be whatever the callback - returned. Before calling the Visit callback, the following - attribute(s) on the Visitor class will be populated: - visitor.old_node: The node before the child nodes were visited. - - Additionally, if the visitor has a "Enter" method, that method - will be called on the original node before descending into it. If - "Enter" returns False, the visitor will not visit children of - this node. If "Enter" returns a set of field names, those field - names will not be visited. Otherwise, "Enter" should return - None, to indicate that nodes will be visited normally. - - "Enter" is called pre-order; "Visit and "Leave" are - called post-order. A counterpart to "Enter" is "Leave", - which is intended for any clean-up that "Enter" needs (other - than that, it's redundant, and could be combined with "Visit"). + with the name of the Node class, a callback will be triggered, and + the transformed version of this node will be whatever the callback + returned. Before calling the Visit callback, the following attribute(s) + on the Visitor class will be populated: visitor.old_node: The node before + the child nodes were visited. Additionally, if the visitor has a + "Enter" method, that method will be called on the original node + before descending into it. If "Enter" returns False, the visitor + will not visit children of this node. If "Enter" returns a set of + field names, those field names will not be visited. Otherwise, + "Enter" should return None, to indicate that nodes will be visited + normally. "Enter" is called pre-order; "Visit and + "Leave" are called post-order. A counterpart to "Enter" is + "Leave", which is intended for any clean-up that "Enter" needs + (other than that, it's redundant, and could be combined with + "Visit"). + visiting: The set of visitor names currently being processed. *args: Passed to visitor callbacks. **kwargs: Passed to visitor callbacks. + Returns: The transformed Node (which *may* be the original node but could be a new node, even if the contents are the same). @@ -168,7 +164,7 @@ def _VisitNode(node, visitor, *args, **kwargs): changed = False new_children = [] for child in node: - new_child = _VisitNode(child, visitor, *args, **kwargs) + new_child = _VisitNode(child, visitor, visiting, *args, **kwargs) if new_child is not child: changed = True new_children.append(new_child) @@ -209,7 +205,7 @@ def _VisitNode(node, visitor, *args, **kwargs): if name in skip_children: new_child = child else: - new_child = _VisitNode(child, visitor, *args, **kwargs) + new_child = _VisitNode(child, visitor, visiting, *args, **kwargs) if new_child is not child: changed = True new_children.append(new_child)