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__(
193
193
)
194
194
)
195
195
196
+ __init__ .__pydantic_base_init__ = True # type: ignore
197
+
196
198
@classmethod
197
199
def settings_customise_sources (
198
200
cls ,
Original file line number Diff line number Diff line change 30
30
SecretStr ,
31
31
Tag ,
32
32
ValidationError ,
33
+ ValidationInfo ,
34
+ field_validator ,
33
35
)
34
36
from pydantic import (
35
37
dataclasses as pydantic_dataclasses ,
@@ -5187,6 +5189,22 @@ class Settings(BaseSettings):
5187
5189
assert s .model_dump () == {'nested' : {'foo' : ['one' , 'two' ]}}
5188
5190
5189
5191
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
+
5190
5208
def test_nested_model_field_with_alias_choices (env ):
5191
5209
class NestedSettings (BaseModel ):
5192
5210
foo : List [str ] = Field (alias = AliasChoices ('fooalias' , 'foo-alias' ))
You can’t perform that action at this time.
0 commit comments