Skip to content

Commit

Permalink
Merge pull request #160 from Extrality/aw/pydantic-v2
Browse files Browse the repository at this point in the history
pydantic v2
  • Loading branch information
mansenfranzen authored Jul 22, 2023
2 parents 4af0249 + d6ef69f commit c0b011a
Show file tree
Hide file tree
Showing 52 changed files with 597 additions and 1,145 deletions.
64 changes: 16 additions & 48 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,28 @@ jobs:
fail-fast: false
matrix:
include:
- python_version: 3.7
pydantic_version: "16"
sphinx_version: "34"
- python_version: 3.8
pydantic_version: "17"
sphinx_version: "35"
- python_version: 3.9
pydantic_version: "18"
sphinx_version: "35"
- python_version: 3.9
pydantic_version: "18"
- python_version: "3.7"
pydantic_version: "20"
sphinx_version: "40"
- python_version: 3.9
pydantic_version: "18"
sphinx_version: "41"
- python_version: 3.9
pydantic_version: "18"
sphinx_version: "42"
- python_version: 3.9
pydantic_version: "18"
sphinx_version: "43"
- python_version: "3.10"
pydantic_version: "18"
sphinx_version: "43"
- python_version: "3.10"
pydantic_version: "19"
sphinx_version: "43"
- python_version: "3.10"
pydantic_version: "19"
sphinx_version: "44"
- python_version: "3.10"
pydantic_version: "19"
- python_version: "3.8"
pydantic_version: "20"
sphinx_version: "45"
- python_version: "3.9"
pydantic_version: "20"
sphinx_version: "45"
- python_version: "3.9"
pydantic_version: "20"
sphinx_version: "53"
- python_version: "3.10"
pydantic_version: "19"
sphinx_version: "50"
- python_version: "3.10"
pydantic_version: "110"
sphinx_version: "51"
- python_version: "3.10"
pydantic_version: "110"
sphinx_version: "52"
- python_version: "3.10"
pydantic_version: "110"
sphinx_version: "60"
- python_version: "3.10"
pydantic_version: "110"
sphinx_version: "61"
- python_version: "3.10"
pydantic_version: "110"
pydantic_version: "20"
sphinx_version: "62"
- python_version: "3.10"
pydantic_version: "110"
pydantic_version: "20"
sphinx_version: "70"
- python_version: "3.11"
pydantic_version: "20"
sphinx_version: "70"

runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ Additionally, it adds support for sphinx 4.3.
Bugfix
~~~~~~

- Fix a corner-case where a module that imported
- Fix a corner-case where a module that imported
``numpy.typing.NDArray`` caused autodoc_pydantic to experience
an uncaught exception
`#57 <https://github.com/mansenfranzen/autodoc_pydantic/issues/57>`__.
Expand Down Expand Up @@ -975,7 +975,7 @@ Added
- ``config_members``

- Directives ``PydanticModel``, ``PydanticSettings``,
``PydanticField``, ``PydanticValidator``, ``PydanticConfigClass``
``PydanticField``, ``PydanticValidator``

Internal
~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
author = 'Franz Wöllert'

# The full version, including alpha/beta/rc tags
release = '1.9.0'
release = '2.0.0'

# -- General configuration ---------------------------------------------------

Expand Down
6 changes: 2 additions & 4 deletions docs/source/developers/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ programmatically to inspect pydantic models:
.. code-block:: python
from sphinxcontrib.autodoc_pydantic.inspection import ModelInspector
from pydantic import BaseModel, validator, Field
from pydantic import BaseModel, field_validator, Field
class TestModel(BaseModel):
field_a: int = Field(1, min=0, max=10)
field_b: str = "FooBar"
@validator("field_a")
@field_validator("field_a")
def validate_field_a(cls, v):
return v
Expand Down Expand Up @@ -139,7 +139,6 @@ The following auto-documenters exist in the :ref:`autodocumenters <api_autodocum
- :class:`PydanticSettingsDocumenter <sphinxcontrib.autodoc_pydantic.directives.autodocumenters.PydanticSettingsDocumenter>`
- :class:`PydanticFieldDocumenter <sphinxcontrib.autodoc_pydantic.directives.autodocumenters.PydanticFieldDocumenter>`
- :class:`PydanticValidatorDocumenter <sphinxcontrib.autodoc_pydantic.directives.autodocumenters.PydanticValidatorDocumenter>`
- :class:`PydanticConfigClassDocumenter <sphinxcontrib.autodoc_pydantic.directives.autodocumenters.PydanticConfigClassDocumenter>`

All auto-documenters are not written from scratch but inherit from
the default auto-documenters to borrow most of the main functionality provided
Expand Down Expand Up @@ -300,4 +299,3 @@ customization:
- :class:`PydanticSettings <sphinxcontrib.autodoc_pydantic.directives.directives.PydanticSettings>`
- :class:`PydanticField <sphinxcontrib.autodoc_pydantic.directives.directives.PydanticField>`
- :class:`PydanticValidator <sphinxcontrib.autodoc_pydantic.directives.directives.PydanticValidator>`
- :class:`PydanticConfigClass <sphinxcontrib.autodoc_pydantic.directives.directives.PydanticConfigClass>`
4 changes: 2 additions & 2 deletions docs/source/developers/guides.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ This requires at least two pydantic fields and validators to be sortable.
field_b: int = 1
field_a: int = 1
@validator("field_b")
@field_validator("field_b")
def validate_b(cls, v):
return v
@validator("field_a")
@field_validator("field_a")
def validate_a(cls, v):
return v
Expand Down
30 changes: 0 additions & 30 deletions docs/source/users/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -638,36 +638,6 @@ Validators

Define the signature prefix for pydantic validator.

------------
Config Class
------------

.. config_description:: autopydantic_model
:title: Show Members
:path: target.configuration.ConfigMembers
:confpy: autodoc_pydantic_config_members
:directive_option: members
:enable: model-show-config-member, undoc-members
:values: True, False

Show members. By default, members are hidden for standard :code:`auto`
directives. For pydantic class config, this is overwritten if enabled.

.. note::

By default, all undocumented members are shown for the `Config` class.
The directive option :code:`:undoc-members:` is added automatically.


.. config_description:: autopydantic_config
:title: Signature Prefix
:path: target.configuration.ConfigSignaturePrefix.Config
:confpy: autodoc_pydantic_config_signature_prefix
:directive_option: config-signature-prefix
:values: model, class, foobar

Define the signature prefix for config class.


-------
General
Expand Down
4 changes: 2 additions & 2 deletions docs/source/users/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Asterisk and root validators
----------------------------

This example highlights how `asterisk <https://pydantic-docs.helpmanual.io/usage/validators/#pre-and-per-item-validators>`_
(``@validator('*')``) and `root validators <https://pydantic-docs.helpmanual.io/usage/validators/#root-validators>`_ (``@root_validator``)
(``@field_validator('*')``) and `root validators <https://pydantic-docs.helpmanual.io/usage/validators/#root-validators>`_ (``@root_validator``)
are represented. Since they validate all fields, their corresponding field reference is replaced
with a simple ``all fields`` marker which hyperlinks to the related model itself.

Expand Down Expand Up @@ -333,4 +333,4 @@ and the
.. tab:: python

.. autocodeblock:: target.example_reused_validators
.. autocodeblock:: target.example_reused_validators
35 changes: 2 additions & 33 deletions docs/source/users/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ specialized directives provided by *autodoc_pydantic*:
- :ref:`autopydantic_settings`
- :ref:`autopydantic_field`
- :ref:`autopydantic_validator`
- :ref:`autopydantic_config`

.. _autopydantic_model:

Expand All @@ -126,11 +125,11 @@ sensible default settings.

.. code-block:: rest
.. autopydantic_model:: target.usage_model.ExampleModel
.. autopydantic_model:: target.usage_model.ExampleSettings
.. tab:: *rendered output*

.. autopydantic_model:: target.usage_model.ExampleModel
.. autopydantic_model:: target.usage_model.ExampleSettings
:noindex:

.. tab:: python
Expand Down Expand Up @@ -227,33 +226,3 @@ from its corresponding pydantic model/settings but it is still possible.
To overwrite global defaults, the following directive options can be supplied:

.. documenter_config_toc:: validator

.. _autopydantic_config:

autopydantic_config
-------------------

Very rarely, you may want to document a pydantic config class without the corresponding
pydantic model/setting. However, technically it's possible since the :code:`autopydantic_config`
directive is used by the :code:`autopydantic_model` and :code:`autopydantic_settings`.

.. tabs::

.. tab:: reST

.. code-block:: rest
.. autopydantic_config:: target.usage_setting.ExampleSettings.Config
.. tab:: *rendered output*

.. autopydantic_config:: target.usage_setting.ExampleSettings.Config
:noindex:

.. tab:: python

.. autocodeblock:: target.usage_setting

To overwrite global defaults, the following directive options can be supplied:

.. documenter_config_toc:: config
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "autodoc_pydantic"
version = "1.9.0"
version = "2.0.0"
description = "Seamlessly integrate pydantic models in your Sphinx documentation."
authors = ["mansenfranzen <[email protected]>"]
packages = [{ include = "sphinxcontrib" }]
Expand All @@ -19,8 +19,9 @@ include = ["sphinxcontrib/autodoc_pydantic/css/autodoc_pydantic.css"]

[tool.poetry.dependencies]
python = ">=3.7.1,<4.0.0"
Sphinx = ">=3.4"
pydantic = ">=1.5,<2.0.0"
Sphinx = ">=4.0"
pydantic = ">=2.0,<3.0.0"
pydantic-settings = ">=2.0,<3.0.0"

sphinx-rtd-theme = { version = "^1.0", optional = true }
sphinx-tabs = { version = "^3", optional = true }
Expand All @@ -30,7 +31,7 @@ pytest = {version = "^7", optional = true }
coverage = { version ="^7", optional = true }
flake8 = { version = "^3", optional = true }
tox = { version ="^3", optional = true }
erdantic= { version ="^0.5", optional = true }
erdantic = { version ="^0.6", optional = true }

[tool.pytest.ini_options]
minversion = "6.0"
Expand Down
7 changes: 2 additions & 5 deletions sphinxcontrib/autodoc_pydantic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from pathlib import Path
from typing import Dict, Any
from importlib.metadata import version

from sphinx.domains import ObjType
from sphinx.application import Sphinx

from sphinxcontrib.autodoc_pydantic.directives.autodocumenters import (
PydanticValidatorDocumenter,
PydanticModelDocumenter,
PydanticConfigClassDocumenter,
PydanticFieldDocumenter,
PydanticSettingsDocumenter
)
Expand All @@ -20,13 +20,12 @@

from sphinxcontrib.autodoc_pydantic.directives.directives import (
PydanticField,
PydanticConfigClass,
PydanticValidator,
PydanticModel,
PydanticSettings
)

__version__ = "1.8.0"
__version__ = version("autodoc_pydantic")

from sphinxcontrib.autodoc_pydantic.events import add_fallback_css_class

Expand Down Expand Up @@ -135,15 +134,13 @@ def add_directives_and_autodocumenters(app: Sphinx):
app.add_directive_to_domain("py", "pydantic_field", PydanticField)
app.add_directive_to_domain("py", "pydantic_model", PydanticModel)
app.add_directive_to_domain("py", "pydantic_settings", PydanticSettings)
app.add_directive_to_domain("py", "pydantic_config", PydanticConfigClass)
app.add_directive_to_domain("py", "pydantic_validator", PydanticValidator)

app.setup_extension('sphinx.ext.autodoc')
app.add_autodocumenter(PydanticFieldDocumenter)
app.add_autodocumenter(PydanticModelDocumenter)
app.add_autodocumenter(PydanticSettingsDocumenter)
app.add_autodocumenter(PydanticValidatorDocumenter)
app.add_autodocumenter(PydanticConfigClassDocumenter)

app.connect('object-description-transform', add_fallback_css_class)

Expand Down
Loading

0 comments on commit c0b011a

Please sign in to comment.