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
6 changes: 5 additions & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import typing_extensions
from dotenv import dotenv_values
from pydantic import AliasChoices, AliasPath, BaseModel, Json, RootModel, TypeAdapter
from pydantic import AliasChoices, AliasPath, BaseModel, Json, RootModel, Secret, TypeAdapter
from pydantic._internal._repr import Representation
from pydantic._internal._typing_extra import WithArgsTypes, origin_is_union, typing_base
from pydantic._internal._utils import deep_update, is_model_class, lenient_issubclass
Expand Down Expand Up @@ -2202,6 +2202,10 @@ def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) ->
inner, *meta = get_args(annotation)
return _annotation_is_complex(inner, meta)
origin = get_origin(annotation)

if origin is Secret:
return False

return (
_annotation_is_complex_inner(annotation)
or _annotation_is_complex_inner(origin)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
Field,
HttpUrl,
Json,
PostgresDsn,
RootModel,
Secret,
SecretStr,
Tag,
ValidationError,
Expand Down Expand Up @@ -2856,3 +2858,16 @@ class Settings(BaseSettings):

s = Settings()
assert s.model_dump() == {'foo': 'test-foo'}


def test_parsing_secret_field(env):
class Settings(BaseSettings):
foo: Secret[int]
bar: Secret[PostgresDsn]

env.set('foo', '123')
env.set('bar', 'postgres://user:password@localhost/dbname')

s = Settings()
assert s.foo.get_secret_value() == 123
assert s.bar.get_secret_value() == PostgresDsn('postgres://user:password@localhost/dbname')
Loading