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
17 changes: 10 additions & 7 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,13 +2115,16 @@ def read_env_file(
def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) -> bool:
# If the model is a root model, the root annotation should be used to
# evaluate the complexity.
if annotation is not None and inspect.isclass(annotation) and issubclass(annotation, RootModel):
# In some rare cases (see test_root_model_as_field),
# the root attribute is not available. For these cases, python 3.8 and 3.9
# return 'RootModelRootType'.
root_annotation = annotation.__annotations__.get('root', None)
if root_annotation is not None and root_annotation != 'RootModelRootType':
annotation = root_annotation
try:
if annotation is not None and issubclass(annotation, RootModel):
# In some rare cases (see test_root_model_as_field),
# the root attribute is not available. For these cases, python 3.8 and 3.9
# return 'RootModelRootType'.
root_annotation = annotation.__annotations__.get('root', None)
if root_annotation is not None and root_annotation != 'RootModelRootType':
annotation = root_annotation
except TypeError:
pass

if any(isinstance(md, Json) for md in metadata): # type: ignore[misc]
return False
Expand Down
10 changes: 10 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4994,3 +4994,13 @@ class Settings(BaseSettings):
s = Settings()
assert s.POSTGRES_USER == 'postgres'
assert s.model_dump() == {'POSTGRES_USER': 'postgres', 'postgres_name': 'name', 'postgres_user_2': 'postgres2'}


@pytest.mark.skipif(sys.version_info < (3, 9), reason='requires python 3.9 or higher')
def test_annotation_is_complex_root_model_check():
"""Test for https://github.com/pydantic/pydantic-settings/issues/390"""

class Settings(BaseSettings):
foo: list[str] = []

Settings()
Loading