@@ -5004,3 +5004,45 @@ class Settings(BaseSettings):
5004
5004
foo : list [str ] = []
5005
5005
5006
5006
Settings ()
5007
+
5008
+
5009
+ def test_nested_model_field_with_alias (env ):
5010
+ class NestedSettings (BaseModel ):
5011
+ foo : List [str ] = Field (alias = 'fooalias' )
5012
+
5013
+ class Settings (BaseSettings ):
5014
+ model_config = SettingsConfigDict (env_nested_delimiter = '__' )
5015
+
5016
+ nested : NestedSettings
5017
+
5018
+ env .set ('nested__fooalias' , '["one", "two"]' )
5019
+
5020
+ s = Settings ()
5021
+ assert s .model_dump () == {'nested' : {'foo' : ['one' , 'two' ]}}
5022
+
5023
+
5024
+ def test_nested_model_field_with_alias_case_sensitive (monkeypatch ):
5025
+ class NestedSettings (BaseModel ):
5026
+ foo : List [str ] = Field (alias = 'fooAlias' )
5027
+
5028
+ class Settings (BaseSettings ):
5029
+ model_config = SettingsConfigDict (env_nested_delimiter = '__' , case_sensitive = True )
5030
+
5031
+ nested : NestedSettings
5032
+
5033
+ # Need to patch os.environ to get build to work on Windows, where os.environ is case insensitive
5034
+ monkeypatch .setattr (os , 'environ' , value = {'nested__fooalias' : '["one", "two"]' })
5035
+ with pytest .raises (ValidationError ) as exc_info :
5036
+ Settings ()
5037
+ assert exc_info .value .errors (include_url = False ) == [
5038
+ {
5039
+ 'type' : 'missing' ,
5040
+ 'loc' : ('nested' , 'fooAlias' ),
5041
+ 'msg' : 'Field required' ,
5042
+ 'input' : {'fooalias' : '["one", "two"]' },
5043
+ }
5044
+ ]
5045
+
5046
+ monkeypatch .setattr (os , 'environ' , value = {'nested__fooAlias' : '["one", "two"]' })
5047
+ s = Settings ()
5048
+ assert s .model_dump () == {'nested' : {'foo' : ['one' , 'two' ]}}
0 commit comments