mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-22 13:36:15 +03:00
Add wiring support
This commit is contained in:
parent
b62010d37f
commit
bafb865127
File diff suppressed because it is too large
Load Diff
|
@ -147,6 +147,7 @@ class ConfigurationOption(Provider[Any]):
|
|||
def as_float(self) -> TypedConfigurationOption[float]: ...
|
||||
def as_(self, callback: _Callable[..., T], *args: Injection, **kwargs: Injection) -> TypedConfigurationOption[T]: ...
|
||||
def required(self) -> ConfigurationOption: ...
|
||||
def is_required(self) -> bool: ...
|
||||
def update(self, value: Any) -> None: ...
|
||||
def from_ini(self, filepath: Union[Path, str]) -> None: ...
|
||||
def from_yaml(self, filepath: Union[Path, str]) -> None: ...
|
||||
|
|
|
@ -1262,6 +1262,9 @@ cdef class ConfigurationOption(Provider):
|
|||
def required(self):
|
||||
return self.__class__(self.__name, self.__root_ref(), required=True)
|
||||
|
||||
def is_required(self):
|
||||
return self.__required
|
||||
|
||||
def override(self, value):
|
||||
if isinstance(value, Provider):
|
||||
raise Error('Configuration option can only be overridden by a value')
|
||||
|
|
|
@ -157,6 +157,9 @@ class ProvidersMap:
|
|||
else:
|
||||
new = getattr(new, segment)
|
||||
|
||||
if original.is_required():
|
||||
new = new.required()
|
||||
|
||||
if as_:
|
||||
new = new.as_(as_)
|
||||
|
||||
|
|
|
@ -62,6 +62,13 @@ def test_config_value(
|
|||
)
|
||||
|
||||
|
||||
@inject
|
||||
def test_config_value_required_undefined(
|
||||
value_required: int = Provide[Container.config.a.b.c.required()],
|
||||
):
|
||||
return value_required
|
||||
|
||||
|
||||
@inject
|
||||
def test_provide_provider(service_provider: Callable[..., Service] = Provider[Container.service.provider]):
|
||||
service = service_provider()
|
||||
|
|
|
@ -2,6 +2,7 @@ from decimal import Decimal
|
|||
import unittest
|
||||
|
||||
from dependency_injector.wiring import wire, Provide, Closing
|
||||
from dependency_injector import errors
|
||||
|
||||
# Runtime import to avoid syntax errors in samples on Python < 3.5
|
||||
import os
|
||||
|
@ -127,6 +128,11 @@ class WiringTest(unittest.TestCase):
|
|||
self.assertEqual(value_required_str, '10')
|
||||
self.assertEqual(value_required_decimal, Decimal(10))
|
||||
|
||||
def test_configuration_option_required_undefined(self):
|
||||
self.container.config.reset_override()
|
||||
with self.assertRaisesRegex(errors.Error, 'Undefined configuration option "config.a.b.c"'):
|
||||
module.test_config_value_required_undefined()
|
||||
|
||||
def test_provide_provider(self):
|
||||
service = module.test_provide_provider()
|
||||
self.assertIsInstance(service, Service)
|
||||
|
|
Loading…
Reference in New Issue
Block a user