Is there an elegant way to list all properties defined on a Pydantic BaseModel?
It's possible to extract the list of fields via BaseModel.model_fields and the list of computed fields via BaseModel.model_computed_fields, but there does not appear to be a method for listing all the properties.
from pydantic import BaseModel
class Test(BaseModel):
field: str
@property
def len_field(self) -> int:
return len(self.field)
@property
def first_char(self) -> str:
return self.field[0]
t = Test(field="abc")
print(t.model_fields, dir(t))
# how do I list "len_field", "first_char"?
One approach, based on this answer, is to use
inspect.getmembers:From here, it should be possible to filter out the standard pydantic properties.