Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pydantic_settings/sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .utils import (
_annotation_is_complex,
_get_alias_names,
_get_field_metadata,
_get_model_fields,
_strip_annotated,
_union_is_complex,
Expand Down Expand Up @@ -180,7 +181,7 @@ def decode_complex_value(self, field_name: str, field: FieldInfo, value: Any) ->
The decoded value for further preparation
"""
if field and (
NoDecode in field.metadata
NoDecode in _get_field_metadata(field)
or (self.config.get('enable_decoding') is False and ForceDecode not in field.metadata)
):
return value
Expand Down
12 changes: 12 additions & 0 deletions pydantic_settings/sources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ def _annotation_is_complex(annotation: Any, metadata: list[Any]) -> bool:
)


def _get_field_metadata(field: Any) -> list[Any]:
annotation = field.annotation
metadata = field.metadata
if typing_objects.is_typealiastype(annotation) or typing_objects.is_typealiastype(get_origin(annotation)):
annotation = annotation.__value__
origin = get_origin(annotation)
if typing_objects.is_annotated(origin):
_, *meta = get_args(annotation)
metadata += meta
return metadata


def _annotation_is_complex_inner(annotation: type[Any] | None) -> bool:
if _lenient_issubclass(annotation, (str, bytes)):
return False
Expand Down
18 changes: 18 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,24 @@ class AnnotatedComplexSettings(BaseSettings):
assert s.apples == ['russet', 'granny smith']


def test_annotated_with_type_no_decode(env):
A = TypeAliasType('A', Annotated[list[str], NoDecode])

class Settings(BaseSettings):
a: A

# decode the value here. the field value won't be decoded because of NoDecode
@field_validator('a', mode='before')
@classmethod
def decode_a(cls, v: str) -> list[str]:
return json.loads(v)

env.set('a', '["one", "two"]')

s = Settings()
assert s.model_dump() == {'a': ['one', 'two']}


def test_set_dict_model(env):
env.set('bananas', '[1, 2, 3, 3]')
env.set('CARROTS', '{"a": null, "b": 4}')
Expand Down