File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed
Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -1692,7 +1692,7 @@ def __init__(
16921692 def _read_file (self , file_path : Path ) -> dict [str , Any ]:
16931693 import_yaml ()
16941694 with open (file_path , encoding = self .yaml_file_encoding ) as yaml_file :
1695- return yaml .safe_load (yaml_file )
1695+ return yaml .safe_load (yaml_file ) or {}
16961696
16971697
16981698def _get_env_var_key (key : str , case_sensitive : bool = False ) -> str :
Original file line number Diff line number Diff line change @@ -3485,6 +3485,29 @@ def settings_customise_sources(
34853485 assert s .model_dump () == {}
34863486
34873487
3488+ @pytest .mark .skipif (yaml is None , reason = 'pyYaml is not installed' )
3489+ def test_yaml_empty_file (tmp_path ):
3490+ p = tmp_path / '.env'
3491+ p .write_text ('' )
3492+
3493+ class Settings (BaseSettings ):
3494+ model_config = SettingsConfigDict (yaml_file = p )
3495+
3496+ @classmethod
3497+ def settings_customise_sources (
3498+ cls ,
3499+ settings_cls : Type [BaseSettings ],
3500+ init_settings : PydanticBaseSettingsSource ,
3501+ env_settings : PydanticBaseSettingsSource ,
3502+ dotenv_settings : PydanticBaseSettingsSource ,
3503+ file_secret_settings : PydanticBaseSettingsSource ,
3504+ ) -> Tuple [PydanticBaseSettingsSource , ...]:
3505+ return (YamlConfigSettingsSource (settings_cls ),)
3506+
3507+ s = Settings ()
3508+ assert s .model_dump () == {}
3509+
3510+
34883511@pytest .mark .skipif (sys .version_info <= (3 , 11 ) and tomli is None , reason = 'tomli/tomllib is not installed' )
34893512def test_toml_file (tmp_path ):
34903513 p = tmp_path / '.env'
You can’t perform that action at this time.
0 commit comments