Skip to content

Commit b2f052e

Browse files
committed
Small refactor in deletion code
1 parent 92b604d commit b2f052e

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

dpath/util.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ def f(obj, pair, counter):
116116
key = path_segments[-1]
117117
parent = segments.get(obj, path_segments[:-1])
118118

119-
try:
120-
# Attempt to treat parent like a sequence.
121-
parent[0]
119+
# Deletion behavior depends on parent type
120+
if isinstance(parent, dict):
121+
del parent[key]
122+
123+
else:
124+
# Handle sequence types
125+
# TODO: Consider cases where type isn't a simple list (e.g. set)
122126

123127
if len(parent) - 1 == key:
124128
# Removing the last element of a sequence. It can be
@@ -132,14 +136,12 @@ def f(obj, pair, counter):
132136
# of a list and end up with None values when we
133137
# don't need them.
134138
del parent[key]
139+
135140
else:
136141
# This key can't be removed completely because it
137142
# would affect the order of items that remain in our
138143
# result.
139144
parent[key] = None
140-
except:
141-
# Attempt to treat parent like a dictionary instead.
142-
del parent[key]
143145

144146
counter[0] += 1
145147

0 commit comments

Comments
 (0)