Skip to content

Commit b07cc6a

Browse files
committed
Resolve int ambiguity in get function
1 parent 436dac4 commit b07cc6a

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

dpath/segments.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def walk(obj, location=()):
8181
yield found
8282

8383

84-
def get(obj, segments):
84+
def get(obj, segments: Path):
8585
"""
8686
Return the value at the path indicated by segments.
8787
@@ -92,6 +92,9 @@ def get(obj, segments):
9292
if leaf(current):
9393
raise PathNotFound(f"Path: {segments}[{i}]")
9494

95+
if isinstance(current, Sequence) and isinstance(segment, (str, bytes)) and segment.isdigit():
96+
segment = int(segment)
97+
9598
current = current[segment]
9699
return current
97100

@@ -339,7 +342,7 @@ def set(
339342
for (i, segment) in enumerate(segments[:-1]):
340343

341344
# If segment is non-int but supposed to be a sequence index
342-
if isinstance(segment, str) and isinstance(current, Sequence) and segment.isdigit():
345+
if isinstance(segment, (str, bytes)) and isinstance(current, Sequence) and segment.isdigit():
343346
segment = int(segment)
344347

345348
try:
@@ -361,7 +364,7 @@ def set(
361364
last_segment = segments[-1]
362365

363366
# Resolve ambiguity of last segment
364-
if isinstance(last_segment, str) and isinstance(current, Sequence) and last_segment.isdigit():
367+
if isinstance(last_segment, (str, bytes)) and isinstance(current, Sequence) and last_segment.isdigit():
365368
last_segment = int(last_segment)
366369

367370
if isinstance(last_segment, int):

dpath/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MergeType(IntFlag):
4646
replaces the destination in this situation."""
4747

4848

49-
PathSegment = Union[int, str]
49+
PathSegment = Union[int, str, bytes]
5050
"""Type alias for dict path segments where integers are explicitly casted."""
5151

5252
Filter = Callable[[Any], bool]

0 commit comments

Comments
 (0)