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
If hinting to Category with Category , this error occurs: Exception has occurred: NameError name 'Category' is not defined
If hinting to Category with "Category" (like you usually do in Python), this error occurs: Exception has occurred: TypeError issubclass() arg 1 must be a class
The text was updated successfully, but these errors were encountered:
Self-Referencing annotations can be handled quite nice with Pedantic (see doc). Whenever you want to reference the self-class in an annotation, simply pass it as a string and it works. Here is an overexagurated example:
importpydanticimportenumimporttypingimportjsonclassCategoryType(pydantic.BaseModel):
"""Recursion is possible when the type is given as a string"""foo: strbar: strcategory: typing.Optional['CategoryType'] =None# Create recursion objectscategory=CategoryType(foo="foo", bar="bar")
top_category=CategoryType(foo="top_foo", bar="top_bar")
toptop_category=CategoryType(foo="toptop_foo", bar="toptop_bar")
# Get them togethercategory.category=top_categorycategory.category.category=toptop_category
If hinting to Category with
Category
, this error occurs:Exception has occurred: NameError
name 'Category' is not defined
If hinting to Category with
"Category"
(like you usually do in Python), this error occurs:Exception has occurred: TypeError
issubclass() arg 1 must be a class
The text was updated successfully, but these errors were encountered: