Skip to content

Commit

Permalink
Second fix for the TypeError bug introduced in 2.5 (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani authored Sep 11, 2024
1 parent b2c0ee2 commit 81d7c46
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
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()

0 comments on commit 81d7c46

Please sign in to comment.