Skip to content

Commit 6030950

Browse files
authored
Configuration(pydantic_settings=[...]) (#525)
* Add implementation * Update changelog * Fix deepcopy() * Add example * Add tests * Add docs
1 parent 34902db commit 6030950

File tree

10 files changed

+6808
-6169
lines changed

10 files changed

+6808
-6169
lines changed

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Develop
1616
- Add support of ``with`` statement for ``container.override_providers()`` method.
1717
- Add ``Configuration(yaml_files=[...])`` argument.
1818
- Add ``Configuration(ini_files=[...])`` argument.
19+
- Add ``Configuration(pydantic_settings=[...])`` argument.
1920
- Drop support of Python 3.4. There are no immediate breaking changes, but Dependency Injector
2021
will no longer be tested on Python 3.4 and any bugs will not be fixed.
2122
- Fix ``Dependency.is_defined`` attribute to always return boolean value.

docs/providers/configuration.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ If you need to pass an argument to this call, use ``.from_pydantic()`` keyword a
154154
155155
container.config.from_pydantic(Settings(), exclude={"optional"})
156156
157+
Alternatively, you can provide a ``pydantic`` settings object over the configuration provider argument. In that case,
158+
the container will call ``config.from_pydantic()`` automatically:
159+
160+
.. code-block:: python
161+
:emphasize-lines: 3
162+
163+
class Container(containers.DeclarativeContainer):
164+
165+
config = providers.Configuration(pydantic_settings=[Settings()])
166+
167+
168+
if __name__ == "__main__":
169+
container = Container() # Config is loaded from Settings()
170+
171+
157172
.. note::
158173

159174
``Dependency Injector`` doesn't install ``pydantic`` by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""`Configuration` provider values loading example."""
2+
3+
import os
4+
5+
from dependency_injector import containers, providers
6+
from pydantic import BaseSettings, Field
7+
8+
# Emulate environment variables
9+
os.environ["AWS_ACCESS_KEY_ID"] = "KEY"
10+
os.environ["AWS_SECRET_ACCESS_KEY"] = "SECRET"
11+
12+
13+
class AwsSettings(BaseSettings):
14+
15+
access_key_id: str = Field(env="aws_access_key_id")
16+
secret_access_key: str = Field(env="aws_secret_access_key")
17+
18+
19+
class Settings(BaseSettings):
20+
21+
aws: AwsSettings = AwsSettings()
22+
optional: str = Field(default="default_value")
23+
24+
25+
class Container(containers.DeclarativeContainer):
26+
27+
config = providers.Configuration(pydantic_settings=[Settings()])
28+
29+
30+
if __name__ == "__main__":
31+
container = Container()
32+
33+
assert container.config.aws.access_key_id() == "KEY"
34+
assert container.config.aws.secret_access_key() == "SECRET"
35+
assert container.config.optional() == "default_value"

0 commit comments

Comments
 (0)