mansenfranzen / autodoc_pydantic

Seamlessly integrate pydantic models in your Sphinx documentation.
MIT License
159 stars 27 forks source link

Type aliases don't propagate for nested pydantic models with inherited members. #187

Open tasansal opened 12 months ago

tasansal commented 12 months ago

Hello, I think this is a bug (not recursively resolved maybe?).

I will give two code snippets for minimal reproducers and at the very bottom is the rst code and relevant conf.py configuration (I tried with all defaults, still the same as well). My environment:

Standard Python Classes

from typing import TypeAlias

class BaseClassA:
    """Base-class A."""

class BaseClassB:
    """Base-class B."""

compound_type: TypeAlias = BaseClassA | BaseClassB

class SomeOther:
    """Some other class.

    Attributes:
        a: is a
        b: is b
        c: is c
    """

    a: BaseClassA
    b: BaseClassB
    c: compound_type | None

class SubClassSomeOther(SomeOther):
    """Subclass of some other.

    Attributes:
        a: is a
        b: is b
        c: is c
        d: is d
    """

    d: int | None = None

As you can see, the type hints get unfolded (as expected) and propagate down to SubClassSomeOther.

image


Pydantic Models

from pydantic import Field, BaseModel

class PydanticBaseClassA(BaseModel):
    """Base-model A."""

class PydanticBaseClassB(BaseModel):
    """Base-model B."""

pydantic_compound_type: TypeAlias = PydanticBaseClassA | PydanticBaseClassB

class PydanticSomeOther(BaseModel):
    """Some other model."""

    a: PydanticBaseClassA = Field(..., description="is a")
    b: PydanticBaseClassB = Field(..., description="is b")
    c: pydantic_compound_type = Field(..., description="is c")

class PydanticSubClassSomeOther(PydanticSomeOther):
    """Subclass of some other model."""

    d: int | None = Field(default=None, description="is d")

As you will see below, after subclassing PydanticSomeOther as PydanticSubClassSomeOther we lost the unfolding and now our documentation doesn't know what pydantic_compound_type is.

image


Sphinx rst and conf.py

.. autoclass:: mdio.test_class.BaseClassA
.. autoclass:: mdio.test_class.BaseClassB
.. autoclass:: mdio.test_class.compound_type
.. autoclass:: mdio.test_class.SomeOther
.. autoclass:: mdio.test_class.SubClassSomeOther

.. autopydantic_model:: mdio.test_class.PydanticBaseClassA
.. autopydantic_model:: mdio.test_class.PydanticBaseClassB
.. autoclass:: mdio.test_class.pydantic_compound_type
.. autopydantic_model:: mdio.test_class.PydanticSomeOther
.. autopydantic_model:: mdio.test_class.PydanticSubClassSomeOther
   :inherited-members: BaseModel
# conf.py

autodoc_pydantic_field_list_validators = False
autodoc_pydantic_field_swap_name_and_alias = True
autodoc_pydantic_field_show_alias = False
autodoc_pydantic_model_show_config_summary = False
autodoc_pydantic_model_show_validator_summary = False
autodoc_pydantic_model_show_validator_members = False
autodoc_pydantic_model_show_field_summary = False