Skip to content

Commit

Permalink
Fix Secret field parsing (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani authored Nov 18, 2024
1 parent e8a15b0 commit 6fe3bd1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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')

0 comments on commit 6fe3bd1

Please sign in to comment.