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] 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,