mansenfranzen / autodoc_pydantic

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

Pattern validators not properly escaped #301

Open jnicoulaud-ledger opened 2 months ago

jnicoulaud-ledger commented 2 months ago

When using this kind of types in models:

MyType = Annotated[str, Field(min_length=3, max_length=256, pattern=r"^[a-z0-9_]+$")]

It looks like the generator does not properly escape the regex as needed:

docstring of MyModel.some_field:1:Unknown target name: "a-z0-9".
mansenfranzen commented 2 months ago

Hi @jnicoulaud-ledger,

thanks for raising the issue here!

This looks like an awkward behavior. Could you please provide a minimal reproducible example including the code of the complete pydantic model, the pydantic version you're using and all non-default configurations (conf.py and directive options)? This will help reproduce the bug.

gabicca commented 3 weeks ago

Hi, I have the same problem. Here is an example and versions of code I'm using:

from pydantic import BaseModel, Field
from pydantic.config import ConfigDict

class Example(BaseModel):
    """
    Pointing offset script parameters
    """

    model_config = ConfigDict(title="example")

    version: Annotated[
        str,
        Field(
            pattern=r"^[a-zA-Z0-9_\.-]+$",
            default="0.1.1",
            title="My example failing class",
            description="autodoc_pydantic does not deal with regex correctly",
        ),
    ]

autodoc_pydantic==2.2.0 pydantic==2.9.2 python== 3.11.5

call in .rst .. autopydantic_settings:: mymodule.Example

In conf.py I just added the extension "sphinxcontrib.autodoc_pydantic", did not change anything else in the configuration.