Skip to content

Commit 0a00678

Browse files
authored
Improve docs (#225)
1 parent 40230ab commit 0a00678

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

docs/index.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ In case of nested models, the `case_sensitive` setting will be applied to all ne
212212
```py
213213
import os
214214

215-
from pydantic import ValidationError
215+
from pydantic import BaseModel, ValidationError
216216

217217
from pydantic_settings import BaseSettings
218218

219219

220-
class RedisSettings(BaseSettings):
220+
class RedisSettings(BaseModel):
221221
host: str
222222
port: int
223223

@@ -235,13 +235,10 @@ try:
235235
except ValidationError as e:
236236
print(e)
237237
"""
238-
2 validation errors for Settings
238+
1 validation error for Settings
239239
redis.host
240240
Field required [type=missing, input_value={'HOST': 'localhost', 'port': 6379}, input_type=dict]
241241
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
245242
"""
246243
```
247244

@@ -270,6 +267,10 @@ What it does is simply explodes your variable into nested models or dicts.
270267
So if you define a variable `FOO__BAR__BAZ=123` it will convert it into `FOO={'BAR': {'BAZ': 123}}`
271268
If you have multiple variables with the same structure they will be merged.
272269

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+
273274
As an example, given the following environment variables:
274275
```bash
275276
# your environment
@@ -315,11 +316,9 @@ print(Settings().model_dump())
315316
"""
316317
```
317318

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`.
320320

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`.
323322

324323
`env_nested_delimiter` can be configured via the `model_config` as shown above, or via the
325324
`_env_nested_delimiter` keyword argument on instantiation.
@@ -460,6 +459,11 @@ class Settings(BaseSettings):
460459
```
461460

462461

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+
463467
## Secrets
464468

465469
Placing secret values in files is a common pattern to provide sensitive configuration to an application.

0 commit comments

Comments
 (0)