Search code examples
pythonpython-sphinxpydanticautodocautodoc-pydantic

Autodoc failing with class that has nested pydantic model


As my MRE, I've got the following file:

blah.py

'''Blah module'''

import pydantic

class Foo:
    '''Foo class'''

    class Bar(pydantic.BaseModel):
        '''Bar class'''

        x: str = pydantic.Field(description='The x.')

        @pydantic.validator('x')
        def do_nothing(cls, value: str) -> str:
            return value

I'm attempting to use Sphinx to generate documentation for this module. In my conf.py, I have

extensions = [
    'sphinx.ext.autodoc',
    'sphinxcontrib.autodoc_pydantic',
]

My blah.rst is

Blah
====

.. automodule:: blah.blah
    :members:

I've pip installed pydantic and autodoc_pydantic.

However, when I make html, I get

Exception occurred:
  File "/home/user/Projects/Workspace/env/lib/python3.10/site-packages/sphinxcontrib/autodoc_pydantic/inspection.py", line 311, in __init__
    self.attribute: Dict = self.model.Config
AttributeError: type object 'Foo' has no attribute 'Config'

It appears that autodoc_pydantic thinks that Foo inherits from pydantic.BaseModel when it's really Bar that does. If I remove 'sphinxcontrib.autodoc_pydantic' from extensions, the error goes away.

More interestingly, if I delete the validator, the error goes away as well.

autodoc_pydantic is version 1.6.1.


Solution

  • This issue was fixed in version 1.7.2.