autodoc_pydantic icon indicating copy to clipboard operation
autodoc_pydantic copied to clipboard

Support pydantic dataclass models

Open GlenNicholls opened this issue 2 years ago • 5 comments

I am using pydantic.dataclasses.dataclass because I need the __post_init__ dunder. However, I noticed that automodule does not generate the pydantic model documentation for these data classes. Looking at pydantic's docs, I can access the model using __pydantic_model__, but I'm not sure how to get this to happen automatically with automodule?

GlenNicholls avatar May 09 '22 20:05 GlenNicholls

@GlenNicholls Thanks for raising this feature request here! I agree it would be really helpful to support pydantic.dataclasses.dataclass. At first glance, this should be possible without too much effort but I will need to take a closer look at it. I will come back to you soonish.

mansenfranzen avatar May 15 '22 18:05 mansenfranzen

While having a closer look at it, I came across two usage questions:

  • Do you need a custom signature prefix for dataclasses to distinguish them from pydantic models/settings?
  • Do you need separate configuration properties for dataclasses to behave differently from pydantic models or do the same configuration settings apply as for pydantic models?

In my opinion, dataclasses should be distinguishable from models/settings via a custom prefix. However, their documentation/rendering behavior should be the same

mansenfranzen avatar Jun 07 '22 14:06 mansenfranzen

Do you need a custom signature prefix for dataclasses to distinguish them from pydantic models/settings?

No, the reason I'm using pydantic dataclass is because I need validation for a config file but also to modify some behavior based on data relationships with __post_init_post_parse__/__post_init__. I don't really care if those docstrings are present as it's taking care of transformations that don't really concern users. The docstring is in the code for devs because it matters to them, but It's not relevant otherwise.

Do you need separate configuration properties for dataclasses to behave differently from pydantic models or do the same configuration settings apply as for pydantic models?

Nope. I'm sure someone will ask for that eventually, but I just want the dataclass model to get documented the same as a pydantic model. I'm happy with the defaults so those would be fine with me for dataclasses.

EDIT: regarding your second question, if there is something specific you're thinking of, it won't matter to me is what I'm getting at. I don't have a preference as autodoc_pydantic is miles ahead of anything I've tried so far, but if that changes, I'll open something more specific about what I need.

GlenNicholls avatar Jun 09 '22 22:06 GlenNicholls

Okay thanks for your response. I guess I will support explicit configurations for dataclasses right away instead of using the same configuration settings as for models. As you've pointed out, this will become a requirement eventually by someone using dataclasses. Supporting it right from the beginning may prevent backwards incompatibility later on.

This will take me some time to accomplish and it will land in v1.8.0.

mansenfranzen avatar Jun 12 '22 18:06 mansenfranzen

Is there a reason, why this is removed? Would be a nice feature.

nilsAnso avatar Jan 14 '24 11:01 nilsAnso