pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
593 stars 61 forks source link

Clarification Needed on pyproject_toml_table_header Logic in Pydantic Settings #434

Open py-mu opened 5 days ago

py-mu commented 5 days ago

Issue Context

I tried to use pydantic-settings for project configuration management, but I couldn't understand why the pyproject_toml_table_header is restricted to a single block.

        self.toml_table_header: tuple[str, ...] = settings_cls.model_config.get(
            'pyproject_toml_table_header', ('tool', 'pydantic-settings')
        )
        self.toml_data = self._read_files(self.toml_file_path)
        for key in self.toml_table_header:
            self.toml_data = self.toml_data.get(key, {})
        super(TomlConfigSettingsSource, self).__init__(settings_cls, self.toml_data)

If multiple headers are provided, this logic seems to overwrite toml_data repeatedly, resulting in toml_data containing content from only one header. Is my understanding correct?

Are there any alternative logics to better handle this content? For instance, would it be more appropriate to use something like:

self.toml_data = {k:v for k, v in self.toml_data.items() if k in self.toml_table_header}

or other

hramezani commented 3 days ago

Thanks @py-mu for reporting this issue.

Could you please explain more? something like an example pyproject file and a setting model would be helpful.