Skip to content

Commit

Permalink
Better int ambiguity resolution in default creator
Browse files Browse the repository at this point in the history
  • Loading branch information
moomoohk committed Jan 8, 2023
1 parent 312a42c commit f871299
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions dpath/segments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy
from fnmatch import fnmatchcase
from typing import List, Sequence, Tuple, Iterator, Any, Union, Optional, MutableMapping
from typing import List, Sequence, Tuple, Iterator, Any, Union, Optional, MutableMapping, MutableSequence

from dpath import options
from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound
Expand Down Expand Up @@ -254,7 +254,7 @@ def match(segments: Path, glob: Glob):
return False


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


def _default_creator(
current: Union[MutableMapping, List],
current: Union[MutableMapping, Sequence],
segments: Sequence[PathSegment],
i: int,
hints: Sequence[Tuple[PathSegment, type]] = ()
Expand All @@ -294,7 +294,10 @@ def _default_creator(
segment = segments[i]
length = len(segments)

if isinstance(segment, int):
if isinstance(current, Sequence):
segment = int(segment)

if isinstance(current, MutableSequence):
extend(current, segment)

# Infer the type from the hints provided.
Expand All @@ -308,7 +311,7 @@ def _default_creator(
else:
segment_next = None

if isinstance(segment_next, int):
if isinstance(segment_next, int) or segment_next.isdigit():
current[segment] = []
else:
current[segment] = {}
Expand Down

0 comments on commit f871299

Please sign in to comment.