Skip to content
Merged
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 @@ -24,6 +24,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 @@ -179,7 +180,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
13 changes: 13 additions & 0 deletions pydantic_settings/sources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydantic import BaseModel, Json, RootModel, Secret
from pydantic._internal._utils import is_model_class
from pydantic.dataclasses import is_pydantic_dataclass
from pydantic.fields import FieldInfo
from typing_inspection import typing_objects

from ..exceptions import SettingsError
Expand Down Expand Up @@ -72,6 +73,18 @@ def _annotation_is_complex(annotation: Any, metadata: list[Any]) -> bool:
)


def _get_field_metadata(field: FieldInfo) -> 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__ # type: ignore[union-attr]
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 @@ -499,6 +499,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