mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-23 05:56:19 +03:00
Add strict mode + tests
This commit is contained in:
parent
3b69ed91c6
commit
6f22549882
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -107,6 +107,7 @@ cdef class TypedConfigurationOption(Callable):
|
||||||
|
|
||||||
cdef class Configuration(Object):
|
cdef class Configuration(Object):
|
||||||
cdef str __name
|
cdef str __name
|
||||||
|
cdef bint __strict
|
||||||
cdef dict __children
|
cdef dict __children
|
||||||
cdef object __weakref__
|
cdef object __weakref__
|
||||||
|
|
||||||
|
|
|
@ -160,7 +160,7 @@ class TypedConfigurationOption(Callable[T]):
|
||||||
|
|
||||||
class Configuration(Object[Any]):
|
class Configuration(Object[Any]):
|
||||||
DEFAULT_NAME: str = 'config'
|
DEFAULT_NAME: str = 'config'
|
||||||
def __init__(self, name: str = DEFAULT_NAME, default: Optional[Any] = None) -> None: ...
|
def __init__(self, name: str = DEFAULT_NAME, default: Optional[Any] = None, *, strict: bool = False) -> None: ...
|
||||||
def __getattr__(self, item: str) -> ConfigurationOption: ...
|
def __getattr__(self, item: str) -> ConfigurationOption: ...
|
||||||
def __getitem__(self, item: Union[str, Provider]) -> ConfigurationOption: ...
|
def __getitem__(self, item: Union[str, Provider]) -> ConfigurationOption: ...
|
||||||
def get_name(self) -> str: ...
|
def get_name(self) -> str: ...
|
||||||
|
|
|
@ -1396,9 +1396,11 @@ cdef class Configuration(Object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DEFAULT_NAME = 'config'
|
DEFAULT_NAME = 'config'
|
||||||
|
UNDEFINED = object()
|
||||||
|
|
||||||
def __init__(self, name=DEFAULT_NAME, default=None):
|
def __init__(self, name=DEFAULT_NAME, default=None, strict=False):
|
||||||
self.__name = name
|
self.__name = name
|
||||||
|
self.__strict = strict
|
||||||
|
|
||||||
value = {}
|
value = {}
|
||||||
if default is not None:
|
if default is not None:
|
||||||
|
@ -1416,7 +1418,7 @@ cdef class Configuration(Object):
|
||||||
if copied is not None:
|
if copied is not None:
|
||||||
return copied
|
return copied
|
||||||
|
|
||||||
copied = self.__class__(self.__name, self.__provides)
|
copied = self.__class__(self.__name, self.__provides, self.__strict)
|
||||||
memo[id(self)] = copied
|
memo[id(self)] = copied
|
||||||
|
|
||||||
copied.__children = deepcopy(self.__children, memo)
|
copied.__children = deepcopy(self.__children, memo)
|
||||||
|
@ -1467,9 +1469,12 @@ cdef class Configuration(Object):
|
||||||
|
|
||||||
while len(keys) > 0:
|
while len(keys) > 0:
|
||||||
key = keys.pop(0)
|
key = keys.pop(0)
|
||||||
value = value.get(key)
|
value = value.get(key, self.UNDEFINED)
|
||||||
if value is None:
|
|
||||||
break
|
if value is self.UNDEFINED:
|
||||||
|
if self.__strict:
|
||||||
|
raise Error('Undefined configuration option "{0}.{1}"'.format(self.__name, selector))
|
||||||
|
return None
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
|
@ -176,6 +176,11 @@ class ConfigTests(unittest.TestCase):
|
||||||
def test_value_of_undefined_option(self):
|
def test_value_of_undefined_option(self):
|
||||||
self.assertIsNone(self.config.a())
|
self.assertIsNone(self.config.a())
|
||||||
|
|
||||||
|
def test_value_of_undefined_option_in_strict_mode(self):
|
||||||
|
self.config = providers.Configuration(strict=True)
|
||||||
|
with self.assertRaisesRegex(errors.Error, 'Undefined configuration option "config.a"'):
|
||||||
|
self.config.a()
|
||||||
|
|
||||||
def test_getting_of_special_attributes(self):
|
def test_getting_of_special_attributes(self):
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(AttributeError):
|
||||||
self.config.__name__
|
self.config.__name__
|
||||||
|
|
Loading…
Reference in New Issue
Block a user