File tree 3 files changed +76
-0
lines changed
3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 5
5
import json
6
6
from typing import Tuple , Type , Union
7
7
8
+ import pytest
8
9
from pydantic import BaseModel
9
10
10
11
from pydantic_settings import (
@@ -67,6 +68,33 @@ def settings_customise_sources(
67
68
assert s .model_dump () == {}
68
69
69
70
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
+
70
98
def test_multiple_file_json (tmp_path ):
71
99
p5 = tmp_path / '.env.json5'
72
100
p6 = tmp_path / '.env.json6'
Original file line number Diff line number Diff line change @@ -77,6 +77,30 @@ def settings_customise_sources(
77
77
assert s .model_dump () == {}
78
78
79
79
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
+
80
104
@pytest .mark .skipif (sys .version_info <= (3 , 11 ) and tomli is None , reason = 'tomli/tomllib is not installed' )
81
105
def test_multiple_file_toml (tmp_path ):
82
106
p1 = tmp_path / '.env.toml1'
Original file line number Diff line number Diff line change @@ -85,6 +85,30 @@ def settings_customise_sources(
85
85
assert s .nested .nested_field == 'world!'
86
86
87
87
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
+
88
112
@pytest .mark .skipif (yaml is None , reason = 'pyYaml is not installed' )
89
113
def test_yaml_no_file ():
90
114
class Settings (BaseSettings ):
You can’t perform that action at this time.
0 commit comments