Prevent wrapping into Optional when the option has a default

This commit is contained in:
Oleh Prypin
2022-09-24 23:06:18 +02:00
parent aeffe61499
commit cc627f7ba3
2 changed files with 10 additions and 1 deletions

View File

@@ -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):

View File

@@ -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: