diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index 3b4fa9ad..b76af0a7 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -394,14 +394,14 @@ class URL(OptionallyRequired[str]): """ @overload - def __init__(self, default='', *, is_dir: bool = False): + def __init__(self, default=None, *, is_dir: bool = False): ... @overload - def __init__(self, default='', *, required: bool, is_dir: bool = False): + def __init__(self, default=None, *, required: bool, is_dir: bool = False): ... - def __init__(self, default='', required=None, is_dir: bool = False): + def __init__(self, default=None, required=None, is_dir: bool = False): self.is_dir = is_dir super().__init__(default, required=required) diff --git a/mkdocs/tests/config/config_options_legacy_tests.py b/mkdocs/tests/config/config_options_legacy_tests.py index 221ddb60..8a3d545b 100644 --- a/mkdocs/tests/config/config_options_legacy_tests.py +++ b/mkdocs/tests/config/config_options_legacy_tests.py @@ -432,9 +432,22 @@ class URLTest(TestCase): conf = self.get_config(Schema, {'option': "https://mkdocs.org"}) self.assertEqual(conf['option'], "https://mkdocs.org/") + def test_optional(self): + class Schema: + option = c.URL(is_dir=True) + + conf = self.get_config(Schema, {'option': ''}) + self.assertEqual(conf['option'], '') + + conf = self.get_config(Schema, {'option': None}) + self.assertEqual(conf['option'], None) + def test_invalid_url(self): class Schema: - option = c.URL() + option = c.URL(required=True) + + with self.expect_error(option="Required configuration not provided."): + self.get_config(Schema, {'option': None}) for url in "www.mkdocs.org", "//mkdocs.org/test", "http:/mkdocs.org/", "/hello/": with self.subTest(url=url): diff --git a/mkdocs/tests/config/config_options_tests.py b/mkdocs/tests/config/config_options_tests.py index 553dca6b..b64bf827 100644 --- a/mkdocs/tests/config/config_options_tests.py +++ b/mkdocs/tests/config/config_options_tests.py @@ -414,10 +414,23 @@ class URLTest(TestCase): conf = self.get_config(Schema, {'option': "https://mkdocs.org"}) self.assertEqual(conf.option, "https://mkdocs.org/") + def test_optional(self): + class Schema(Config): + option = c.Optional(c.URL(is_dir=True)) + + conf = self.get_config(Schema, {'option': ''}) + self.assertEqual(conf.option, '') + + conf = self.get_config(Schema, {'option': None}) + self.assertEqual(conf.option, None) + def test_invalid_url(self) -> None: class Schema(Config): option = c.URL() + with self.expect_error(option="Required configuration not provided."): + self.get_config(Schema, {'option': None}) + for url in "www.mkdocs.org", "//mkdocs.org/test", "http:/mkdocs.org/", "/hello/": with self.subTest(url=url): with self.expect_error(