File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -193,6 +193,8 @@ def __init__(
193193 )
194194 )
195195
196+ __init__ .__pydantic_base_init__ = True # type: ignore
197+
196198 @classmethod
197199 def settings_customise_sources (
198200 cls ,
Original file line number Diff line number Diff line change 3030 SecretStr ,
3131 Tag ,
3232 ValidationError ,
33+ ValidationInfo ,
34+ field_validator ,
3335)
3436from pydantic import (
3537 dataclasses as pydantic_dataclasses ,
@@ -5187,6 +5189,22 @@ class Settings(BaseSettings):
51875189 assert s .model_dump () == {'nested' : {'foo' : ['one' , 'two' ]}}
51885190
51895191
5192+ def test_validation_context ():
5193+ class Settings (BaseSettings ):
5194+ foo : str
5195+
5196+ @field_validator ('foo' )
5197+ @classmethod
5198+ def test_validator (cls , v : str , info : ValidationInfo ):
5199+ context = info .context
5200+ assert context == {'foo' : 'bar' }
5201+ return v
5202+
5203+ s = Settings .model_validate ({'foo' : 'foo bar' }, context = {'foo' : 'bar' })
5204+ assert s .foo == 'foo bar'
5205+ assert s .model_dump () == {'foo' : 'foo bar' }
5206+
5207+
51905208def test_nested_model_field_with_alias_choices (env ):
51915209 class NestedSettings (BaseModel ):
51925210 foo : List [str ] = Field (alias = AliasChoices ('fooalias' , 'foo-alias' ))
You can’t perform that action at this time.
0 commit comments