From fa46296639b483042c31adf9a9d5dd53cf9c05a8 Mon Sep 17 00:00:00 2001 From: Rupayon Haldar <80724680+rupayon123@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:39:26 -0400 Subject: [PATCH] Normalize integer globs when matching string segments --- dpath/segments.py | 2 +- tests/test_get_values.py | 13 +++++++++++++ tests/test_segments.py | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/dpath/segments.py b/dpath/segments.py index f1443f0..2111935 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -244,7 +244,7 @@ def match(segments: Path, glob: Glob): # Let's see if the glob matches. We will turn any kind of # exception while attempting to match into a False for the # match. - if not fnmatchcase(s, g): + if not fnmatchcase(s, int_str(g)): return False except: return False diff --git a/tests/test_get_values.py b/tests/test_get_values.py index 68d5d3b..b4b9848 100644 --- a/tests/test_get_values.py +++ b/tests/test_get_values.py @@ -61,6 +61,19 @@ def test_get_glob_single(): assert dpath.get(ehash, ['doesnt', '*', 'exist'], default=6) == 6 +def test_integer_glob_with_string_mapping_key(): + obj = {'records': {'0': 'original', '1': 'unrelated'}} + path = ['records', 0] + + assert dpath.get(obj, path) == 'original' + assert dpath.values(obj, path) == ['original'] + assert dpath.search(obj, path) == {'records': {'0': 'original'}} + assert dpath.set(obj, path, 'updated') == 1 + assert obj == {'records': {'0': 'updated', '1': 'unrelated'}} + assert dpath.delete(obj, path) == 1 + assert obj == {'records': {'1': 'unrelated'}} + + def test_get_glob_multiple(): ehash = { "a": { diff --git a/tests/test_segments.py b/tests/test_segments.py index fb6a8bc..5f14f7b 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -262,6 +262,12 @@ def test_match(self, pair): (segments, glob) = pair assert api.match(segments, glob) is True + @given(st.integers()) + def test_match_integer_glob_with_string_segment(self, number): + assert api.match([str(number)], [number]) is True + assert api.match([number], [str(number)]) is True + assert api.match([str(number) + '0'], [number]) is False + @given(random_segments_with_nonmatching_glob()) def test_match_nonmatching(self, pair): '''