From 64bc84759a79582ba6fa388d74ed1e4bdb7fcea6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 23 Jun 2024 16:44:13 -0500 Subject: [PATCH 1/2] Fix vol.Remove not removing keys fixes a regression from #479 blocks https://github.com/alecthomas/voluptuous/pull/479 --- voluptuous/schema_builder.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index ca134ca..2bf6954 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -435,11 +435,11 @@ def validate_mapping(path, iterable, out): break else: - if error: - errors.append(error) - elif remove_key: + if remove_key: # remove key continue + elif error: + errors.append(error) elif self.extra == ALLOW_EXTRA: out[key] = value elif self.extra != REMOVE_EXTRA: From 468b66c293d16510f47d363416710464bcb4dd19 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 23 Jun 2024 16:49:31 -0500 Subject: [PATCH 2/2] add test --- voluptuous/tests/tests.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index f6d6784..8fa1883 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -176,6 +176,29 @@ def test_remove(): assert out_ == [1, 2, 1.0, 4] +def test_remove_with_error(): + def starts_with_dot(key: str) -> str: + """Check if key starts with dot.""" + if not key.startswith("."): + raise Invalid("Key does not start with .") + return key + + def does_not_start_with_dot(key: str) -> str: + """Check if key does not start with dot.""" + if key.startswith("."): + raise Invalid("Key starts with .") + return key + + schema = Schema( + { + Remove(All(str, starts_with_dot)): object, + does_not_start_with_dot: Any(None), + } + ) + out_ = schema({".remove": None, "ok": None}) + assert ".remove" not in out_ and "ok" in out_ + + def test_extra_empty_errors(): schema = Schema({'a': {Extra: object}}, required=True) schema({'a': {}})