Description
There are issues with the typing for the file options for SettingsConfigDict
:
- They are typed as
PathType | None
despite documentation indicating they accept lists (which indeed they do).
pydantic-settings/pydantic_settings/main.py
Lines 43 to 46 in bcbfd17
The documentation indicates this at: https://docs.pydantic.dev/latest/concepts/pydantic_settings/#other-settings-source
PathType
could, but does not not includeimportlib.resources.abc.Traversable
:
I should note that if one provides a Traversable
, it does currently work, indicating the subset of Path
functionality used by pydantic-settings
is within the Traversable
subset of Path
.
In the example below, I pass a Traversable
, and I have verified in a test that it does indeed load the packaged resource file just fine.
Motivation
A somewhat useful pattern is to package a default configuration file inside a Python package. For example:
class FoobarSettings(BaseSettings):
data_dir: Path
model_config = SettingsConfigDict(
env_prefix="foobar_",
toml_file=[
resources.files("foobar").joinpath("foobar.defaults.toml"),
"foobar.toml"
],
)
@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, ...]:
default_sources = super().settings_customise_sources(
settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings
)
return *default_sources, TomlConfigSettingsSource(settings_cls)
And then have in src/foobar/foobar.defaults.toml
:
data_dir = ".foobar/data"
This currently works. However, it will not type-check.