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

pydantic v2 #160

Merged
merged 42 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b56fe0a
update pydantic dep
awoimbee Jul 6, 2023
ffd7fbb
poetry lock
awoimbee Jul 6, 2023
6785aa6
up sphinx default to 4
awoimbee Jul 6, 2023
40111d7
poetry lock
awoimbee Jul 6, 2023
ecf9486
pyproject: add pydantic-settings
awoimbee Jul 6, 2023
84d3493
ock
awoimbee Jul 6, 2023
cb9c3d8
pydantic v2 early compat
awoimbee Jul 6, 2023
caa1401
no import error, limited warnings
awoimbee Jul 12, 2023
786b84b
more tests passing
awoimbee Jul 18, 2023
c22c76d
Remove poetry.lock
Jul 20, 2023
0582abe
Order of constraints is now alphabetical in v2.
Jul 20, 2023
b7e02fd
Fix ignore additional kwargs in Field for v2.
Jul 20, 2023
c905902
Adapt to changed behavior of optional/required field values.
Jul 20, 2023
2233774
Add `Optional[int]` as required field in v2.
Jul 21, 2023
9a90075
Change `None` to `PydanticUndefined` to due changed semantics in v2.
Jul 21, 2023
869c382
Respect order of parameters in `ConfigDict`.
Jul 21, 2023
0e744dc
Adjust tests to account for removed config member.
Jul 21, 2023
a860b59
Adjust docs to account for removed config documenter.
Jul 21, 2023
2fb22f9
Adjust to modified bound->unbound reused validators in v2.
Jul 21, 2023
a458bde
Simplify `ValidatorAdapter` & `ValidatorInspector`. Simplify reused v…
Jul 21, 2023
3584ae0
`ConfigInspector` remains mandatory for `ModelDocumenter`.
Jul 21, 2023
998d9d7
Adjust for removed config documenter.
Jul 21, 2023
e5774ce
Replace `root_validators` with `model_validators` in v2.
Jul 21, 2023
62ba8a0
Remove config directive tests in v2.
Jul 21, 2023
aede571
Support fetching constraints in v2.
Jul 22, 2023
12c1cac
Adjust serializability checks for v2.
Jul 22, 2023
48744cd
Adjust tests for v2 and account for removed config documenter.
Jul 22, 2023
7f17e40
Further test adjustments to account for better field serializability …
Jul 22, 2023
e341207
Adjust model config retrieval to work with modified `BaseSettings` in…
Jul 22, 2023
476f842
Fix generics example for v2.
Jul 22, 2023
b07222e
Remove obsolete test cases.
Jul 22, 2023
7178e9f
Adjust constraint keys to varying levels of nesting.
Jul 22, 2023
0f81c1a
Fix imports for v2 compatibility.
Jul 22, 2023
d57cfd4
Update test matrix for latest features versions.
Jul 22, 2023
1ab16b3
Fix sphinx version dependant type annotation.
Jul 22, 2023
71ccfc7
Remove erdantic version number from test.
Jul 22, 2023
1c04090
Allow python 3.7.1. Bump version to 2.0.0.
Jul 22, 2023
19ba844
Add separate test for JSON warning/coerce.
Jul 22, 2023
ff6b73d
Catch `PydanticInvalidForJsonSchema` in v2.
Jul 22, 2023
881f50f
Move schema generation after error checks.
Jul 22, 2023
17ea809
Remove reference to config documenter.
Jul 22, 2023
d6ef69f
Bump version - ci skip.
Jul 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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``)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root validator is deprecated

Suggested change
(``@field_validator('*')``) and `root validators <https://pydantic-docs.helpmanual.io/usage/validators/#root-validators>`_ (``@root_validator``)
(``@field_validator('*')``) and `model validators <https://docs.pydantic.dev/latest/usage/validators/#model-validators>`_ (``@model_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