File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 55import json
66from typing import Tuple , Type , Union
77
8+ import pytest
89from pydantic import BaseModel
910
1011from pydantic_settings import (
@@ -67,6 +68,33 @@ def settings_customise_sources(
6768 assert s .model_dump () == {}
6869
6970
71+ def test_nondict_json_file (tmp_path ):
72+ p = tmp_path / '.env'
73+ p .write_text (
74+ """
75+ "noway"
76+ """
77+ )
78+
79+ class Settings (BaseSettings ):
80+ foobar : str
81+ model_config = SettingsConfigDict (json_file = p )
82+
83+ @classmethod
84+ def settings_customise_sources (
85+ cls ,
86+ settings_cls : Type [BaseSettings ],
87+ init_settings : PydanticBaseSettingsSource ,
88+ env_settings : PydanticBaseSettingsSource ,
89+ dotenv_settings : PydanticBaseSettingsSource ,
90+ file_secret_settings : PydanticBaseSettingsSource ,
91+ ) -> Tuple [PydanticBaseSettingsSource , ...]:
92+ return (JsonConfigSettingsSource (settings_cls ),)
93+
94+ with pytest .raises (ValueError ):
95+ Settings ()
96+
97+
7098def test_multiple_file_json (tmp_path ):
7199 p5 = tmp_path / '.env.json5'
72100 p6 = tmp_path / '.env.json6'
Original file line number Diff line number Diff line change @@ -77,6 +77,30 @@ def settings_customise_sources(
7777 assert s .model_dump () == {}
7878
7979
80+ @pytest .mark .skipif (sys .version_info <= (3 , 11 ) and tomli is None , reason = 'tomli/tomllib is not installed' )
81+ def test_pyproject_nondict_toml (cd_tmp_path ):
82+ pyproject = cd_tmp_path / 'pyproject.toml'
83+ pyproject .write_text (
84+ """
85+ [tool.pydantic-settings]
86+ foobar
87+ """
88+ )
89+
90+ class Settings (BaseSettings ):
91+ foobar : str
92+ model_config = SettingsConfigDict ()
93+
94+ @classmethod
95+ def settings_customise_sources (
96+ cls , settings_cls : Type [BaseSettings ], ** _kwargs : PydanticBaseSettingsSource
97+ ) -> Tuple [PydanticBaseSettingsSource , ...]:
98+ return (TomlConfigSettingsSource (settings_cls , pyproject ),)
99+
100+ with pytest .raises (ValueError ):
101+ Settings ()
102+
103+
80104@pytest .mark .skipif (sys .version_info <= (3 , 11 ) and tomli is None , reason = 'tomli/tomllib is not installed' )
81105def test_multiple_file_toml (tmp_path ):
82106 p1 = tmp_path / '.env.toml1'
Original file line number Diff line number Diff line change @@ -85,6 +85,30 @@ def settings_customise_sources(
8585 assert s .nested .nested_field == 'world!'
8686
8787
88+ @pytest .mark .skipif (yaml is None , reason = 'pyYaml is not installed' )
89+ def test_nondict_yaml_file (tmp_path ):
90+ p = tmp_path / '.env'
91+ p .write_text ('test invalid yaml' )
92+
93+ class Settings (BaseSettings ):
94+ foobar : str
95+ model_config = SettingsConfigDict (yaml_file = p )
96+
97+ @classmethod
98+ def settings_customise_sources (
99+ cls ,
100+ settings_cls : Type [BaseSettings ],
101+ init_settings : PydanticBaseSettingsSource ,
102+ env_settings : PydanticBaseSettingsSource ,
103+ dotenv_settings : PydanticBaseSettingsSource ,
104+ file_secret_settings : PydanticBaseSettingsSource ,
105+ ) -> Tuple [PydanticBaseSettingsSource , ...]:
106+ return (YamlConfigSettingsSource (settings_cls ),)
107+
108+ with pytest .raises (ValueError ):
109+ Settings ()
110+
111+
88112@pytest .mark .skipif (yaml is None , reason = 'pyYaml is not installed' )
89113def test_yaml_no_file ():
90114 class Settings (BaseSettings ):
You can’t perform that action at this time.
0 commit comments