|
| 1 | +from __future__ import annotations as _annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pydantic import AnyHttpUrl, Field |
| 10 | + |
| 11 | +try: |
| 12 | + import yaml # type: ignore |
| 13 | +except Exception: |
| 14 | + yaml = None |
| 15 | + |
| 16 | +try: |
| 17 | + import tomli # type: ignore |
| 18 | +except Exception: |
| 19 | + tomli = None |
| 20 | + |
| 21 | +from pydantic_settings import ( |
| 22 | + BaseSettings, |
| 23 | + JsonConfigSettingsSource, |
| 24 | + PydanticBaseSettingsSource, |
| 25 | + SettingsConfigDict, |
| 26 | + TomlConfigSettingsSource, |
| 27 | + YamlConfigSettingsSource, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def test_init_kwargs_override_env_for_alias_with_populate_by_name(monkeypatch): |
| 32 | + class Settings(BaseSettings): |
| 33 | + abc: AnyHttpUrl = Field(validation_alias='my_abc') |
| 34 | + model_config = SettingsConfigDict(populate_by_name=True, extra='allow') |
| 35 | + |
| 36 | + monkeypatch.setenv('MY_ABC', 'http://localhost.com/') |
| 37 | + |
| 38 | + # Passing by field name should be accepted (populate_by_name=True) and should |
| 39 | + # override env-derived value. Also ensures init > env precedence with validation_alias. |
| 40 | + assert str(Settings(abc='http://prod.localhost.com/').abc) == 'http://prod.localhost.com/' |
| 41 | + |
| 42 | + |
| 43 | +def test_deep_merge_multiple_file_json(tmp_path: Path): |
| 44 | + p1 = tmp_path / 'a.json' |
| 45 | + p2 = tmp_path / 'b.json' |
| 46 | + |
| 47 | + with open(p1, 'w') as f1: |
| 48 | + json.dump({'a': 1, 'nested': {'x': 1, 'y': 1}}, f1) |
| 49 | + with open(p2, 'w') as f2: |
| 50 | + json.dump({'b': 2, 'nested': {'y': 2, 'z': 3}}, f2) |
| 51 | + |
| 52 | + class Settings(BaseSettings): |
| 53 | + a: Optional[int] = None |
| 54 | + b: Optional[int] = None |
| 55 | + nested: dict[str, int] |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def settings_customise_sources( |
| 59 | + cls, |
| 60 | + settings_cls: type[BaseSettings], |
| 61 | + init_settings: PydanticBaseSettingsSource, |
| 62 | + env_settings: PydanticBaseSettingsSource, |
| 63 | + dotenv_settings: PydanticBaseSettingsSource, |
| 64 | + file_secret_settings: PydanticBaseSettingsSource, |
| 65 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 66 | + return (JsonConfigSettingsSource(settings_cls, json_file=[p1, p2]),) |
| 67 | + |
| 68 | + s = Settings() |
| 69 | + assert s.a == 1 |
| 70 | + assert s.b == 2 |
| 71 | + assert s.nested == {'x': 1, 'y': 2, 'z': 3} |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.skipif(yaml is None, reason='pyYAML is not installed') |
| 75 | +def test_deep_merge_multiple_file_yaml(tmp_path: Path): |
| 76 | + p1 = tmp_path / 'a.yaml' |
| 77 | + p2 = tmp_path / 'b.yaml' |
| 78 | + |
| 79 | + p1.write_text( |
| 80 | + """ |
| 81 | + a: 1 |
| 82 | + nested: |
| 83 | + x: 1 |
| 84 | + y: 1 |
| 85 | + """ |
| 86 | + ) |
| 87 | + p2.write_text( |
| 88 | + """ |
| 89 | + b: 2 |
| 90 | + nested: |
| 91 | + y: 2 |
| 92 | + z: 3 |
| 93 | + """ |
| 94 | + ) |
| 95 | + |
| 96 | + class Settings(BaseSettings): |
| 97 | + a: Optional[int] = None |
| 98 | + b: Optional[int] = None |
| 99 | + nested: dict[str, int] |
| 100 | + |
| 101 | + @classmethod |
| 102 | + def settings_customise_sources( |
| 103 | + cls, |
| 104 | + settings_cls: type[BaseSettings], |
| 105 | + init_settings: PydanticBaseSettingsSource, |
| 106 | + env_settings: PydanticBaseSettingsSource, |
| 107 | + dotenv_settings: PydanticBaseSettingsSource, |
| 108 | + file_secret_settings: PydanticBaseSettingsSource, |
| 109 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 110 | + return (YamlConfigSettingsSource(settings_cls, yaml_file=[p1, p2]),) |
| 111 | + |
| 112 | + s = Settings() |
| 113 | + assert s.a == 1 |
| 114 | + assert s.b == 2 |
| 115 | + assert s.nested == {'x': 1, 'y': 2, 'z': 3} |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed') |
| 119 | +def test_deep_merge_multiple_file_toml(tmp_path: Path): |
| 120 | + p1 = tmp_path / 'a.toml' |
| 121 | + p2 = tmp_path / 'b.toml' |
| 122 | + |
| 123 | + p1.write_text( |
| 124 | + """ |
| 125 | + a=1 |
| 126 | +
|
| 127 | + [nested] |
| 128 | + x=1 |
| 129 | + y=1 |
| 130 | + """ |
| 131 | + ) |
| 132 | + p2.write_text( |
| 133 | + """ |
| 134 | + b=2 |
| 135 | +
|
| 136 | + [nested] |
| 137 | + y=2 |
| 138 | + z=3 |
| 139 | + """ |
| 140 | + ) |
| 141 | + |
| 142 | + class Settings(BaseSettings): |
| 143 | + a: Optional[int] = None |
| 144 | + b: Optional[int] = None |
| 145 | + nested: dict[str, int] |
| 146 | + |
| 147 | + @classmethod |
| 148 | + def settings_customise_sources( |
| 149 | + cls, |
| 150 | + settings_cls: type[BaseSettings], |
| 151 | + init_settings: PydanticBaseSettingsSource, |
| 152 | + env_settings: PydanticBaseSettingsSource, |
| 153 | + dotenv_settings: PydanticBaseSettingsSource, |
| 154 | + file_secret_settings: PydanticBaseSettingsSource, |
| 155 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 156 | + return (TomlConfigSettingsSource(settings_cls, toml_file=[p1, p2]),) |
| 157 | + |
| 158 | + s = Settings() |
| 159 | + assert s.a == 1 |
| 160 | + assert s.b == 2 |
| 161 | + assert s.nested == {'x': 1, 'y': 2, 'z': 3} |
| 162 | + |
| 163 | + |
| 164 | +@pytest.mark.skipif(yaml is None, reason='pyYAML is not installed') |
| 165 | +def test_yaml_config_section_after_deep_merge(tmp_path: Path): |
| 166 | + # Ensure that config section is picked from the deep-merged data |
| 167 | + p1 = tmp_path / 'a.yaml' |
| 168 | + p2 = tmp_path / 'b.yaml' |
| 169 | + p1.write_text( |
| 170 | + """ |
| 171 | + nested: |
| 172 | + x: 1 |
| 173 | + y: 1 |
| 174 | + """ |
| 175 | + ) |
| 176 | + p2.write_text( |
| 177 | + """ |
| 178 | + nested: |
| 179 | + y: 2 |
| 180 | + z: 3 |
| 181 | + other: true |
| 182 | + """ |
| 183 | + ) |
| 184 | + |
| 185 | + class S2(BaseSettings): |
| 186 | + x: int |
| 187 | + y: int |
| 188 | + z: int |
| 189 | + model_config = SettingsConfigDict(yaml_config_section='nested') |
| 190 | + |
| 191 | + @classmethod |
| 192 | + def settings_customise_sources( |
| 193 | + cls, |
| 194 | + settings_cls: type[BaseSettings], |
| 195 | + init_settings: PydanticBaseSettingsSource, |
| 196 | + env_settings: PydanticBaseSettingsSource, |
| 197 | + dotenv_settings: PydanticBaseSettingsSource, |
| 198 | + file_secret_settings: PydanticBaseSettingsSource, |
| 199 | + ) -> tuple[PydanticBaseSettingsSource, ...]: |
| 200 | + return (YamlConfigSettingsSource(settings_cls, yaml_file=[p1, p2]),) |
| 201 | + |
| 202 | + s2 = S2() |
| 203 | + assert s2.model_dump() == {'x': 1, 'y': 2, 'z': 3} |
0 commit comments