From 44ef8346800bd875a9e8cd43a8640f34028bb142 Mon Sep 17 00:00:00 2001 From: codingFeng101 <3524962421@qq.com> Date: Tue, 30 Jun 2026 08:38:28 +0800 Subject: [PATCH 1/2] Reject empty keypaths separator --- benedict/core/keypaths.py | 4 +++- tests/core/test_keypaths.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/benedict/core/keypaths.py b/benedict/core/keypaths.py index ca4c9dea..9ba6151f 100644 --- a/benedict/core/keypaths.py +++ b/benedict/core/keypaths.py @@ -13,9 +13,11 @@ def keypaths( indexes: bool = False, sort: bool = True, ) -> list[str]: - separator = separator or "." + separator = "." if separator is None else separator if not type_util.is_string(separator): raise ValueError("separator argument must be a (non-empty) string.") + if not separator: + raise ValueError("separator argument must be a (non-empty) string.") kls = keylists(d, indexes=indexes) kps = [separator.join([f"{key}" for key in kl]) for kl in kls] if sort: diff --git a/tests/core/test_keypaths.py b/tests/core/test_keypaths.py index 5d4faa6f..62834ad1 100644 --- a/tests/core/test_keypaths.py +++ b/tests/core/test_keypaths.py @@ -106,6 +106,10 @@ def test_keypaths_with_invalid_separator(self) -> None: with self.assertRaises(ValueError): _ = _keypaths(i, separator=True) # type: ignore[arg-type] + def test_keypaths_with_empty_separator(self) -> None: + with self.assertRaises(ValueError): + _ = _keypaths({"a": {"b": 1}}, separator="") + def test_keypaths_without_separator(self) -> None: i = { "a": 1, From 0fcdb4641c75db07728db0e6adbd7d250826e8ee Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Tue, 7 Jul 2026 03:42:46 -0700 Subject: [PATCH 2/2] Use a single condition. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- benedict/core/keypaths.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/benedict/core/keypaths.py b/benedict/core/keypaths.py index 9ba6151f..ddd9c1e1 100644 --- a/benedict/core/keypaths.py +++ b/benedict/core/keypaths.py @@ -14,9 +14,7 @@ def keypaths( sort: bool = True, ) -> list[str]: separator = "." if separator is None else separator - if not type_util.is_string(separator): - raise ValueError("separator argument must be a (non-empty) string.") - if not separator: + if not type_util.is_string(separator) or not separator: raise ValueError("separator argument must be a (non-empty) string.") kls = keylists(d, indexes=indexes) kps = [separator.join([f"{key}" for key in kl]) for kl in kls]