Skip to content

Commit 5748f06

Browse files
committed
Address comments
1 parent 0340603 commit 5748f06

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

docs/index.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,9 @@ print(Settings().model_dump())
373373

374374
### Disabling JSON parsing
375375

376-
pydatnic-settings by default parses complex types from environment variables as JSON strings. If you want to disable
377-
this behavior for a field and parse the value by your own, you can annotate the field with `NoDecode`:
376+
pydantic-settings by default parses complex types from environment variables as JSON strings. If you want to disable
377+
this behavior for a field and parse the value in your own validator, you can annotate the field with
378+
[`NoDecode`](../api/pydantic_settings.md#pydantic_settings.NoDecode):
378379

379380
```py
380381
import os
@@ -430,13 +431,14 @@ print(Settings().model_dump())
430431
#> {'numbers': [1, 2, 3]}
431432
```
432433

433-
You can force JSON parsing for a field by annotating it with `ForceDecode`. This will bypass
434-
the the `enable_decoding` config setting:
434+
You can force JSON parsing for a field by annotating it with [`ForceDecode`](../api/pydantic_settings.md#pydantic_settings.ForceDecode).
435+
This will bypass the the `enable_decoding` config setting:
435436

436437
```py
437438
import os
438439
from typing import List
439440

441+
from pydantic import field_validator
440442
from typing_extensions import Annotated
441443

442444
from pydantic_settings import BaseSettings, ForceDecode, SettingsConfigDict
@@ -446,13 +448,23 @@ class Settings(BaseSettings):
446448
model_config = SettingsConfigDict(enable_decoding=False)
447449

448450
numbers: Annotated[List[int], ForceDecode]
451+
numbers1: List[int] # (1)!
452+
453+
@field_validator('numbers1', mode='before')
454+
@classmethod
455+
def decode_numbers1(cls, v: str) -> List[int]:
456+
return [int(x) for x in v.split(',')]
449457

450458

451459
os.environ['numbers'] = '["1","2","3"]'
460+
os.environ['numbers1'] = '1,2,3'
452461
print(Settings().model_dump())
453-
#> {'numbers': [1, 2, 3]}
462+
#> {'numbers': [1, 2, 3], 'numbers1': [1, 2, 3]}
454463
```
455464

465+
1. The `numbers1` field is not annotated with `ForceDecode`, so it will not be parsed as JSON.
466+
and we have to provide a custom validator to parse the value.
467+
456468
## Nested model default partial updates
457469

458470
By default, Pydantic settings does not allow partial updates to nested model default objects. This behavior can be

pydantic_settings/sources.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ def import_azure_key_vault() -> None:
119119

120120

121121
class NoDecode:
122+
"""Annotation to prevent decoding of a field value."""
123+
122124
pass
123125

124126

125127
class ForceDecode:
128+
"""Annotation to force decoding of a field value."""
129+
126130
pass
127131

128132

0 commit comments

Comments
 (0)