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

Crash when YamlConfigSettingsSource file contains no items #329

Closed
darrenburns opened this issue Jul 1, 2024 · 2 comments · Fixed by #330
Closed

Crash when YamlConfigSettingsSource file contains no items #329

darrenburns opened this issue Jul 1, 2024 · 2 comments · Fixed by #330
Assignees
Labels
bug Something isn't working

Comments

@darrenburns
Copy link

darrenburns commented Jul 1, 2024

Hi,

I'm trying to use a yaml file as a settings source, and an exception is being raised when the file contains no items.

This does not occur with the toml settings source.

Here's an example:

from typing import Type
from pydantic import Field
from pydantic_settings import (
    BaseSettings,
    PydanticBaseSettingsSource,
    SettingsConfigDict,
    YamlConfigSettingsSource,
)

class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        extra="allow",
        yaml_file="config.yaml",
    )

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> tuple[PydanticBaseSettingsSource, ...]:
        return (
            init_settings,
            YamlConfigSettingsSource(settings_cls),
            env_settings,
            dotenv_settings,
            file_secret_settings,
        )

settings = Settings()

The file config.yaml is completely empty.

This crashes with the following exception:

Traceback (most recent call last):
  File "/Users/darrenburns/code/posting/.venv/bin/posting", line 5, in <module>
    from posting.__main__ import cli
  File "/Users/darrenburns/code/posting/src/posting/__main__.py", line 6, in <module>
    from posting.app import Posting
  File "/Users/darrenburns/code/posting/src/posting/app.py", line 35, in <module>
    from posting.config import Settings
  File "/Users/darrenburns/code/posting/src/posting/config.py", line 45, in <module>
    settings = Settings()
               ^^^^^^^^^^
  File "/Users/darrenburns/code/posting/.venv/lib/python3.11/site-packages/pydantic_settings/main.py", line 141, in __init__
    **__pydantic_self__._settings_build_values(
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/darrenburns/code/posting/.venv/lib/python3.11/site-packages/pydantic_settings/main.py", line 284, in _settings_build_values
    sources = self.settings_customise_sources(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/darrenburns/code/posting/src/posting/config.py", line 38, in settings_customise_sources
    YamlConfigSettingsSource(settings_cls),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/darrenburns/code/posting/.venv/lib/python3.11/site-packages/pydantic_settings/sources.py", line 1673, in __init__
    self.yaml_data = self._read_files(self.yaml_file_path)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/darrenburns/code/posting/.venv/lib/python3.11/site-packages/pydantic_settings/sources.py", line 1555, in _read_files
    vars.update(self._read_file(file_path))
TypeError: 'NoneType' object is not iterable

Versions:

pydantic==2.7.3
pydantic-core==2.18.4
pydantic-settings==2.3.4
@darrenburns darrenburns changed the title Crash when YamlConfigSettingsSource file is empty Crash when YamlConfigSettingsSource file contains no items Jul 1, 2024
@hramezani
Copy link
Member

Thanks @darrenburns for reporting this.

I created #330 as a fix

@hramezani hramezani assigned hramezani and unassigned dmontagu Jul 1, 2024
@hramezani hramezani added bug Something isn't working and removed unconfirmed labels Jul 1, 2024
@darrenburns
Copy link
Author

Thanks @hramezani!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants