From c23bcca8803f790bae87c9749bb5a21dfa57751f Mon Sep 17 00:00:00 2001 From: moomoohk <2220203+moomoohk@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:59:52 +0200 Subject: [PATCH 1/5] Handle edge case where last search segment is a wildcard --- dpath/segments.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dpath/segments.py b/dpath/segments.py index 1e5dc04..3e622b7 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -185,6 +185,10 @@ def match(segments: Path, glob: Glob): match(segments, glob) -> bool """ + # Handle edge case where last segment of the glob is a wildcard. + if glob.count("*") > 0 and glob.index("*") == len(glob) - 1: + segments = list(segments) + [""] + segments = tuple(segments) glob = tuple(glob) @@ -217,7 +221,6 @@ def match(segments: Path, glob: Glob): # If we were successful in matching up the lengths, then we can # compare them using fnmatch. if path_len == len(ss_glob): - # TODO: Delete if not needed (previous code) - i = zip(map(int_str, segments), map(int_str, ss_glob)) i = zip(segments, ss_glob) for s, g in i: # Match the stars we added to the glob to the type of the From 808bd2b60c92ac694583996638dabf77f2d68650 Mon Sep 17 00:00:00 2001 From: moomoohk <2220203+moomoohk@users.noreply.github.com> Date: Thu, 1 Dec 2022 19:03:42 +0200 Subject: [PATCH 2/5] Bump version --- dpath/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpath/version.py b/dpath/version.py index 5b0431e..b777579 100644 --- a/dpath/version.py +++ b/dpath/version.py @@ -1 +1 @@ -VERSION = "2.1.1" +VERSION = "2.1.2" From d78c72d8cfd35cab49f3954c15e1ff9a898d90d5 Mon Sep 17 00:00:00 2001 From: moomoohk <2220203+moomoohk@users.noreply.github.com> Date: Thu, 1 Dec 2022 19:18:00 +0200 Subject: [PATCH 3/5] Simplify int handling in matching code --- dpath/segments.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dpath/segments.py b/dpath/segments.py index 3e622b7..ac1ace9 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -232,9 +232,9 @@ def match(segments: Path, glob: Glob): g = '*' try: - # If search path segment (s) is an int and the current evaluated index (g) is int-like, - # then g might be a sequence index as well. Try converting it to an int. - if isinstance(s, int) and isinstance(g, str): + # If search path segment (s) is an int then assume currently evaluated index (g) might be a sequenc + # index as well. Try converting it to an int. + if isinstance(s, int): return s == int(g) except: # Will reach this point if g can't be converted to an int (e.g. when g is a RegEx pattern). From 780d7858590c3a935bc13e6f3068bd583bb5a9b7 Mon Sep 17 00:00:00 2001 From: moomoohk <2220203+moomoohk@users.noreply.github.com> Date: Thu, 1 Dec 2022 22:36:47 +0200 Subject: [PATCH 4/5] Handle edge case better --- dpath/segments.py | 4 ++-- tests/test_broken_afilter.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dpath/segments.py b/dpath/segments.py index ac1ace9..a74effd 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -185,8 +185,8 @@ def match(segments: Path, glob: Glob): match(segments, glob) -> bool """ - # Handle edge case where last segment of the glob is a wildcard. - if glob.count("*") > 0 and glob.index("*") == len(glob) - 1: + # Handle edge case where last segment of the glob is a wildcard and we want fnmatchcase to validate the path. + if len(segments) == len(glob) - 1 and glob.count("*") > 0 and glob.index("*") == len(glob) - 1: segments = list(segments) + [""] segments = tuple(segments) diff --git a/tests/test_broken_afilter.py b/tests/test_broken_afilter.py index 683c727..1afca6f 100644 --- a/tests/test_broken_afilter.py +++ b/tests/test_broken_afilter.py @@ -52,7 +52,6 @@ def filter(x): ], } - results = [[x[0], x[1]] for x in dpath.search(a, 'actions/*', yielded=True)] results = [[x[0], x[1]] for x in dpath.search(a, 'actions/*', afilter=filter, yielded=True)] assert len(results) == 1 assert results[0][1]['type'] == 'correct' From 951a1850e4b97c7675906ef5b3341c5a31aa3dec Mon Sep 17 00:00:00 2001 From: moomoohk <2220203+moomoohk@users.noreply.github.com> Date: Fri, 14 Apr 2023 18:20:23 +0300 Subject: [PATCH 5/5] Returned lost comment --- dpath/segments.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dpath/segments.py b/dpath/segments.py index 72bfd28..08b02d0 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -235,6 +235,8 @@ def match(segments: Path, glob: Glob): g = '*' try: + # If search path segment (s) is an int then assume currently evaluated index (g) might be a sequence + # index as well. Try converting it to an int. if isinstance(s, int) and s == int(g): continue except: