Skip to content

Commit f871299

Browse files
committed
Better int ambiguity resolution in default creator
1 parent 312a42c commit f871299

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

dpath/segments.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from copy import deepcopy
22
from fnmatch import fnmatchcase
3-
from typing import List, Sequence, Tuple, Iterator, Any, Union, Optional, MutableMapping
3+
from typing import List, Sequence, Tuple, Iterator, Any, Union, Optional, MutableMapping, MutableSequence
44

55
from dpath import options
66
from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound
@@ -254,7 +254,7 @@ def match(segments: Path, glob: Glob):
254254
return False
255255

256256

257-
def extend(thing: List, index: int, value=None):
257+
def extend(thing: MutableSequence, index: int, value=None):
258258
"""
259259
Extend a sequence like thing such that it contains at least index +
260260
1 many elements. The extension values will be None (default).
@@ -280,7 +280,7 @@ def extend(thing: List, index: int, value=None):
280280

281281

282282
def _default_creator(
283-
current: Union[MutableMapping, List],
283+
current: Union[MutableMapping, Sequence],
284284
segments: Sequence[PathSegment],
285285
i: int,
286286
hints: Sequence[Tuple[PathSegment, type]] = ()
@@ -294,7 +294,10 @@ def _default_creator(
294294
segment = segments[i]
295295
length = len(segments)
296296

297-
if isinstance(segment, int):
297+
if isinstance(current, Sequence):
298+
segment = int(segment)
299+
300+
if isinstance(current, MutableSequence):
298301
extend(current, segment)
299302

300303
# Infer the type from the hints provided.
@@ -308,7 +311,7 @@ def _default_creator(
308311
else:
309312
segment_next = None
310313

311-
if isinstance(segment_next, int):
314+
if isinstance(segment_next, int) or segment_next.isdigit():
312315
current[segment] = []
313316
else:
314317
current[segment] = {}

0 commit comments

Comments
 (0)