Skip to content

Commit 81d7c46

Browse files
authored
Second fix for the TypeError bug introduced in 2.5 (#396)
1 parent b2c0ee2 commit 81d7c46

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

pydantic_settings/sources.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,13 +2115,16 @@ def read_env_file(
21152115
def _annotation_is_complex(annotation: type[Any] | None, metadata: list[Any]) -> bool:
21162116
# If the model is a root model, the root annotation should be used to
21172117
# evaluate the complexity.
2118-
if annotation is not None and inspect.isclass(annotation) and issubclass(annotation, RootModel):
2119-
# In some rare cases (see test_root_model_as_field),
2120-
# the root attribute is not available. For these cases, python 3.8 and 3.9
2121-
# return 'RootModelRootType'.
2122-
root_annotation = annotation.__annotations__.get('root', None)
2123-
if root_annotation is not None and root_annotation != 'RootModelRootType':
2124-
annotation = root_annotation
2118+
try:
2119+
if annotation is not None and issubclass(annotation, RootModel):
2120+
# In some rare cases (see test_root_model_as_field),
2121+
# the root attribute is not available. For these cases, python 3.8 and 3.9
2122+
# return 'RootModelRootType'.
2123+
root_annotation = annotation.__annotations__.get('root', None)
2124+
if root_annotation is not None and root_annotation != 'RootModelRootType':
2125+
annotation = root_annotation
2126+
except TypeError:
2127+
pass
21252128

21262129
if any(isinstance(md, Json) for md in metadata): # type: ignore[misc]
21272130
return False

tests/test_settings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4994,3 +4994,13 @@ class Settings(BaseSettings):
49944994
s = Settings()
49954995
assert s.POSTGRES_USER == 'postgres'
49964996
assert s.model_dump() == {'POSTGRES_USER': 'postgres', 'postgres_name': 'name', 'postgres_user_2': 'postgres2'}
4997+
4998+
4999+
@pytest.mark.skipif(sys.version_info < (3, 9), reason='requires python 3.9 or higher')
5000+
def test_annotation_is_complex_root_model_check():
5001+
"""Test for https://github.com/pydantic/pydantic-settings/issues/390"""
5002+
5003+
class Settings(BaseSettings):
5004+
foo: list[str] = []
5005+
5006+
Settings()

0 commit comments

Comments
 (0)