Closed aidanmontare-fed closed 1 month ago
Thanks @aidanmontare-fed for reporting this bug. I will prepare a fix.
@aidanmontare-fed I created https://github.com/pydantic/pydantic-settings/pull/417 to fix the problem.
Could you please confirm?
@hramezani just checked it out and it works great! Thanks so much!
Thanks @aidanmontare-fed for reporting the bug and confirming the fix.
The fix will be available in the next release!
@aidanmontare-fed I have to revert the fix. Actually, the issue is related to pydantic https://github.com/pydantic/pydantic/issues/10062
The fix that I did caused a lot of issues for current users and had to be reverted. Here are some of the issues:
https://github.com/pydantic/pydantic-settings/issues/456
You can solve your problem by adding __init__.__pydantic_base_init__ = True
to your model before the real fix in pydantic. but you have to take care about the other issues that might happen:
class Settings(BaseSettings):
text: str
def __init__(self, **data):
super().__init__(**data)
__init__.__pydantic_base_init__ = True
@field_validator('text')
@classmethod
def test_validator(cls, v: str, info: ValidationInfo):
context = info.context
print(f'{context=}')
if context:
print('have context')
return v
Settings.model_validate({'text': 'foo bar'}, context={'biz': 'baz'})
moving here from #pydantic/pydantic#10418
Initial Checks
Description
Hi there! It appears that the
context
arg that can be passed tomodel_validate
does not work when using Pydantic Settings, when it does work using a regular Pydantic model.Example Code
Python, Pydantic & OS Version