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__(
1692
1692
def _read_file (self , file_path : Path ) -> dict [str , Any ]:
1693
1693
import_yaml ()
1694
1694
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 {}
1696
1696
1697
1697
1698
1698
def _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(
3485
3485
assert s .model_dump () == {}
3486
3486
3487
3487
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
+
3488
3511
@pytest .mark .skipif (sys .version_info <= (3 , 11 ) and tomli is None , reason = 'tomli/tomllib is not installed' )
3489
3512
def test_toml_file (tmp_path ):
3490
3513
p = tmp_path / '.env'
You can’t perform that action at this time.
0 commit comments