From a673d24e727df8beaac9ff08f97ded8fa4471d2d Mon Sep 17 00:00:00 2001 From: squatched Date: Fri, 31 May 2024 14:08:47 -0500 Subject: [PATCH 1/2] Support Empty List Path In get Solves Issue #193. Currently, dpath.get() works with a path of '/' by returning the value of the object passed in. This behavior is not reflected with an empty list. Now, passing an empty list to dpath.get() will behave the same way (returning the root object value). --- dpath/__init__.py | 2 +- tests/test_get_values.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/dpath/__init__.py b/dpath/__init__.py index 9f56e6b..faa62c7 100644 --- a/dpath/__init__.py +++ b/dpath/__init__.py @@ -167,7 +167,7 @@ def get( If more than one leaf matches the glob, ValueError is raised. If the glob is not found and a default is not provided, KeyError is raised. """ - if glob == "/": + if glob == "/" or (type(glob) is not str and len(glob) == 0): return obj globlist = _split_path(glob, separator) diff --git a/tests/test_get_values.py b/tests/test_get_values.py index 9eeef82..2ae506b 100644 --- a/tests/test_get_values.py +++ b/tests/test_get_values.py @@ -17,6 +17,9 @@ def test_util_get_root(): ret = dpath.get(x, '/') assert ret == x + ret = dpath.get(x, []) + assert ret == x + def test_get_explicit_single(): ehash = { From dc3f5d9d70f492af9b61226dcf95603003801b35 Mon Sep 17 00:00:00 2001 From: moomoohk <2220203+moomoohk@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:19:24 +0300 Subject: [PATCH 2/2] Simplify condition --- dpath/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpath/__init__.py b/dpath/__init__.py index 8bb6027..5c9faca 100644 --- a/dpath/__init__.py +++ b/dpath/__init__.py @@ -167,7 +167,7 @@ def get( If more than one leaf matches the glob, ValueError is raised. If the glob is not found and a default is not provided, KeyError is raised. """ - if glob == "/" or (type(glob) is not str and len(glob) == 0): + if isinstance(glob, str) and glob == "/" or len(glob) == 0: return obj globlist = _split_path(glob, separator)