Skip to content

Support using a table header as root when reading a TOML file - #882

Merged
hramezani merged 5 commits into
pydantic:mainfrom
whyscream:toml-settings-source-with-table-header
Jun 7, 2026
Merged

hramezani merged 5 commits into
pydantic:mainfrom
whyscream:toml-settings-source-with-table-header

Conversation

@whyscream

Copy link
Copy Markdown
Contributor

Support toml_table_header for regular TOML files.

It can be set from model_config or from settings_customise_sources.

Resolves #881

@whyscream
whyscream force-pushed the toml-settings-source-with-table-header branch 2 times, most recently from 06dd021 to e7b7f78 Compare June 4, 2026 23:57
Comment thread tests/test_source_toml.py

s = Settings()
assert s.model_dump() == {'hello': 'world', 'nested': {'foo': 3, 'bar': 2 if deep_merge else 0}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for the case where toml_table_header references a key that doesn't exist in the TOML file? Currently self.toml_data.get(key, {}) silently returns {}, which would result in missing/default field values. It'd be good to have a test that documents this behavior explicitly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hramezani Nice catch! I added a test for the missing table header, and an explicit error, based on how it's done with yaml_config_section.

@hramezani

Copy link
Copy Markdown
Contributor

Thanks @whyscream for the PR.

toml_table_header should also be added to the _settings_warn_unused_config_keys check in main.py.
Currently line 560 only checks for ('toml_file',):

warn_if_not_used(TomlConfigSettingsSource, ('toml_file',))

It should be:

warn_if_not_used(TomlConfigSettingsSource, ('toml_file', 'toml_table_header'))

Without this, if a user sets toml_table_header in model_config but forgets to add a TomlConfigSettingsSource to their sources, they won't get the expected warning.

@whyscream
whyscream force-pushed the toml-settings-source-with-table-header branch from 1f66cd4 to 247f39d Compare June 5, 2026 10:23
@hramezani

Copy link
Copy Markdown
Contributor

Thanks @whyscream

@hramezani
hramezani enabled auto-merge (squash) June 7, 2026 14:37
@hramezani
hramezani merged commit afda1f7 into pydantic:main Jun 7, 2026
19 checks passed
@whyscream
whyscream deleted the toml-settings-source-with-table-header branch June 8, 2026 06:22
@hramezani hramezani mentioned this pull request Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TomlConfigSettingsSource should support toml_table_header

2 participants