File tree Expand file tree Collapse file tree 3 files changed +33
-10
lines changed
Expand file tree Collapse file tree 3 files changed +33
-10
lines changed Original file line number Diff line number Diff line change 3636
3737
3838def Field (
39+ * args ,
3940 initial = None ,
4041 json_schema_extra = None ,
4142 ** kwargs ,
4243):
43- """ """
44- if initial and not callable (initial ):
45- val = initial
46- initial = lambda : val
47-
48- json_schema_extra = json_schema_extra or {}
49- if initial and isinstance (json_schema_extra , dict ):
44+ if initial :
45+ json_schema_extra = json_schema_extra or {}
5046 json_schema_extra ["initial" ] = initial
51- return PydanticField (json_schema_extra = json_schema_extra , ** kwargs )
47+
48+ return PydanticField (* args , json_schema_extra = json_schema_extra , ** kwargs )
5249
5350
5451class GoodConfConfigDict (SettingsConfigDict ):
Original file line number Diff line number Diff line change @@ -186,6 +186,29 @@ class TestConf(GoodConf):
186186 TestConf (load = True )
187187
188188
189+ def test_default_values_are_used (monkeypatch ):
190+ """
191+ Covers regression in: https://github.com/lincolnloop/goodconf/pull/51
192+
193+ Requires more than one defined field to reproduce.
194+ """
195+ monkeypatch .delenv ("a" , raising = False )
196+ monkeypatch .setenv ("b" , "value_from_env" )
197+ monkeypatch .delenv ("c" , raising = False )
198+
199+ class TestConf (GoodConf ):
200+ a : str = Field (default = "default_for_a" )
201+ b : str = Field (initial = lambda : "1234" )
202+ c : str = Field ("default_for_c" )
203+
204+ c = TestConf ()
205+ c .load ()
206+
207+ assert c .a == "default_for_a"
208+ assert c .b == "value_from_env"
209+ assert c .c == "default_for_c"
210+
211+
189212def test_set_on_init ():
190213 class TestConf (GoodConf ):
191214 a : str = Field ()
Original file line number Diff line number Diff line change 11from typing import Optional
22
3+ import pytest
4+
35from goodconf import Field , GoodConf , initial_for_field
46
57from .utils import KEY
@@ -12,11 +14,12 @@ class C(GoodConf):
1214 assert initial_for_field (KEY , C .model_fields ["f" ]) == "x"
1315
1416
15- def test_initial_converts_to_callable ():
17+ def test_initial_bad ():
1618 class C (GoodConf ):
1719 f : str = Field (initial = "x" )
1820
19- assert initial_for_field (KEY , C .model_fields ["f" ]) == "x"
21+ with pytest .raises (ValueError ):
22+ initial_for_field (KEY , C .model_fields ["f" ])
2023
2124
2225def test_initial_default ():
You can’t perform that action at this time.
0 commit comments