autodoc_pydantic icon indicating copy to clipboard operation
autodoc_pydantic copied to clipboard

`model_post_init` from `BaseModel` appears in the rendered documentation

Open helgee opened this issue 5 months ago • 5 comments

See screenshot. This is not supposed to happen, right?

grafik

helgee avatar Jan 26 '24 19:01 helgee

Here is an MWE.

The problem appears when I provide a custom __init__ for the model (like shown here).

class Test(BaseModel):
    field: str = Field(description="My field")
    _field: str = PrivateAttr()
    
    def __init__(self, **data):
        super().__init__(**data)
        self._field = self.field

helgee avatar Jan 29 '24 09:01 helgee

Workaround: Add a :exclude-members: model_post_init directive.

helgee avatar Jan 29 '24 14:01 helgee

You can also exclude everything on BaseModel with the :inherited-members: BaseModel directive

j-carson avatar Feb 18 '24 02:02 j-carson

@helgee Have you tried @j-carson solution and is it feasible for you?

mansenfranzen avatar Mar 14 '24 12:03 mansenfranzen

@mansenfranzen I suggested that workaround since my last PR had been hanging out for so long without response.

Pydantic 2 has a documented naming convention that the items it adds via the BaseModel class will all start with "model_" -- it seems like the better long-term solution would be an autodoc pydantic option to bulk-drop these attributes rather than a hardcoded list in the autodocumenter's __init__ method since the exact list has changed over time.

j-carson avatar Mar 17 '24 20:03 j-carson