autodoc_pydantic
autodoc_pydantic copied to clipboard
Add suppport for enums to use autopydantic_model directive
Use case: I have a directory with a large amount of pydantic models and enums are mixed together in several directories. It is difficult to discern which is a model and which is an enum from naming alone. I propose we add support for documenting enums (which are typically used extensively in pydantic models) with autopydantic_model so developers do not have to sift through and separate enums to use autoclass
Order
^^^^^
.. autoclass:: arcgis.map.popups.Order
:members:
:undoc-members:
:show-inheritance:
OrderByField
^^^^^^^^^^^^
.. autopydantic_model:: arcgis.map.popups.OrderByField
to
Order
^^^^^
.. autopydantic_model:: arcgis.map.popups.Order
OrderByField
^^^^^^^^^^^^
.. autopydantic_model:: arcgis.map.popups.OrderByField
Where Order is defined as (e.g.):
class Order(Enum):
"""The Order"""
value1 = "value1"
Current behavior when using autopydantic_model on a class that inherits from Enum:
Exception occurred:
File "/opt/conda/envs/arcgis/lib/python3.11/enum.py", line 782, in __getattr__
raise AttributeError(name)
AttributeError: __pydantic_decorators__
Thank you. This library is immensely useful to us.
Related: it would be useful to print the model in question that is failing here:
File "/opt/conda/envs/arcgis/lib/python3.11/site-packages/sphinxcontrib/autodoc_pydantic/inspection.py", line 473, in get_field_validator_mapping
decorators = self.model.__pydantic_decorators__
Hi @jtroe,
thanks for raising this issue here!
If I understand you correctly, you want to leverage autodoc_pydantic to document classes that inherit from Enum but not from pydantic.BaseModel?
Hi @mansenfranzen,
I have the same understanding and would like to elaborate, as I have the same issue.
class UserType(str, Enum):
USER = "user"
SERVICE = "service"
class User(BaseModel):
email: str = Field(..., description="The email address of the user.")
first_name: str = Field(..., description="The first name of the user.")
last_name: str = Field(..., description="The last name of the user.")
user_type: UserType = Field(UserType.USER, description="Whether this is a regular user or a service account.")
UserType is displayed as class, instead of a pydantic model.
class UserType(
value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None
)
The enum values are not displayed at all. I would be more than happy, of the enum values are just displayed with the field User.user_type.
I've just hit this issue too and would love to see this feature.
I think it makes sense to display the enum values under "Constraints":
E.g. for the user_type field above I would have expected something like this:
Constraints: - one of "user", "service"
or
Constraints: - "user"
- "service"