Refs #32948 -- Simplified WhereNode and Node.__deepcopy__()/add().

We can use copy() in Node.add() instead of create() as we don't need the
children to be cloned via [:] subscript in __init__().
This commit is contained in:
Nick Pope 2021-09-20 23:38:47 +01:00 committed by Mariusz Felisiak
parent ddf0002bb7
commit ed9eca8457
2 changed files with 7 additions and 13 deletions

View File

@ -67,12 +67,12 @@ class WhereNode(tree.Node):
else: else:
where_parts.append(c) where_parts.append(c)
having_node = ( having_node = (
self.__class__(having_parts, self.connector, self.negated) self.create(having_parts, self.connector, self.negated)
if having_parts if having_parts
else None else None
) )
where_node = ( where_node = (
self.__class__(where_parts, self.connector, self.negated) self.create(where_parts, self.connector, self.negated)
if where_parts if where_parts
else None else None
) )
@ -171,16 +171,11 @@ class WhereNode(tree.Node):
self.children[pos] = child.relabeled_clone(change_map) self.children[pos] = child.relabeled_clone(change_map)
def clone(self): def clone(self):
clone = self.__class__.create( clone = self.create(connector=self.connector, negated=self.negated)
children=None,
connector=self.connector,
negated=self.negated,
)
for child in self.children: for child in self.children:
if hasattr(child, "clone"): if hasattr(child, "clone"):
clone.children.append(child.clone()) child = child.clone()
else: clone.children.append(child)
clone.children.append(child)
return clone return clone
def relabeled_clone(self, change_map): def relabeled_clone(self, change_map):

View File

@ -45,8 +45,7 @@ class Node:
return "<%s: %s>" % (self.__class__.__name__, self) return "<%s: %s>" % (self.__class__.__name__, self)
def __deepcopy__(self, memodict): def __deepcopy__(self, memodict):
obj = Node(connector=self.connector, negated=self.negated) obj = self.create(connector=self.connector, negated=self.negated)
obj.__class__ = self.__class__
obj.children = copy.deepcopy(self.children, memodict) obj.children = copy.deepcopy(self.children, memodict)
return obj return obj
@ -93,7 +92,7 @@ class Node:
node other got squashed or not. node other got squashed or not.
""" """
if self.connector != conn_type: if self.connector != conn_type:
obj = self.create(self.children, self.connector, self.negated) obj = self.copy()
self.connector = conn_type self.connector = conn_type
self.children = [obj, data] self.children = [obj, data]
return data return data