|
2 | 2 | Test pydantic_settings.JsonConfigSettingsSource. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import importlib.resources |
5 | 6 | import json |
| 7 | +import sys |
| 8 | + |
| 9 | +if sys.version_info < (3, 11): |
| 10 | + from importlib.abc import Traversable |
| 11 | +else: |
| 12 | + from importlib.resources.abc import Traversable |
| 13 | + |
6 | 14 | from pathlib import Path |
7 | 15 |
|
8 | 16 | import pytest |
@@ -132,3 +140,77 @@ def settings_customise_sources( |
132 | 140 |
|
133 | 141 | s = Settings() |
134 | 142 | assert s.model_dump() == {'hello': 'world', 'nested': {'foo': 3, 'bar': 2 if deep_merge else 0}} |
| 143 | + |
| 144 | + |
| 145 | +class TestTraversableSupport: |
| 146 | + FILENAME = 'example_test_config.json' |
| 147 | + |
| 148 | + @pytest.fixture(params=['importlib_resources', 'custom']) |
| 149 | + def json_config_path(self, request, tmp_path): |
| 150 | + tests_package_dir = importlib.resources.files('tests') |
| 151 | + |
| 152 | + if request.param == 'importlib_resources': |
| 153 | + # get Traversable object using importlib.resources |
| 154 | + return tests_package_dir / self.FILENAME |
| 155 | + |
| 156 | + # Create a custom Traversable implementation |
| 157 | + class CustomTraversable(Traversable): |
| 158 | + def __init__(self, path): |
| 159 | + self._path = path |
| 160 | + |
| 161 | + def __truediv__(self, child): |
| 162 | + return CustomTraversable(self._path / child) |
| 163 | + |
| 164 | + def is_file(self): |
| 165 | + return self._path.is_file() |
| 166 | + |
| 167 | + def is_dir(self): |
| 168 | + return self._path.is_dir() |
| 169 | + |
| 170 | + def iterdir(self): |
| 171 | + raise NotImplementedError('iterdir not implemented for this test') |
| 172 | + |
| 173 | + def open(self, mode='r', *args, **kwargs): |
| 174 | + return self._path.open(mode, *args, **kwargs) |
| 175 | + |
| 176 | + def read_bytes(self): |
| 177 | + return self._path.read_bytes() |
| 178 | + |
| 179 | + def read_text(self, encoding=None): |
| 180 | + return self._path.read_text(encoding=encoding) |
| 181 | + |
| 182 | + @property |
| 183 | + def name(self): |
| 184 | + return self._path.name |
| 185 | + |
| 186 | + def joinpath(self, *descendants): |
| 187 | + return CustomTraversable(self._path.joinpath(*descendants)) |
| 188 | + |
| 189 | + custom_traversable = CustomTraversable(tests_package_dir) |
| 190 | + return custom_traversable / self.FILENAME |
| 191 | + |
| 192 | + def test_traversable_support(self, json_config_path: Traversable): |
| 193 | + assert json_config_path.is_file() |
| 194 | + |
| 195 | + class Settings(BaseSettings): |
| 196 | + foobar: str |
| 197 | + |
| 198 | + model_config = SettingsConfigDict( |
| 199 | + # Traversable is not added in annotation, but is supported |
| 200 | + json_file=json_config_path, |
| 201 | + ) |
| 202 | + |
| 203 | + @classmethod |
| 204 | + def settings_customise_sources( |
| 205 | + cls, |
| 206 | + settings_cls: type[BaseSettings], |
| 207 | + init_settings: PydanticBaseSettingsSource, |
| 208 | + env_settings: PydanticBaseSettingsSource, |
| 209 | + dotenv_settings: PydanticBaseSettingsSource, |
| 210 | + file_secret_settings: PydanticBaseSettingsSource, |
| 211 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 212 | + return (JsonConfigSettingsSource(settings_cls),) |
| 213 | + |
| 214 | + s = Settings() |
| 215 | + # "test" value in file |
| 216 | + assert s.foobar == 'test' |
0 commit comments