diff --git a/pydantic_settings/sources.py b/pydantic_settings/sources.py index 30e7fa33..7400b8f0 100644 --- a/pydantic_settings/sources.py +++ b/pydantic_settings/sources.py @@ -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 diff --git a/tests/test_settings.py b/tests/test_settings.py index d91695b3..62f073c6 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -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()