Skip to content

Commit 6fe3bd1

Browse files
authored
Fix Secret field parsing (#478)
1 parent e8a15b0 commit 6fe3bd1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pydantic_settings/sources.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
import typing_extensions
3939
from dotenv import dotenv_values
40-
from pydantic import AliasChoices, AliasPath, BaseModel, Json, RootModel, TypeAdapter
40+
from pydantic import AliasChoices, AliasPath, BaseModel, Json, RootModel, Secret, TypeAdapter
4141
from pydantic._internal._repr import Representation
4242
from pydantic._internal._typing_extra import WithArgsTypes, origin_is_union, typing_base
4343
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]) ->
22022202
inner, *meta = get_args(annotation)
22032203
return _annotation_is_complex(inner, meta)
22042204
origin = get_origin(annotation)
2205+
2206+
if origin is Secret:
2207+
return False
2208+
22052209
return (
22062210
_annotation_is_complex_inner(annotation)
22072211
or _annotation_is_complex_inner(origin)

tests/test_settings.py

+15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
Field,
2121
HttpUrl,
2222
Json,
23+
PostgresDsn,
2324
RootModel,
25+
Secret,
2426
SecretStr,
2527
Tag,
2628
ValidationError,
@@ -2856,3 +2858,16 @@ class Settings(BaseSettings):
28562858

28572859
s = Settings()
28582860
assert s.model_dump() == {'foo': 'test-foo'}
2861+
2862+
2863+
def test_parsing_secret_field(env):
2864+
class Settings(BaseSettings):
2865+
foo: Secret[int]
2866+
bar: Secret[PostgresDsn]
2867+
2868+
env.set('foo', '123')
2869+
env.set('bar', 'postgres://user:password@localhost/dbname')
2870+
2871+
s = Settings()
2872+
assert s.foo.get_secret_value() == 123
2873+
assert s.bar.get_secret_value() == PostgresDsn('postgres://user:password@localhost/dbname')

0 commit comments

Comments
 (0)