diff --git a/pydantic_settings/sources.py b/pydantic_settings/sources.py index dad37dd..4b0e984 100644 --- a/pydantic_settings/sources.py +++ b/pydantic_settings/sources.py @@ -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 @@ -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) diff --git a/tests/test_settings.py b/tests/test_settings.py index e266093..2179817 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -20,7 +20,9 @@ Field, HttpUrl, Json, + PostgresDsn, RootModel, + Secret, SecretStr, Tag, ValidationError, @@ -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')