From cc627f7ba3bfb667b63df0cfd297dd456972d447 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sat, 24 Sep 2022 23:06:18 +0200 Subject: [PATCH] Prevent wrapping into Optional when the option has a default --- mkdocs/config/config_options.py | 7 ++++++- mkdocs/tests/config/config_options_tests.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index b76af0a7..211c0be4 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -429,6 +429,11 @@ class Optional(Generic[T], BaseConfigOption[Union[T, None]]): """ def __init__(self, config_option: BaseConfigOption[T]): + if config_option.default is not None: + raise ValueError( + f"This option already has a default ({config_option.default!r}) " + f"and doesn't need to be wrapped into Optional" + ) super().__init__() self.option = config_option self.warnings = config_option.warnings @@ -443,7 +448,7 @@ class Optional(Generic[T], BaseConfigOption[Union[T, None]]): def run_validation(self, value): if value is None: - return self.default + return None return self.option.validate(value) def post_validation(self, config, key_name): diff --git a/mkdocs/tests/config/config_options_tests.py b/mkdocs/tests/config/config_options_tests.py index b64bf827..71b3b14e 100644 --- a/mkdocs/tests/config/config_options_tests.py +++ b/mkdocs/tests/config/config_options_tests.py @@ -95,6 +95,10 @@ class TypeTest(TestCase): ): self.get_config(Schema, {'option': "Testing Long"}) + def test_optional_with_default(self) -> None: + with self.assertRaisesRegex(ValueError, "doesn't need to be wrapped into Optional"): + c.Optional(c.Type(int, default=5)) + class ChoiceTest(TestCase): def test_required(self) -> None: