@@ -212,12 +212,12 @@ In case of nested models, the `case_sensitive` setting will be applied to all ne
212
212
``` py
213
213
import os
214
214
215
- from pydantic import ValidationError
215
+ from pydantic import BaseModel, ValidationError
216
216
217
217
from pydantic_settings import BaseSettings
218
218
219
219
220
- class RedisSettings (BaseSettings ):
220
+ class RedisSettings (BaseModel ):
221
221
host: str
222
222
port: int
223
223
@@ -235,13 +235,10 @@ try:
235
235
except ValidationError as e:
236
236
print (e)
237
237
"""
238
- 2 validation errors for Settings
238
+ 1 validation error for Settings
239
239
redis.host
240
240
Field required [type=missing, input_value={'HOST': 'localhost', 'port': 6379}, input_type=dict]
241
241
For further information visit https://errors.pydantic.dev/2/v/missing
242
- redis.HOST
243
- Extra inputs are not permitted [type=extra_forbidden, input_value='localhost', input_type=str]
244
- For further information visit https://errors.pydantic.dev/2/v/extra_forbidden
245
242
"""
246
243
```
247
244
@@ -270,6 +267,10 @@ What it does is simply explodes your variable into nested models or dicts.
270
267
So if you define a variable ` FOO__BAR__BAZ=123 ` it will convert it into ` FOO={'BAR': {'BAZ': 123}} `
271
268
If you have multiple variables with the same structure they will be merged.
272
269
270
+ !!! note
271
+ Sub model has to inherit from ` pydantic.BaseModel ` , Otherwise ` pydantic-settings ` will initialize sub model,
272
+ collects values for sub model fields separately, and you may get unexpected results.
273
+
273
274
As an example, given the following environment variables:
274
275
``` bash
275
276
# your environment
@@ -315,11 +316,9 @@ print(Settings().model_dump())
315
316
"""
316
317
```
317
318
318
- 1 . Sub model has to inherit from ` pydantic.BaseModel ` , Otherwise ` pydantic-settings ` will initialize sub model,
319
- collects values for sub model fields separately, and you may get unexpected results.
319
+ 1 . Sub model has to inherit from ` pydantic.BaseModel ` .
320
320
321
- 2 . Sub model has to inherit from ` pydantic.BaseModel ` , Otherwise ` pydantic-settings ` will initialize sub model,
322
- collects values for sub model fields separately, and you may get unexpected results.
321
+ 2 . Sub model has to inherit from ` pydantic.BaseModel ` .
323
322
324
323
` env_nested_delimiter ` can be configured via the ` model_config ` as shown above, or via the
325
324
` _env_nested_delimiter ` keyword argument on instantiation.
@@ -460,6 +459,11 @@ class Settings(BaseSettings):
460
459
```
461
460
462
461
462
+ !!! note
463
+ Pydantic settings loads all the values from dotenv file and passes it to the model, regardless of the model's ` env_prefix ` .
464
+ So if you provide extra values in a dotenv file, whether they start with ` env_prefix ` or not,
465
+ a ` ValidationError ` will be raised.
466
+
463
467
## Secrets
464
468
465
469
Placing secret values in files is a common pattern to provide sensitive configuration to an application.
0 commit comments