Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Dec 4, 2024
1 parent 0340603 commit 5748f06
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 17 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ print(Settings().model_dump())

### Disabling JSON parsing

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

```py
import os
Expand Down Expand Up @@ -430,13 +431,14 @@ print(Settings().model_dump())
#> {'numbers': [1, 2, 3]}
```

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

```py
import os
from typing import List

from pydantic import field_validator
from typing_extensions import Annotated

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

numbers: Annotated[List[int], ForceDecode]
numbers1: List[int] # (1)!

@field_validator('numbers1', mode='before')
@classmethod
def decode_numbers1(cls, v: str) -> List[int]:
return [int(x) for x in v.split(',')]


os.environ['numbers'] = '["1","2","3"]'
os.environ['numbers1'] = '1,2,3'
print(Settings().model_dump())
#> {'numbers': [1, 2, 3]}
#> {'numbers': [1, 2, 3], 'numbers1': [1, 2, 3]}
```

1. The `numbers1` field is not annotated with `ForceDecode`, so it will not be parsed as JSON.
and we have to provide a custom validator to parse the value.

## Nested model default partial updates

By default, Pydantic settings does not allow partial updates to nested model default objects. This behavior can be
Expand Down
4 changes: 4 additions & 0 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ def import_azure_key_vault() -> None:


class NoDecode:
"""Annotation to prevent decoding of a field value."""

pass


class ForceDecode:
"""Annotation to force decoding of a field value."""

pass


Expand Down

0 comments on commit 5748f06

Please sign in to comment.