Skip to content

Commit 6cc1a0c

Browse files
committed
Merge branch 'release/4.15.0' into master
2 parents 1fabbf3 + f48fd15 commit 6cc1a0c

File tree

14 files changed

+7941
-6618
lines changed

14 files changed

+7941
-6618
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ Key features of the ``Dependency Injector``:
5959
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
6060
and configuring dev / stage environment to replace API clients with stubs etc. See
6161
`Provider overriding <https://python-dependency-injector.ets-labs.org/providers/overriding.html>`_.
62-
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, environment variables
63-
and dictionaries.
62+
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
63+
environment variables, and dictionaries.
6464
See `Configuration provider <https://python-dependency-injector.ets-labs.org/providers/configuration.html>`_.
6565
- **Containers**. Provides declarative and dynamic containers.
6666
See `Containers <https://python-dependency-injector.ets-labs.org/containers/index.html>`_.

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ Key features of the ``Dependency Injector``:
7070
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
7171
and configuring dev / stage environment to replace API clients with stubs etc. See
7272
:ref:`provider-overriding`.
73-
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, environment variables
74-
and dictionaries. See :ref:`configuration-provider`.
73+
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
74+
environment variables, and dictionaries. See :ref:`configuration-provider`.
7575
- **Resources**. Helps with initialization and configuring of logging, event loop, thread
7676
or process pool, etc. Can be used for per-function execution scope in tandem with wiring.
7777
See :ref:`resource-provider`.

docs/introduction/key_features.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Key features of the ``Dependency Injector``:
1616
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
1717
and configuring dev / stage environment to replace API clients with stubs etc. See
1818
:ref:`provider-overriding`.
19-
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, environment variables
20-
and dictionaries. See :ref:`configuration-provider`.
19+
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
20+
environment variables, and dictionaries. See :ref:`configuration-provider`.
2121
- **Resources**. Helps with initialization and configuring of logging, event loop, thread
2222
or process pool, etc. Can be used for per-function execution scope in tandem with wiring.
2323
See :ref:`resource-provider`.

docs/main/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
4.15.0
11+
------
12+
- Add ``Configuration.from_pydantic()`` method to load configuration from a ``pydantic`` settings.
13+
1014
4.14.0
1115
------
1216
- Add container providers traversal.

docs/providers/configuration.rst

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Configuration provider
55

66
.. meta::
77
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Configuration,Injection,
8-
Option,Ini,Json,Yaml,Dict,Environment Variable,Load,Read,Get
8+
Option,Ini,Json,Yaml,Pydantic,Dict,Environment Variable,Load,Read,Get
99
:description: Configuration provides configuration options to the other providers. This page
1010
demonstrates how to use Configuration provider to inject the dependencies, load
11-
a configuration from an ini or yaml file, dictionary or an environment variable.
11+
a configuration from an ini or yaml file, a dictionary, an environment variable,
12+
or a pydantic settings object.
1213

1314
.. currentmodule:: dependency_injector.providers
1415

@@ -89,6 +90,38 @@ You can also specify a YAML loader as an argument:
8990

9091
*Don't forget to mirror the changes in the requirements file.*
9192

93+
Loading from a Pydantic settings
94+
--------------------------------
95+
96+
``Configuration`` provider can load configuration from a ``pydantic`` settings object using the
97+
:py:meth:`Configuration.from_pydantic` method:
98+
99+
.. literalinclude:: ../../examples/providers/configuration/configuration_pydantic.py
100+
:language: python
101+
:lines: 3-
102+
:emphasize-lines: 31
103+
104+
To get the data from pydantic settings ``Configuration`` provider calls ``Settings.dict()`` method.
105+
If you need to pass an argument to this call, use ``.from_pydantic()`` keyword arguments.
106+
107+
.. code-block:: python
108+
109+
container.config.from_pydantic(Settings(), exclude={'optional'})
110+
111+
.. note::
112+
113+
``Dependency Injector`` doesn't install ``pydantic`` by default.
114+
115+
You can install the ``Dependency Injector`` with an extra dependency::
116+
117+
pip install dependency-injector[pydantic]
118+
119+
or install ``pydantic`` directly::
120+
121+
pip install pydantic
122+
123+
*Don't forget to mirror the changes in the requirements file.*
124+
92125
Loading from a dictionary
93126
-------------------------
94127

@@ -211,7 +244,7 @@ Methods ``.from_*()`` in strict mode raise an exception if configuration file do
211244
configuration data is undefined:
212245

213246
.. code-block:: python
214-
:emphasize-lines: 10,15,20,25
247+
:emphasize-lines: 10,15,20,25,30
215248
216249
class Container(containers.DeclarativeContainer):
217250
@@ -231,6 +264,11 @@ configuration data is undefined:
231264
except FileNotFoundError:
232265
...
233266
267+
try:
268+
container.config.from_pydantic(EmptySettings()) # raise exception
269+
except ValueError:
270+
...
271+
234272
try:
235273
container.config.from_env('UNDEFINED_ENV_VAR') # raise exception
236274
except ValueError:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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()
28+
29+
30+
if __name__ == '__main__':
31+
container = Container()
32+
33+
container.config.from_pydantic(Settings())
34+
35+
assert container.config.aws.access_key_id() == 'KEY'
36+
assert container.config.aws.secret_access_key() == 'SECRET'
37+
assert container.config.optional() == 'default_value'

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ mypy
1010
pyyaml
1111
httpx
1212
fastapi
13+
pydantic
1314

1415
-r requirements-ext.txt

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
'yaml': [
6565
'pyyaml',
6666
],
67+
'pydantic': [
68+
'pydantic',
69+
],
6770
'flask': [
6871
'flask',
6972
],

src/dependency_injector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Top-level package."""
22

3-
__version__ = '4.14.0'
3+
__version__ = '4.15.0'
44
"""Version number.
55
66
:type: str

0 commit comments

Comments
 (0)