Is there a way to patch a method used in a BeforeValidator of an Annotated type (used in Pydantic BaseSettings) such that the following code will work?
Why doesn't it work? Is it because Annotated type is constructed first and then the patching is executed, but the Annotated type is already in memory?
How could I refactor so it works? In the real code there is a Config class with some parsers and the asd_func tests if paths exist on disk, but in the test I would like to skip this particular validation.
from typing import Annotated
from unittest.mock import patch
from pydantic import BeforeValidator
from pydantic_settings import BaseSettings
def asd_func(value):
return value
AsdType = Annotated[str, BeforeValidator(asd_func)]
class Config(BaseSettings):
ASD: AsdType = "asd"
@patch(__name__ + ".asd_func", return_value="zzzzzz")
def test_config_load(_):
config = Config()
assert config.ASD == "zzzzzz" # explodes here
test_config_load()