diff --git a/src/dependency_injector/providers.pyi b/src/dependency_injector/providers.pyi index 1dbed5ea..28b64800 100644 --- a/src/dependency_injector/providers.pyi +++ b/src/dependency_injector/providers.pyi @@ -117,6 +117,39 @@ class CoroutineDelegate(Delegate): def __init__(self, coroutine: Coroutine) -> None: ... +class ConfigurationOption(Provider): + UNDEFINED: object + def __init__(self, name: str, root: Configuration) -> None: ... + def __call__(self, *args: Injection, **kwargs: Injection) -> Any: ... + def __getattr__(self, item: str) -> ConfigurationOption: ... + def __getitem__(self, item: str) -> ConfigurationOption: ... + def get_name(self) -> str: ... + def as_int(self) -> Callable[int]: ... + def as_float(self) -> Callable[float]: ... + def as_(self, callback: _Callable[..., T], *args: Injection, **kwargs: Injection) -> Callable[T]: ... + def update(self, value: Any) -> None: ... + def from_ini(self, filepath: str) -> None: ... + def from_yaml(self, filepath: str) -> None: ... + def from_dict(self, options: Dict[str, Any]) -> None: ... + def from_env(self, name: str, default: Optional[Any] = None) -> None: ... + + +class Configuration(Object): + DEFAULT_NAME: str = 'config' + def __init__(self, name: str = DEFAULT_NAME, default: Optional[Any] = None) -> None: ... + def __getattr__(self, item: str) -> ConfigurationOption: ... + def __getitem__(self, item: str) -> ConfigurationOption: ... + def get_name(self) -> str: ... + def get(self, selector: str) -> Any: ... + def set(self, selector: str, value: Any) -> OverridingContext: ... + def reset_cache(self) -> None: ... + def update(self, value: Any) -> None: ... + def from_ini(self, filepath: str) -> None: ... + def from_yaml(self, filepath: str) -> None: ... + def from_dict(self, options: Dict[str, Any]) -> None: ... + def from_env(self, name: str, default: Optional[Any] = None) -> None: ... + + class Factory(Provider, Generic[T]): provided_type: Optional[Type] def __init__(self, provides: _Callable[..., T], *args: Injection, **kwargs: Injection) -> None: ... diff --git a/tests/typing/configuration.py b/tests/typing/configuration.py new file mode 100644 index 00000000..e44eda31 --- /dev/null +++ b/tests/typing/configuration.py @@ -0,0 +1,19 @@ +from dependency_injector import providers + + +# Test 1: to check the getattr +config1 = providers.Configuration() +provider1 = providers.Factory(dict, a=config1.a) + +# Test 2: to check the from_*() method +config2 = providers.Configuration() +config2.from_dict({}) +config2.from_ini('config.ini') +config2.from_yaml('config.yml') +config2.from_env('ENV', 'default') + +# Test 3: to check as_*() methods +config3 = providers.Configuration() +int3: providers.Callable[int] = config3.option.as_int() +float3: providers.Callable[float] = config3.option.as_float() +int3_custom: providers.Callable[int] = config3.option.as_(int)