Skip to content

Commit cb6b94b

Browse files
committed
Improve negative number check
1 parent 308b977 commit cb6b94b

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

dpath/segments.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,12 @@ def match(segments: Path, glob: Glob):
230230

231231
try:
232232
# If search path segment (s) is an int and the current evaluated index (g) is int-like,
233-
# then g is surely a sequence index. Convert it to int and compare.
234-
if isinstance(s, int) and isinstance(g, str) and (g.count("-") == 0 or g.lstrip("-").isdigit()):
235-
return s == int(g)
233+
# then g is surely a sequence index as well. Convert it to int and compare.
234+
if isinstance(s, int) and isinstance(g, str):
235+
neg_c = g.count("-")
236+
237+
if (neg_c == 0 and g.isdigit()) or (neg_c == 1 and g.lstrip("-").isdigit()):
238+
return s == int(g)
236239

237240
# Let's see if the glob matches. We will turn any kind of
238241
# exception while attempting to match into a False for the

0 commit comments

Comments
 (0)