Replies: 3 comments
-
Hi @zhengye1, Thanks for your question. Happy to help you figure this out 😄. What are you trying to accomplish with this code snippet? I think there might be an easier way to get what you want without the attempted type checking against values of |
Beta Was this translation helpful? Give feedback.
-
Perhaps typing_inspect or typeguard could help out with this? |
Beta Was this translation helpful? Give feedback.
-
I am dealing with this too: https://stackoverflow.com/questions/78486064/how-do-i-get-the-model-type-inside-a-pydanticbasesettingssource I need to get the type of the FieldInfo object because I'm using that for triggering my custom 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 How can I do this? I just want to get test if the field is of MyModel type. |
Beta Was this translation helpful? Give feedback.
-
Initial Checks
Description
In v1 version, all the
model.model_fields
was usingmodel.__fields__
and I can access thetype_
, and when I run the code, for thefield_1
, the result fortype_
it isstr
, so it will not failing onissubclass(field.type_, BaseModel)
However when I update to v2, and after I check #7220 and change the code to the below, but after I run it, the result is
typing.Optional[str]
and it looks like this is not a class, and failed on issubclass method..What should I change to make the behaviour is same as v1?
Example Code
Python, Pydantic & OS Version
Beta Was this translation helpful? Give feedback.
All reactions