Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: issubclass() arg 1 must be a class for list[] config #395

Closed
duytrnq opened this issue Sep 11, 2024 · 5 comments
Closed

TypeError: issubclass() arg 1 must be a class for list[] config #395

duytrnq opened this issue Sep 11, 2024 · 5 comments
Assignees
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@duytrnq
Copy link

duytrnq commented Sep 11, 2024

After upgrading to version 2.5.1 I still of TypeError when init class with list[type] it will raise TypeError: issubclass() arg 1 must be a class which is supposed to be fixed
Code

from pydantic_settings import BaseSettings

class TestConfig(BaseSettings):
    cors_origins: list[str] | None = ["*"]
    cors_headers: list[str] | None = ["*"]
    cors_methods: list[str] | None = ["*"]

def main():
    config = TestConfig()
    print(config.cors_origins)

if __name__ == "__main__":
    main()

Traceback

Traceback (most recent call last):
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 538, in __call__
    field_value, field_key, value_is_complex = self.get_field_value(field, field_name)
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 714, in get_field_value
    for field_key, env_name, value_is_complex in self._extract_field_info(field, field_name):
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 443, in _extract_field_info
    elif origin_is_union(get_origin(field.annotation)) and _union_is_complex(field.annotation, field.metadata):
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 2152, in _union_is_complex
    return any(_annotation_is_complex(arg, metadata) for arg in get_args(annotation))
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 2152, in <genexpr>
    return any(_annotation_is_complex(arg, metadata) for arg in get_args(annotation))
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 2118, in _annotation_is_complex
    if annotation is not None and inspect.isclass(annotation) and issubclass(annotation, RootModel):
  File "/usr/lib/python3.10/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    main()
  File "test.py", line 25, in main
    config = TestConfig()
  File "venv/lib/python3.10/site-packages/pydantic_settings/main.py", line 153, in __init__
    **__pydantic_self__._settings_build_values(
  File "venv/lib/python3.10/site-packages/pydantic_settings/main.py", line 357, in _settings_build_values
    source_state = source()
  File "venv/lib/python3.10/site-packages/pydantic_settings/sources.py", line 540, in __call__
    raise SettingsError(
pydantic_settings.sources.SettingsError: error getting value for field "cors_origins" from source "EnvSettingsSource"

Here is my log from pip list

$ pip list | grep pydantic
pydantic              2.8.2
pydantic_core         2.20.1
pydantic-extra-types  2.9.0
pydantic-settings     2.5.1

[notice] A new release of pip is available: 23.2.1 -> 24.2
[notice] To update, run: pip install --upgrade pip
@pgiraud
Copy link

pgiraud commented Sep 11, 2024

Probably duplicate of #390

@hramezani
Copy link
Member

Thanks @duytrnq for reporting. I will check it and prepare a fix soon

@hramezani hramezani added bug Something isn't working duplicate This issue or pull request already exists and removed unconfirmed labels Sep 11, 2024
@hramezani
Copy link
Member

Closing this issue and we can follow on #390

@hramezani
Copy link
Member

Here is the patch #396

@hramezani
Copy link
Member

The second patch has been released in pydantic-settings 2.5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants