Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suppport for enums to use autopydantic_model directive #296

Open
jtroe opened this issue Aug 21, 2024 · 4 comments
Open

Add suppport for enums to use autopydantic_model directive #296

jtroe opened this issue Aug 21, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@jtroe
Copy link

jtroe commented Aug 21, 2024

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.

@jtroe
Copy link
Author

jtroe commented Aug 21, 2024

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__

@mansenfranzen mansenfranzen added the enhancement New feature or request label Sep 23, 2024
@mansenfranzen
Copy link
Owner

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?

@consvs
Copy link

consvs commented Nov 6, 2024

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.

@dsgibbons
Copy link

I've just hit this issue too and would love to see this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants