@@ -1215,12 +1215,14 @@ def _get_abs_canonical_name(self, join_str="/") -> str:
1215
1215
assert root_maker , f"root name ctx { self .root } is not assigned to a maker (module)"
1216
1216
if root_maker is self .maker :
1217
1217
return "" # special case
1218
- # Do a breadth-first search through the parents, starting from self.maker, until we find root_maker.
1218
+ # Do a depth-first search through the parents, starting from self.maker, until we find root_maker.
1219
+ # Use depth-first instead of breadth-first to prefer the first parent when there are multiple.
1219
1220
queue = [self .maker ]
1220
1221
cache = {} # maker -> full name
1221
1222
while queue :
1222
- maker = queue .pop (0 )
1223
+ maker = queue .pop (- 1 ) # depth-first
1223
1224
postfix = (join_str + cache [maker ]) if maker in cache else ""
1225
+ queue_ext = []
1224
1226
for parent , attr in maker .parents_with_attr ():
1225
1227
if parent in cache :
1226
1228
continue
@@ -1232,7 +1234,8 @@ def _get_abs_canonical_name(self, join_str="/") -> str:
1232
1234
assert call .layer_abs_name_scope
1233
1235
return call .layer_abs_name_scope + join_str + attr + postfix
1234
1236
cache [parent ] = attr + postfix
1235
- queue .append (parent )
1237
+ queue_ext .append (parent )
1238
+ queue .extend (reversed (queue_ext ))
1236
1239
if root_maker in cache :
1237
1240
break
1238
1241
if root_maker not in cache :
@@ -1316,20 +1319,23 @@ def _get_suggested_name(self) -> str:
1316
1319
reserved_names = set (self .parent .children .keys ()) | self ._ReservedNames
1317
1320
if self .parent .maker :
1318
1321
# Check parent name scope maker, any attrib from there to self.maker.
1319
- # Do a breadth -first search through the parents, starting from self.maker,
1322
+ # Do a depth -first search through the parents, starting from self.maker,
1320
1323
# until we find self.parent.maker.
1324
+ # Somewhat consistent to _get_abs_canonical_name.
1321
1325
queue = [self .maker ]
1322
1326
cache = {} # parent -> full attrib
1323
1327
while queue :
1324
- maker = queue .pop (0 )
1328
+ maker = queue .pop (- 1 ) # depth-first
1325
1329
postfix = f".{ cache [maker ]} " if maker in cache else ""
1330
+ queue_ext = []
1326
1331
for parent , attr in maker .parents_with_attr ():
1327
1332
if parent in cache :
1328
1333
if cache [parent ] in reserved_names :
1329
1334
cache [parent ] = attr + postfix # anyway overwrite
1330
1335
continue
1331
1336
cache [parent ] = attr + postfix
1332
- queue .append (parent )
1337
+ queue_ext .append (parent )
1338
+ queue .extend (reversed (queue_ext ))
1333
1339
if self .parent .maker in cache :
1334
1340
break
1335
1341
if self .parent .maker in cache :
0 commit comments