1
1
from copy import deepcopy
2
2
from fnmatch import fnmatchcase
3
- from typing import List , Sequence , Tuple , Iterator , Any , Union , Optional , MutableMapping
3
+ from typing import Sequence , Tuple , Iterator , Any , Union , Optional , MutableMapping , MutableSequence
4
4
5
5
from dpath import options
6
6
from dpath .exceptions import InvalidGlob , InvalidKeyName , PathNotFound
@@ -81,7 +81,7 @@ def walk(obj, location=()):
81
81
yield found
82
82
83
83
84
- def get (obj , segments ):
84
+ def get (obj , segments : Path ):
85
85
"""
86
86
Return the value at the path indicated by segments.
87
87
@@ -92,6 +92,9 @@ def get(obj, segments):
92
92
if leaf (current ):
93
93
raise PathNotFound (f"Path: { segments } [{ i } ]" )
94
94
95
+ if isinstance (current , Sequence ) and isinstance (segment , str ) and segment .isdecimal ():
96
+ segment = int (segment )
97
+
95
98
current = current [segment ]
96
99
return current
97
100
@@ -254,7 +257,7 @@ def match(segments: Path, glob: Glob):
254
257
return False
255
258
256
259
257
- def extend (thing : List , index : int , value = None ):
260
+ def extend (thing : MutableSequence , index : int , value = None ):
258
261
"""
259
262
Extend a sequence like thing such that it contains at least index +
260
263
1 many elements. The extension values will be None (default).
@@ -280,7 +283,7 @@ def extend(thing: List, index: int, value=None):
280
283
281
284
282
285
def _default_creator (
283
- current : Union [MutableMapping , List ],
286
+ current : Union [MutableMapping , Sequence ],
284
287
segments : Sequence [PathSegment ],
285
288
i : int ,
286
289
hints : Sequence [Tuple [PathSegment , type ]] = ()
@@ -294,7 +297,10 @@ def _default_creator(
294
297
segment = segments [i ]
295
298
length = len (segments )
296
299
297
- if isinstance (segment , int ):
300
+ if isinstance (current , Sequence ):
301
+ segment = int (segment )
302
+
303
+ if isinstance (current , MutableSequence ):
298
304
extend (current , segment )
299
305
300
306
# Infer the type from the hints provided.
@@ -308,7 +314,7 @@ def _default_creator(
308
314
else :
309
315
segment_next = None
310
316
311
- if isinstance (segment_next , int ):
317
+ if isinstance (segment_next , int ) or ( isinstance ( segment_next , str ) and segment_next . isdecimal ()) :
312
318
current [segment ] = []
313
319
else :
314
320
current [segment ] = {}
@@ -336,7 +342,7 @@ def set(
336
342
for (i , segment ) in enumerate (segments [:- 1 ]):
337
343
338
344
# If segment is non-int but supposed to be a sequence index
339
- if isinstance (segment , str ) and isinstance (current , Sequence ) and segment .isdigit ():
345
+ if isinstance (segment , str ) and isinstance (current , Sequence ) and segment .isdecimal ():
340
346
segment = int (segment )
341
347
342
348
try :
@@ -358,7 +364,7 @@ def set(
358
364
last_segment = segments [- 1 ]
359
365
360
366
# Resolve ambiguity of last segment
361
- if isinstance (last_segment , str ) and isinstance (current , Sequence ) and last_segment .isdigit ():
367
+ if isinstance (last_segment , str ) and isinstance (current , Sequence ) and last_segment .isdecimal ():
362
368
last_segment = int (last_segment )
363
369
364
370
if isinstance (last_segment , int ):
0 commit comments