diff --git a/dpath/__init__.py b/dpath/__init__.py index c717314..c4c5c8f 100644 --- a/dpath/__init__.py +++ b/dpath/__init__.py @@ -21,7 +21,7 @@ ] from collections.abc import MutableMapping, MutableSequence -from typing import Union, List, Any, Callable, Optional +from typing import Union, List, Any, Callable, Optional, Sequence from dpath import segments, options from dpath.exceptions import InvalidKeyName, PathNotFound @@ -30,7 +30,7 @@ _DEFAULT_SENTINEL = object() -def _split_path(path: Path, separator: Optional[str]) -> Union[List[PathSegment], PathSegment]: +def _split_path(path: Path, separator: Optional[str] = "/") -> Union[List[PathSegment], PathSegment]: """ Given a path and separator, return a tuple of segments. If path is already a non-leaf thing, return it. @@ -45,16 +45,6 @@ def _split_path(path: Path, separator: Optional[str]) -> Union[List[PathSegment] else: split_segments = path.lstrip(separator).split(separator) - if options.CONVERT_INT_LIKE_SEGMENTS: - # Attempt to convert integer segments into actual integers. - final = [] - for segment in split_segments: - try: - final.append(int(segment)) - except ValueError: - final.append(segment) - split_segments = final - return split_segments diff --git a/dpath/options.py b/dpath/options.py index 91b4290..41f35c4 100644 --- a/dpath/options.py +++ b/dpath/options.py @@ -1,2 +1 @@ ALLOW_EMPTY_STRING_KEYS = False -CONVERT_INT_LIKE_SEGMENTS = True diff --git a/dpath/segments.py b/dpath/segments.py index ccf10e7..ed368bd 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -320,6 +320,11 @@ def set( # For everything except the last value, walk down the path and # create if creator is set. for (i, segment) in enumerate(segments[:-1]): + + # If segment is non-int but supposed to be a sequence index + if not isinstance(segment, int) and segment.isdigit() and isinstance(current, Sequence): + segment = int(segment) + try: # Optimistically try to get the next value. This makes the # code agnostic to whether current is a list or a dict. diff --git a/dpath/types.py b/dpath/types.py index b876e6a..d54a2e6 100644 --- a/dpath/types.py +++ b/dpath/types.py @@ -42,4 +42,4 @@ def creator( segments: Sequence[PathSegment], i: int, hints: Sequence[Tuple[PathSegment, type]] = () - )""" + ) -> PathSegment"""