1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
"""
Test pydantic_settings.TomlConfigSettingsSource.
"""
import sys
from pathlib import Path
import pytest
from pydantic import BaseModel
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
TomlConfigSettingsSource,
)
try:
import tomli
except ImportError:
tomli = None
def test_repr() -> None:
source = TomlConfigSettingsSource(BaseSettings, Path('config.toml'))
assert repr(source) == 'TomlConfigSettingsSource(toml_file=config.toml)'
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
def test_toml_file(tmp_path):
p = tmp_path / '.env'
p.write_text(
"""
foobar = "Hello"
[nested]
nested_field = "world!"
"""
)
class Nested(BaseModel):
nested_field: str
class Settings(BaseSettings):
foobar: str
nested: Nested
model_config = SettingsConfigDict(toml_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 (TomlConfigSettingsSource(settings_cls),)
s = Settings()
assert s.foobar == 'Hello'
assert s.nested.nested_field == 'world!'
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
def test_toml_no_file():
class Settings(BaseSettings):
model_config = SettingsConfigDict(toml_file=None)
@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 (TomlConfigSettingsSource(settings_cls),)
s = Settings()
assert s.model_dump() == {}
@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'
p2 = tmp_path / '.env.toml2'
p1.write_text(
"""
toml1=1
"""
)
p2.write_text(
"""
toml2=2
"""
)
class Settings(BaseSettings):
toml1: int
toml2: int
@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 (TomlConfigSettingsSource(settings_cls, toml_file=[p1, p2]),)
s = Settings()
assert s.model_dump() == {'toml1': 1, 'toml2': 2}
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
@pytest.mark.parametrize('deep_merge', [False, True])
def test_multiple_file_toml_merge(tmp_path, deep_merge):
p1 = tmp_path / '.env.toml1'
p2 = tmp_path / '.env.toml2'
p1.write_text(
"""
hello = "world"
[nested]
foo=1
bar=2
"""
)
p2.write_text(
"""
[nested]
foo=3
"""
)
class Nested(BaseModel):
foo: int
bar: int = 0
class Settings(BaseSettings):
hello: str
nested: Nested
@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 (TomlConfigSettingsSource(settings_cls, toml_file=[p1, p2], deep_merge=deep_merge),)
s = Settings()
assert s.model_dump() == {'hello': 'world', 'nested': {'foo': 3, 'bar': 2 if deep_merge else 0}}
|