Skip to content

Commit 5aa1780

Browse files
williambdeanrlouf
authored andcommitted
Add name argument to clone methods via kwargs
1 parent ac5fb05 commit 5aa1780

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

aesara/compile/sharedvalue.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ def zero(self, borrow=False):
167167
else:
168168
self.container.value = 0 * self.container.value
169169

170-
def clone(self):
170+
def clone(self, **kwargs):
171+
name = kwargs.get("name", self.name)
171172
cp = self.__class__(
172-
name=self.name,
173+
name=name,
173174
type=self.type,
174175
value=None,
175176
strict=None,

aesara/graph/basic.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,14 @@ def __repr__(self, firstPass=True):
510510
pass
511511
return "\n".join(to_print)
512512

513-
def clone(self):
513+
def clone(self, **kwargs):
514514
"""Return a new, un-owned `Variable` like `self`.
515515
516+
Parameters
517+
----------
518+
**kwargs : dict
519+
Optional "name" keyword argument for the copied instance. Same as `self.name` if value not provided.
520+
516521
Returns
517522
-------
518523
Variable instance
@@ -523,7 +528,8 @@ def clone(self):
523528
Tags and names are copied to the returned instance.
524529
525530
"""
526-
cp = self.__class__(self.type, None, None, self.name)
531+
name = kwargs.pop("name", self.name)
532+
cp = self.__class__(type=self.type, owner=None, index=None, name=name, **kwargs)
527533
cp.tag = copy(self.tag)
528534
return cp
529535

@@ -621,8 +627,8 @@ def __getstate__(self):
621627
class AtomicVariable(Variable[_TypeType, None]):
622628
"""A node type that has no ancestors and should never be considered an input to a graph."""
623629

624-
def __init__(self, type: _TypeType, **kwargs):
625-
super().__init__(type, None, None, **kwargs)
630+
def __init__(self, type: _TypeType, name: Optional[str] = None, **kwargs):
631+
super().__init__(type=type, owner=None, index=None, name=name, **kwargs)
626632

627633
@abc.abstractmethod
628634
def signature(self):
@@ -656,6 +662,12 @@ def index(self, value):
656662
if value is not None:
657663
raise ValueError("AtomicVariable instances cannot have an index.")
658664

665+
def clone(self, **kwargs):
666+
name = kwargs.pop("name", self.name)
667+
cp = self.__class__(type=self.type, name=name, **kwargs)
668+
cp.tag = copy(self.tag)
669+
return cp
670+
659671

660672
class NominalVariable(AtomicVariable[_TypeType]):
661673
"""A variable that enables alpha-equivalent comparisons."""
@@ -682,12 +694,13 @@ def _str(self):
682694

683695
return cls.__instances__[(typ, id)]
684696

685-
def __init__(self, id: _IdType, typ: _TypeType, **kwargs):
697+
def __init__(self, id: _IdType, typ: _TypeType, name: Optional[str] = None):
686698
self.id = id
687-
super().__init__(typ, **kwargs)
699+
super().__init__(type=typ, name=name)
688700

689-
def clone(self):
690-
return self
701+
def clone(self, **kwargs):
702+
name = kwargs.pop("name", self.name)
703+
return self.__class__(id=self.id, typ=self.type, name=name, **kwargs)
691704

692705
def __eq__(self, other):
693706
if self is other:
@@ -744,8 +757,7 @@ def __str__(self):
744757
name = name[:10] + "..." + name[-10:]
745758
return f"{type(self).__name__}{{{name}}}"
746759

747-
def clone(self):
748-
"""Return `self`, because there's no reason to clone a constant."""
760+
def clone(self, **kwargs):
749761
return self
750762

751763
@property

tests/graph/test_basic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ def test_clone(self):
354354
assert r1.auto_name == "auto_" + str(autoname_id)
355355
assert r2.auto_name == "auto_" + str(autoname_id + 1)
356356

357+
assert r1.name is None and r1.name is r2.name
358+
359+
r3_name = "r3"
360+
r3 = r1.clone(name=r3_name)
361+
assert r3.name == r3_name
362+
357363

358364
def test_equal_computations():
359365

0 commit comments

Comments
 (0)