I’m writing a custom PydanticBaseSettingsSource and processing fields of a specific type MyModel
class CustomSettingsSource(PydanticBaseSettingsSource):
# Must implement
def get_field_value(
self, field: FieldInfo, field_name: str
) -> Tuple[Any, str, bool]:
return ({}, "", False)
def __call__(self) -> Dict[str, Any]:
d: Dict[str, Any] = {}
for field_name, field in self.settings_cls.model_fields.items():
# If a field is of type MyModel I do custom processing/fetching
# This returns <class 'mymodule.MyModel'>
print(field.annotation)
# This does not work
if isinstance(field.annotation, MyModel):
# do stuff...
return d
I create a config assigning a field the MyModel type:
class Test(MyConfig):
test: MyModel = MyModel(blah="sdfsdf")
What is the correct way to do this? I really don’t want to have do something like str(field.annotation).contains(...)
because that feels like a hack.