Skip to content

Commit

Permalink
Use SettingsErrors when file can't be used as source
Browse files Browse the repository at this point in the history
Some file formats (at least yaml) allow to represent non dictionnary
values. In such situation we can't add the values read from those files.
Instead of raising a ValueError, we now raise a SettingsError and
indicate we can't parse the related config file.
  • Loading branch information
Julian Vanden Broeck committed Oct 8, 2024
1 parent 0d605d0 commit e49dbbd
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,15 @@ def _read_files(self, files: PathType | None) -> dict[str, Any]:
for file in files:
file_path = Path(file).expanduser()
if file_path.is_file():
vars.update(self._read_file(file_path))
try:
settings = self._read_file(file_path)
except ValueError as e:
raise SettingsError(f'Failed to parse settings from {file_path}, {e}')
if not isinstance(settings, dict):
raise SettingsError(
f'Failed to parse settings from {file_path}, expecting an object (valid dictionnary)'
)
vars.update(settings)
return vars

@abstractmethod
Expand Down
29 changes: 29 additions & 0 deletions tests/test_source_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import json
from typing import Tuple, Type, Union

import pytest
from pydantic import BaseModel

from pydantic_settings import (
BaseSettings,
JsonConfigSettingsSource,
PydanticBaseSettingsSource,
SettingsConfigDict,
SettingsError,
)


Expand Down Expand Up @@ -67,6 +69,33 @@ def settings_customise_sources(
assert s.model_dump() == {}


def test_nondict_json_file(tmp_path):
p = tmp_path / '.env'
p.write_text(
"""
"noway"
"""
)

class Settings(BaseSettings):
foobar: str
model_config = SettingsConfigDict(json_file=p)

@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 (JsonConfigSettingsSource(settings_cls),)

with pytest.raises(SettingsError, match=f'Failed to parse settings from {p}, expecting an object'):
s = Settings()


def test_multiple_file_json(tmp_path):
p5 = tmp_path / '.env.json5'
p6 = tmp_path / '.env.json6'
Expand Down
25 changes: 25 additions & 0 deletions tests/test_source_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
SettingsError,
TomlConfigSettingsSource,
)

Expand Down Expand Up @@ -77,6 +78,30 @@ def settings_customise_sources(
assert s.model_dump() == {}


@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
def test_pyproject_nondict_toml(cd_tmp_path):
pyproject = cd_tmp_path / 'pyproject.toml'
pyproject.write_text(
"""
[tool.pydantic-settings]
foobar
"""
)

class Settings(BaseSettings):
foobar: str
model_config = SettingsConfigDict()

@classmethod
def settings_customise_sources(
cls, settings_cls: Type[BaseSettings], **_kwargs: PydanticBaseSettingsSource
) -> Tuple[PydanticBaseSettingsSource, ...]:
return (TomlConfigSettingsSource(settings_cls, pyproject),)

with pytest.raises(SettingsError, match=f'Failed to parse settings from {pyproject}'):
Settings()


@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
def test_multiple_file_toml(tmp_path):
p1 = tmp_path / '.env.toml1'
Expand Down
25 changes: 25 additions & 0 deletions tests/test_source_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
SettingsError,
YamlConfigSettingsSource,
)

Expand Down Expand Up @@ -85,6 +86,30 @@ def settings_customise_sources(
assert s.nested.nested_field == 'world!'


@pytest.mark.skipif(yaml is None, reason='pyYaml is not installed')
def test_nondict_yaml_file(tmp_path):
p = tmp_path / '.env'
p.write_text('test invalid yaml')

class Settings(BaseSettings):
foobar: str
model_config = SettingsConfigDict(yaml_file=p)

@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 (YamlConfigSettingsSource(settings_cls),)

with pytest.raises(SettingsError, match=f'Failed to parse settings from {p}, expecting an object'):
Settings()


@pytest.mark.skipif(yaml is None, reason='pyYaml is not installed')
def test_yaml_no_file():
class Settings(BaseSettings):
Expand Down

0 comments on commit e49dbbd

Please sign in to comment.