You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since type_ method has been deprecated how can I check if the annotation is Pydantic BaseModel underneath?
In my specific case Building class inherits BaseModel not directly:
class Building(Name):
address: str
officers: list[str]
class Name(BaseModel):
"""
BaseModel subclass
"""
What I have tried:
if isinstance(value.annotation, BaseModel):
# do something
# returns False when trying hq, although should return True, returns False with Name also
if issubclass(value.annotation, BaseModel):
# do something
# returns TypeError: issubclass() arg 1 must be a class, same with Name class
Is checking the type_ like in the Pydantic v1 was forgotten to implement in Pydantic v2?
Example Code
frompydanticimportBaseModelfromtypingimportOptional, get_origin, ListclassName(BaseModel):
""" BaseModel subclass """classBuilding(Name):
address: strofficers: list[str]
classPerson(Name):
firstname: strlastname: Optional[str]
classCompany(Name):
id: strhq: Optional[Building]
ceo: Personemployees: List[Person]
model=Company(
id="123",
hq=Building(address="test1", officers=["test2"]),
ceo=Person(firstname="test3", lastname="test4"),
employees=[
Person(firstname="test3", lastname="test4"),
Person(firstname="test5", lastname="test6"),
],
)
#model_fields print:#{'id': FieldInfo(annotation=str, required=True), 'hq': FieldInfo(annotation=Union[Building, NoneType], required=True), 'ceo': FieldInfo(annotation=Person, required=True), 'employees': FieldInfo(annotation=List[Person], required=True)}forkey, valueinmodel.model_fields.items():
ifisinstance(value.annotation, BaseModel):
print(f"Key - {key} is instance of BaseModel")
# should print on hq, ceo keysifisinstance(value.annotation, Name):
print(f"Key - {key} is instance of Name")
# should print on hq, ceo keysifget_origin(value.annotation) ==list:
print(f"Key - {key} is list")
# should print on employees key# if issubclass(value.annotation, BaseModel):# TypeError: issubclass() arg 1 must be a class# if issubclass(value.annotation, Name):# TypeError: issubclass() arg 1 must be a class#How we were using this in Pydantic v1:frompydantic.mainimportModelMetaclassforkey, valueinmodel.__dict__["__fields__"].items():
ifisinstance(value.type_, ModelMetaclass):
# do something with hq, ceoelifget_origin(value.outer_type_) ==list:
# do something with employees
Is checking the type_ like in the Pydantic v1 was forgotten to implement in Pydantic v2?
It wasn't "forgotten", it's just not there because things are implemented differently. That was never meant to be a "feature" in v1 (and in fact there was a lot of weirdness around ModelField.type_ unpacking Optional[...] and such). You'll have to rework your code to use FieldInfo.annotation directly:
Initial Checks
Description
Hello, I am migrating to Pydantic v2 and while migrating I can see that getting
model_fields
has changed:What also changed is what these two return:
Since
type_
method has been deprecated how can I check if the annotation is Pydantic BaseModel underneath?In my specific case
Building
class inherits BaseModel not directly:What I have tried:
Is checking the type_ like in the Pydantic v1 was forgotten to implement in Pydantic v2?
Example Code
Python, Pydantic & OS Version
Selected Assignee: @lig
The text was updated successfully, but these errors were encountered: