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

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

Open
tasansal opened this issue Dec 1, 2023 · 0 comments

Comments

@tasansal
Copy link

tasansal commented Dec 1, 2023

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:

  • python 3.11
  • sphinx 7.2.6
  • myst-parser 2.0.0 (irrelevant to this but adding in case)
  • autodoc-pydantic 2.0.1

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant