Skip to content

Commit e71a2b3

Browse files
committed
updated configuration provider interface and doc
1 parent 779a2fb commit e71a2b3

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

docs/providers/configuration.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ the container will call ``config.from_pydantic()`` automatically:
212212
if __name__ == "__main__":
213213
container = Container() # Config is loaded from Settings()
214214
215+
In addition, if you need the pydantic instance to be initialized on use, you can provide ``pydantic_settings.BaseSettings`` type instead.
216+
The container will initialize a pydantic instance on load without kwargs.
217+
218+
.. code-block:: python
219+
:emphasize-lines: 3
220+
221+
class Container(containers.DeclarativeContainer):
222+
223+
config = providers.Configuration(pydantic_settings=[Settings])
224+
225+
226+
if __name__ == "__main__":
227+
container = Container() # Config is loaded from Settings instance that is initialized
215228
216229
.. note::
217230

src/dependency_injector/providers.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class Configuration(Object[Any]):
272272
ini_files: Optional[_Iterable[Union[Path, str]]] = None,
273273
yaml_files: Optional[_Iterable[Union[Path, str]]] = None,
274274
json_files: Optional[_Iterable[Union[Path, str]]] = None,
275-
pydantic_settings: Optional[_Iterable[PydanticSettings]] = None,
275+
pydantic_settings: Optional[_Iterable[Union[PydanticSettings, Type[PydanticSettings]]]] = None,
276276
) -> None: ...
277277
def __enter__(self) -> _Self: ...
278278
def __exit__(self, *exc_info: Any) -> None: ...
@@ -292,8 +292,8 @@ class Configuration(Object[Any]):
292292
def set_yaml_files(self, files: _Iterable[Union[Path, str]]) -> _Self: ...
293293
def get_json_files(self) -> _List[Union[Path, str]]: ...
294294
def set_json_files(self, files: _Iterable[Union[Path, str]]) -> _Self: ...
295-
def get_pydantic_settings(self) -> _List[PydanticSettings]: ...
296-
def set_pydantic_settings(self, settings: _Iterable[PydanticSettings]) -> _Self: ...
295+
def get_pydantic_settings(self) -> _List[Union[PydanticSettings, Type[PydanticSettings]]]: ...
296+
def set_pydantic_settings(self, settings: _Iterable[Union[PydanticSettings, Type[PydanticSettings]]]) -> _Self: ...
297297
def load(self, required: bool = False, envs_required: bool = False) -> None: ...
298298
def get(self, selector: str) -> Any: ...
299299
def set(self, selector: str, value: Any) -> OverridingContext[P]: ...
@@ -319,7 +319,7 @@ class Configuration(Object[Any]):
319319
envs_required: bool = False,
320320
) -> None: ...
321321
def from_pydantic(
322-
self, settings: PydanticSettings, required: bool = False, **kwargs: Any
322+
self, settings: Union[PydanticSettings, Type[PydanticSettings]], required: bool = False, **kwargs: Any
323323
) -> None: ...
324324
def from_dict(self, options: _Dict[str, Any], required: bool = False) -> None: ...
325325
def from_env(

tests/typing/configuration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
config2.from_pydantic(PydanticSettings())
3535

36+
config2.from_pydantic(PydanticSettings)
37+
3638
# Test 3: to check as_*() methods
3739
config3 = providers.Configuration()
3840
int3 = config3.option.as_int()
@@ -91,3 +93,8 @@
9193
strict=True,
9294
default={},
9395
)
96+
97+
# Test 7: pydantic class
98+
config7_pydantic_class = providers.Configuration(
99+
pydantic_settings=[PydanticSettings]
100+
)

0 commit comments

Comments
 (0)