Skip to content

Commit

Permalink
Change config_options.URL's default from '' to None
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Sep 26, 2022
1 parent 5156947 commit aeffe61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
6 changes: 3 additions & 3 deletions mkdocs/config/config_options.py
Expand Up @@ -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)

Expand Down
15 changes: 14 additions & 1 deletion mkdocs/tests/config/config_options_legacy_tests.py
Expand Up @@ -432,9 +432,22 @@ class Schema:
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):
Expand Down
13 changes: 13 additions & 0 deletions mkdocs/tests/config/config_options_tests.py
Expand Up @@ -414,10 +414,23 @@ class Schema(Config):
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(
Expand Down

0 comments on commit aeffe61

Please sign in to comment.