Skip to content

Commit cb83100

Browse files
committed
installer: do the _normalize_structure copy more manually
This is a preparation for type hint addition, where we cannot mutate in-place a variable to another type: we have to build an object of the correct return type incrementally. Signed-off-by: Yann Dirson <[email protected]>
1 parent 0e2304f commit cb83100

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

lib/installer.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,28 @@ def top_setattr(self, attrs):
3434
def _normalize_structure(defn):
3535
assert isinstance(defn, dict), f"{defn!r} is not a dict"
3636
assert 'TAG' in defn, f"{defn} has no TAG"
37-
defn = dict(defn)
38-
if 'CONTENTS' not in defn:
39-
defn['CONTENTS'] = []
40-
if not isinstance(defn['CONTENTS'], str):
41-
defn['CONTENTS'] = [AnswerFile._normalize_structure(item)
42-
for item in defn['CONTENTS']]
43-
return defn
37+
38+
# type mutation through nearly-shallow copy
39+
new_defn = {
40+
'TAG': defn['TAG'],
41+
'CONTENTS': [],
42+
}
43+
for key, value in defn.items():
44+
if key == 'CONTENTS':
45+
if isinstance(value, str):
46+
new_defn['CONTENTS'] = value
47+
else:
48+
new_defn['CONTENTS'] = [
49+
AnswerFile._normalize_structure(item)
50+
for item in value
51+
if item is not None
52+
]
53+
elif key == 'TAG':
54+
pass # already copied
55+
else:
56+
new_defn[key] = value
57+
58+
return new_defn
4459

4560
# convert to a ElementTree.Element tree suitable for further
4661
# modification before we serialize it to XML

0 commit comments

Comments
 (0)