mansenfranzen / autodoc_pydantic

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

Unable to resolve intersphinx reference to a validator re-exported from a private module #297

Open rra opened 2 months ago

rra commented 2 months ago

I have Python code that uses a validator from an external library. A simplified example:

from datetime import datetime

from pydantic import BaseModel, field_validator
from safir.pydantic import normalize_datetime

class AdminHistoryEntry(BaseModel):
    event_time: datetime

    _normalize_event_time = field_validator("event_time", mode="before")(
        normalize_datetime
    )

normalize_datetime is actually defined in safir.pydantic._validators, but is then imported and re-exported by safir.pydantic. The intersphinx objects.inv record is for safir.pydantic.normalize_datetime.

When formatting this documentation, with Sphinx documentation for AdminHistoryEntry generated by autodoc_pydantic, I get the following warning:

/home/eagle/lsst/gafaelfawr/.tox/docs/lib/python3.12/site-packages/gafaelfawr/models/history.py:docstring of gafaelfawr.models.history.AdminHistoryEntry:1: WARNING: py:obj reference target not found: safir.pydantic._validators.normalize_datetime [ref.obj]

I tried adding the sphinx_automodapi.smart_resolver extension, which I think is supposed to handle patterns like this, without any change.

I'm using this pattern in multiple places and all of the function and class references resolve fine except this validator pattern, which makes me think it may be a problem with autodoc_pydantic.

mansenfranzen commented 1 month ago

Hi @rra,

thanks for raising the issue here!

This could be very well a bug because autodoc_pydantic does resolve refs for reusable validators. Solving it could be a bit tricky.

I won't be able to take a look at it short-term. Any help is appreciated :-).