Skip to content

Commit 160b089

Browse files
committed
Update Pydantic docs and examples for v2
1 parent 6b627be commit 160b089

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

docs/providers/configuration.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,22 @@ See also: :ref:`configuration-envs-interpolation`.
183183
Loading from a Pydantic settings
184184
--------------------------------
185185

186-
``Configuration`` provider can load configuration from a ``pydantic`` settings object using the
186+
``Configuration`` provider can load configuration from a ``pydantic_settings.BaseSettings`` object using the
187187
:py:meth:`Configuration.from_pydantic` method:
188188

189189
.. literalinclude:: ../../examples/providers/configuration/configuration_pydantic.py
190190
:language: python
191191
:lines: 3-
192-
:emphasize-lines: 31
192+
:emphasize-lines: 32
193193

194-
To get the data from pydantic settings ``Configuration`` provider calls ``Settings.dict()`` method.
194+
To get the data from pydantic settings ``Configuration`` provider calls its ``model_dump()`` method.
195195
If you need to pass an argument to this call, use ``.from_pydantic()`` keyword arguments.
196196

197197
.. code-block:: python
198198
199199
container.config.from_pydantic(Settings(), exclude={"optional"})
200200
201-
Alternatively, you can provide a ``pydantic`` settings object over the configuration provider argument. In that case,
201+
Alternatively, you can provide a ``pydantic_settings.BaseSettings`` object over the configuration provider argument. In that case,
202202
the container will call ``config.from_pydantic()`` automatically:
203203

204204
.. code-block:: python
@@ -215,18 +215,23 @@ the container will call ``config.from_pydantic()`` automatically:
215215
216216
.. note::
217217

218-
``Dependency Injector`` doesn't install ``pydantic`` by default.
218+
``Dependency Injector`` doesn't install ``pydantic-settings`` by default.
219219

220220
You can install the ``Dependency Injector`` with an extra dependency::
221221

222-
pip install dependency-injector[pydantic]
222+
pip install dependency-injector[pydantic2]
223223

224-
or install ``pydantic`` directly::
224+
or install ``pydantic-settings`` directly::
225225

226-
pip install pydantic
226+
pip install pydantic-settings
227227

228228
*Don't forget to mirror the changes in the requirements file.*
229229

230+
.. note::
231+
232+
For backward-compatibility, Pydantic v1 is still supported.
233+
Passing ``pydantic.BaseSettings`` instances will work just as fine as ``pydantic_settings.BaseSettings``.
234+
230235
Loading from a dictionary
231236
-------------------------
232237

examples/providers/configuration/configuration_pydantic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
import os
44

55
from dependency_injector import containers, providers
6-
from pydantic import BaseSettings, Field
6+
from pydantic_settings import BaseSettings, SettingsConfigDict
77

88
# Emulate environment variables
99
os.environ["AWS_ACCESS_KEY_ID"] = "KEY"
1010
os.environ["AWS_SECRET_ACCESS_KEY"] = "SECRET"
1111

1212

1313
class AwsSettings(BaseSettings):
14+
model_config = SettingsConfigDict(env_prefix="aws_")
1415

15-
access_key_id: str = Field(env="aws_access_key_id")
16-
secret_access_key: str = Field(env="aws_secret_access_key")
16+
access_key_id: str
17+
secret_access_key: str
1718

1819

1920
class Settings(BaseSettings):
2021

2122
aws: AwsSettings = AwsSettings()
22-
optional: str = Field(default="default_value")
23+
optional: str = "default_value"
2324

2425

2526
class Container(containers.DeclarativeContainer):

examples/providers/configuration/configuration_pydantic_init.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
import os
44

55
from dependency_injector import containers, providers
6-
from pydantic import BaseSettings, Field
6+
from pydantic_settings import BaseSettings, SettingsConfigDict
77

88
# Emulate environment variables
99
os.environ["AWS_ACCESS_KEY_ID"] = "KEY"
1010
os.environ["AWS_SECRET_ACCESS_KEY"] = "SECRET"
1111

1212

1313
class AwsSettings(BaseSettings):
14+
model_config = SettingsConfigDict(env_prefix="aws_")
1415

15-
access_key_id: str = Field(env="aws_access_key_id")
16-
secret_access_key: str = Field(env="aws_secret_access_key")
16+
access_key_id: str
17+
secret_access_key: str
1718

1819

1920
class Settings(BaseSettings):
2021

2122
aws: AwsSettings = AwsSettings()
22-
optional: str = Field(default="default_value")
23+
optional: str = "default_value"
2324

2425

2526
class Container(containers.DeclarativeContainer):

0 commit comments

Comments
 (0)