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

Finalize pydantic v2 #162

Merged
merged 39 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
90dea4d
Initial update for changelog.
Jul 22, 2023
4e724c5
Remove `show-config-member` options from configuration docs.
Jul 23, 2023
716d04d
Add note on v1 vs v2 in user installation docs.
Jul 23, 2023
c382fed
Minor formatting improvement.
Jul 23, 2023
5f2e259
Remove the `complete` example as being irrelevant.
Jul 23, 2023
4ceb7e7
Add FAQ for v2 migration.
Jul 23, 2023
ae2b194
Reduce installation note to the minimum required.
Jul 23, 2023
b92841f
Add note about separate v1/v2 branches.
Jul 23, 2023
34b7c39
Add note for current documentation version. Update features.
Jul 23, 2023
7731866
Update README.
Jul 23, 2023
505b92a
Remove global config member confs from models/settings.
Jul 23, 2023
22eac1c
Update readme and index.
Jul 23, 2023
3bc19ed
Add link to developer guide in readme.
Jul 23, 2023
becd26a
Replace BaseModel with NamedTuple for ValidatorAdapter.
Jul 23, 2023
7dbc583
Remove obsolete pre/post root validator attributes.
Jul 23, 2023
0128dd5
Add correct hyperlinks.
Jul 23, 2023
7cc1332
Linter changes.
Jul 23, 2023
8fca991
Add `importlib-metadata` for python 3.7.
Jul 23, 2023
ffae62f
Update sphinx version support to >4.0.0.
Jul 23, 2023
090f024
Remove dep range for importlib-metadata.
Jul 23, 2023
2144efa
Fix importlib import error.
Jul 23, 2023
5f0d6a9
Fix import error for `Annotated`.
Jul 23, 2023
e754a8a
Fix string representation of annotated types in py<3.9.
Jul 23, 2023
fe29c7f
Lint fixes.
Jul 23, 2023
dbcfa4c
Provide more explicit test name.
Jul 23, 2023
97b9c16
Improve migration doc - ci skip.
Jul 23, 2023
a06f9a1
Add erdantic to dev setup install.
Jul 24, 2023
599475f
Remove obsolete config documenter directive options.
Jul 24, 2023
73ff9fa
Improve docs on FAQ migration.
Jul 24, 2023
cc6ad27
Remove obsolete pydantic compatibility test helpers.
Jul 24, 2023
6df261c
Update changelog - ci skip.
Jul 24, 2023
e745537
docs: update README.md [skip ci]
allcontributors[bot] Jul 24, 2023
aca3dcb
docs: update .all-contributorsrc [skip ci]
allcontributors[bot] Jul 24, 2023
01c9941
Merge pull request #163 from mansenfranzen/all-contributors/add-awoimbee
mansenfranzen Jul 24, 2023
024b0c6
docs: update README.md [skip ci]
allcontributors[bot] Jul 24, 2023
598579c
docs: update .all-contributorsrc [skip ci]
allcontributors[bot] Jul 24, 2023
2cadf0d
Merge branch 'finalize_pydantic_v2' into all-contributors/add-PriOliv…
mansenfranzen Jul 24, 2023
a0646de
Merge pull request #166 from mansenfranzen/all-contributors/add-PriOl…
mansenfranzen Jul 24, 2023
db01b4a
Update contributor count - skip ci.
Jul 24, 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
22 changes: 22 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,28 @@
"contributions": [
"doc"
]
},
{
"login": "PriOliveira",
"name": "Priscila Oliveira",
"avatar_url": "https://avatars.githubusercontent.com/u/13801839?v=4",
"profile": "https://github.com/PriOliveira",
"contributions": [
"review",
"userTesting"
]
},
{
"login": "awoimbee",
"name": "Arthur Woimbée",
"avatar_url": "https://avatars.githubusercontent.com/u/22431493?v=4",
"profile": "https://github.com/awoimbee",
"contributions": [
"review",
"userTesting",
"code",
"test"
]
}
],
"contributorsPerLine": 7,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
with:
fail_ci_if_error: true

test-special-version:
test-no-erdantic-version:
if: "!contains(github.event.head_commit.message, 'skip ci')"
environment: ci
runs-on: ubuntu-22.04
Expand Down
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Coverage](https://img.shields.io/codecov/c/gh/mansenfranzen/autodoc_pydantic?style=for-the-badge)](https://app.codecov.io/gh/mansenfranzen/autodoc_pydantic)

[![Downloads](https://img.shields.io/pypi/dm/autodoc_pydantic?color=fe7d37&style=for-the-badge)](https://pypistats.org/packages/autodoc-pydantic)<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-32-orange.svg?style=for-the-badge)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-34-orange.svg?style=for-the-badge)](#contributors)
<!-- ALL-CONTRIBUTORS-BADGE:END -->


Expand All @@ -28,15 +28,8 @@ Don't worry - just `pip install autodoc_pydantic` ☺.
- 📎 defines explicit pydantic prefixes for models, settings, fields, validators and model config
- 📋 shows summary section for model configuration, fields and validators
- 👀 hides overloaded and redundant model class signature
- 📚 sorts fields, validators and model config within models by type
- 🍀 Supports `pydantic >= 1.5.0, < 2.0.0` and `sphinx >= 3.4.0`

### Comparison between autodoc sphinx and autodoc pydantic

[![Comparison](https://raw.githubusercontent.com/mansenfranzen/autodoc_pydantic/main/docs/source/material/example_comparison_v1.0.0.gif)](https://autodoc-pydantic.readthedocs.io/en/latest/examples.html#default-configuration)

To see those features in action, jump over to the [example documentation](https://autodoc-pydantic.readthedocs.io/en/stable/users/examples.html#default-configuration) to compare
the appearance of standard sphinx autodoc with *autodoc_pydantic*.
- 🔱 visualizes entity-relationship-diagrams for class hierarchies
- 🍀 Supports `pydantic >= 1.5.0` and `sphinx >= 4.0.0`

## Documentation

Expand All @@ -46,6 +39,14 @@ For more details, please visit the official [documentation](https://autodoc-pyda
- [Configuration](https://autodoc-pydantic.readthedocs.io/en/stable/users/configuration.html)
- [Usage](https://autodoc-pydantic.readthedocs.io/en/stable/users/usage.html)
- [Examples](https://autodoc-pydantic.readthedocs.io/en/stable/users/examples.html)
- [Developer Guide](https://autodoc-pydantic.readthedocs.io/en/stable/developers/setup.html)

## Comparison between autodoc sphinx and autodoc pydantic

[![Comparison](https://raw.githubusercontent.com/mansenfranzen/autodoc_pydantic/main/docs/source/material/example_comparison_v1.0.0.gif)](https://autodoc-pydantic.readthedocs.io/en/latest/examples.html#default-configuration)

To see those features in action, jump over to the [example documentation](https://autodoc-pydantic.readthedocs.io/en/stable/users/examples.html#default-configuration) to compare
the appearance of standard sphinx autodoc with *autodoc_pydantic*.

## Acknowledgements

Expand Down Expand Up @@ -99,6 +100,8 @@ Thanks to great open source projects [sphinx](https://www.sphinx-doc.org/en/mast
<td align="center" valign="top" width="14.28%"><a href="http://charlie.machalow.com"><img src="https://avatars.githubusercontent.com/u/5749838?v=4?s=100" width="100px;" alt="Charles Machalow"/><br /><sub><b>Charles Machalow</b></sub></a><br /><a href="#question-csm10495" title="Answering Questions">💬</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tkaraouzene"><img src="https://avatars.githubusercontent.com/u/20064077?v=4?s=100" width="100px;" alt="Thomas Karaouzene"/><br /><sub><b>Thomas Karaouzene</b></sub></a><br /><a href="https://github.com/mansenfranzen/autodoc_pydantic/issues?q=author%3Atkaraouzene" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/caseyzak24"><img src="https://avatars.githubusercontent.com/u/29411281?v=4?s=100" width="100px;" alt="caseyzak24"/><br /><sub><b>caseyzak24</b></sub></a><br /><a href="https://github.com/mansenfranzen/autodoc_pydantic/commits?author=caseyzak24" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/PriOliveira"><img src="https://avatars.githubusercontent.com/u/13801839?v=4?s=100" width="100px;" alt="Priscila Oliveira"/><br /><sub><b>Priscila Oliveira</b></sub></a><br /><a href="https://github.com/mansenfranzen/autodoc_pydantic/pulls?q=is%3Apr+reviewed-by%3APriOliveira" title="Reviewed Pull Requests">👀</a> <a href="#userTesting-PriOliveira" title="User Testing">📓</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/awoimbee"><img src="https://avatars.githubusercontent.com/u/22431493?v=4?s=100" width="100px;" alt="Arthur Woimbée"/><br /><sub><b>Arthur Woimbée</b></sub></a><br /><a href="https://github.com/mansenfranzen/autodoc_pydantic/pulls?q=is%3Apr+reviewed-by%3Aawoimbee" title="Reviewed Pull Requests">👀</a> <a href="#userTesting-awoimbee" title="User Testing">📓</a> <a href="https://github.com/mansenfranzen/autodoc_pydantic/commits?author=awoimbee" title="Code">💻</a> <a href="https://github.com/mansenfranzen/autodoc_pydantic/commits?author=awoimbee" title="Tests">⚠️</a></td>
</tr>
</tbody>
</table>
Expand Down
88 changes: 88 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,94 @@
Changelog
=========

v2.0.0 - 2023-07-XX
-------------------

This is a major release supporting pydantic v2. In June 2023, pydantic v2 was
released while introducing backwards incompatible API and behavioral changes in
comparison to pydantic v1. Supporting pydantic v2 required substantial
adjustments to the codebase leading to a new major release of autodoc_pydantic
(v1.9.0 -> v2.0.0), too.

In order to keep the codebase clean and concise, separate versions for v1 and
v2 were created. The v2 branch will eventually become the new main branch
while the code for v1 remains in the main-1.x branch.

Changed Behavior
~~~~~~~~~~~~~~~~

- Documenting pydantic model configurations in isolation or as a separate
member of the pydantic model is no longer available. The following options
have been removed:

- ``autodoc_pydantic_model_show_config_member``
- ``autodoc_pydantic_settings_show_config_member``
- ``autodoc_pydantic_config_members``
- ``autodoc_pydantic_config_signature_prefix``

- All semantic changes from pydantic v1 to v2 take full effect.
**autodoc_pydantic** does not modify the underlying behavior of pydantic in
any way. Instead, it only documents whatever pydantic exposes. Hence, all
behavioral changes such as the new default strict mode are preserved in v2.

- Sphinx ``< 4.0.0`` is no longer supported.

Features
~~~~~~~~

- Support for pydantic v2 💫.
- Support annotated type hints.

Internal
~~~~~~~~

- Adjust imports to refer to ``pydantic-settings`` (v2) instead of ``pydantic`` (v1).
- Adjust imports to refer to ``field_validator`` (v2) insteaf of ``validator`` (v1).
- Adjust imports to refer to ``model_validator`` (v2) insteaf of ``root_validator`` (v1).
- Replace ``pydantic.generics.GenericModel`` (v1) with ``typing.Generic`` (v2).
- Simplify ``ValidatorAdapter`` and ``ValidatorInspector``.
- Simplify reused validators retrieval.
- Completely rewrite the model's field constraint retrieval functionality in ``inspection.FieldInspector``.
- Adjust model's field serializability checks in ``inspection.FieldInspector``.
- Replace ``BaseModel`` with ``NamedTuple`` for ``ValidatorAdapter``.
- Remove obsolete pre/post validator attributes.
- Introduce ``importlib-metadata`` to fetch version number including support for python 3.7.

Testing
~~~~~~~

- Remove all obsolete pydantic versions from test matrix.
- Remove all tests for documenting config members.
- Remove compatibility helpers for older pydantic versions.
- Remove obsolete pydantic model example which was not used anywhere.
- Adjust serializability tests to account for changed behavior in v2.
- Adjust optional/required field marker tests to account for changed behavior in v2.
- Adjust field constraint tests to account for changed behavior in v2.
- Adjust erdantic tests to exclude the erdantic version number which caused tests to fail upon erdantic update.

Documentation
~~~~~~~~~~~~~

- Add FAQ section regarding migration guide from v1 to v2.
- Remove ``complete`` showcase from user's example.
- Update READMEs with newest features and version specifiers.
- Update developer's setup section to address v1 to v2 changes.
- Updates user's installation section to address v1 to v2 changes.
- Remove all obsolete documentation on removed config documenters.
- Rename all occurences to v2 ``field_validator`` and ``model_validator``.

Contributors
~~~~~~~~~~~~

- Special thanks to `awoimbee <https://github.com/awoimbee>`__ for providing
a draft for the v1 to v2 migration which really initiated the work on
supporting pydantic v2
`#160 <https://github.com/mansenfranzen/autodoc_pydantic/pull/160>`__.
- Many thanks to `PriOliveira <https://github.com/PriOliveira>`__ for reviewing
changes required for the v1 to v2 release
`#160 <https://github.com/mansenfranzen/autodoc_pydantic/pull/160>`__.


v1.9.0 - 2023-06-08
-------------------

Expand Down
19 changes: 17 additions & 2 deletions docs/source/developers/setup.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.. _main-1.x: https://github.com/mansenfranzen/autodoc_pydantic/tree/main-1.x
.. _main: https://github.com/mansenfranzen/autodoc_pydantic/tree/main

=====
Setup
=====
Expand All @@ -17,12 +20,24 @@ Cloning repository
git clone https://github.com/mansenfranzen/autodoc_pydantic.git
cd autodoc_pydantic

.. note::

In June 2023, **pydantic v2** was released. This introduced backwards
incompatible API and behavioral changes in comparison to pydantic v1. Hence,
**autodoc_pydantic** required substantial adjustments, too. In order to keep
the codebase clean and concise, separate versions for v1 and v2 were
created. The v2 branch will eventually become the new `main`_ branch while
the code for v1 remains in the `main-1.x`_ branch.

In a nutshell, if you want to work on v1, please checkout the corresponding
`main-1.x`_ branch. Otherwise, refer to the `main`_ branch.

Creating environment
--------------------

.. code-block:: bash

poetry install -E dev
poetry install -E dev -E erdantic

-----------------------
Running & writing tests
Expand Down Expand Up @@ -63,7 +78,7 @@ Please visit the ``tox.ini`` for all available test environments.
Building & writing docs
-----------------------

**autodoc_pydantic**'s documentation is generated with `sphinx <https://www.sphinx-doc.org>`__.
**autodoc_pydantic**'s documentation is generated with `sphinx <https://www.sphinx-doc.org>`_ .
To generate the HTML documentation, please use the following:

.. code-block:: bash
Expand Down
12 changes: 9 additions & 3 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ Features
- 📎 defines explicit pydantic prefixes for models, settings, fields, validators and model config
- 📋 shows summary section for model configuration, fields and validators
- 👀 hides overloaded and redundant model class signature
- 📚 sorts fields, validators and model config within models by type
- 🍀 Supports `pydantic >= 1.5.0, <2.0.0` and `sphinx >= 3.4.0`
- 🔱 visualizes entity-relationship-diagrams for class hierarchies
- 🍀 Supports `pydantic >= 1.5.0` and `sphinx >= 4.0.0`

To see those features in action, jump over to the :ref:`example <showcase>` section comparing
the appearance of standard sphinx autodoc with **autodoc_pydantic**.

.. note::

This documentation is based on ``autodoc_pydantic >= 2.0.0``. If you are
using pydantic v1 along with ``autodoc_pydantic < 2.0.0``, please find the
latest v1 documentation `here <https://autodoc-pydantic.readthedocs.io/en/main-1.x/>`_ .

.. toctree::
:maxdepth: 1
:caption: Users
Expand Down Expand Up @@ -80,7 +86,7 @@ and all `contributors <https://github.com/mansenfranzen/autodoc_pydantic/tree/re
.. |DownloadsBadge| image:: https://img.shields.io/pypi/dm/autodoc_pydantic?color=fe7d37&style=flat
.. _DownloadsBadge: https://pypistats.org/packages/autodoc-pydantic

.. |ContributersBadge| image:: https://img.shields.io/badge/all_contributors-31-orange.svg?style=flat
.. |ContributersBadge| image:: https://img.shields.io/badge/all_contributors-35-orange.svg?style=flat
.. _ContributersBadge: https://github.com/mansenfranzen/autodoc_pydantic/tree/refactor_inspection#acknowledgements

.. |CoverageBadge| image:: https://img.shields.io/codecov/c/gh/mansenfranzen/autodoc_pydantic?style=flat
Expand Down
34 changes: 3 additions & 31 deletions docs/source/users/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ how pydantic objects are displayed:

.. autopydantic_model:: module.Model
:model-show-json: True
:model-show-config: False
:model-show-config-summary: False


.. note::
Expand All @@ -47,21 +47,7 @@ Contains all modifications for pydantic `BaseModel`.
:values: True, False

Show model config summary within the class doc string. It may be meaningful
when the class configuration carries some relevant information and you don't
want to show the entire config class as an explicit member
(see :ref:`model-show-config-member <autodoc_pydantic_model_show_config_member>`).


.. config_description:: autopydantic_model
:title: Show Config Member
:path: target.configuration.ModelShowConfigMember
:confpy: autodoc_pydantic_model_show_config_member
:directive_option: model-show-config-member
:enable: members
:values: True, False*

Show pydantic config class. It can be hidden if it is irrelevant or if
replaced with :ref:`model-show-config-summary <autodoc_pydantic_model_show_config_summary>`.
when the class configuration carries some relevant information.


.. config_description:: autopydantic_model
Expand Down Expand Up @@ -277,21 +263,7 @@ Contains all modifications for pydantic `BaseSettings`.
:values: True, False

Show model config summary within the class doc string. It may be meaningful
when the class configuration carries some relevant information and you don't
want to show the entire config class as an explicit member
(see :ref:`settings-show-config-member <autodoc_pydantic_settings_show_config_member>`).


.. config_description:: autopydantic_settings
:title: Show Config Member
:path: target.configuration.SettingsShowConfigMember
:confpy: autodoc_pydantic_settings_show_config_member
:directive_option: settings-show-config-member
:enable: members
:values: True, False*

Show pydantic config class. It can be hidden if it is irrelevant or if
replaced with :ref:`settings-show-config-summary <autodoc_pydantic_settings_show_config_summary>`.
when the class configuration carries some relevant information.


.. config_description:: autopydantic_settings
Expand Down
52 changes: 9 additions & 43 deletions docs/source/users/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ In contrast, it also shows how standard sphinx autodoc displays the same example
.. autopydantic_settings:: target.example_setting.ExampleSettings
:members:
:undoc-members:
:__doc_disable_except__: members, undoc-members, settings-show-validator-members, settings-show-config-member, config-members
:__doc_disable_except__: members, undoc-members, settings-show-validator-members
:noindex:

.. tab:: python
Expand All @@ -43,38 +43,6 @@ In contrast, it also shows how standard sphinx autodoc displays the same example
.. autopydantic_settings:: target.example_setting.ExampleSettings


--------
Complete
--------

This example represents a rendered output for which all features are enabled.
It deviates from the default configuration above because it contains redundant
information which is most likely not required. However, for demonstration purposes,
this scenario covers all available display options for pydantic models/settings.


.. tabs::

.. tab:: *rendered output*

.. autopydantic_settings:: target.example_setting.ExampleSettings
:noindex:
:settings-show-config-member: True
:validator-list-fields: True

.. tab:: reST

.. code-block::

.. autopydantic_settings:: target.example_setting.ExampleSettings
:noindex:
:settings-show-config-member: True
:validator-list-fields: True

.. tab:: python

.. autocodeblock:: target.example_setting

----------------------------
Entity-Relationship Diagram
----------------------------
Expand Down Expand Up @@ -118,7 +86,6 @@ and model/setting config is hidden.
.. autopydantic_settings:: target.example_setting.ExampleSettings
:noindex:
:settings-show-json: False
:settings-show-config-member: False
:settings-show-config-summary: False
:settings-show-validator-members: False
:settings-show-validator-summary: False
Expand All @@ -131,7 +98,6 @@ and model/setting config is hidden.

.. autopydantic_settings:: target.example_setting.ExampleSettings
:settings-show-json: False
:settings-show-config-member: False
:settings-show-config-summary: False
:settings-show-validator-members: False
:settings-show-validator-summary: False
Expand All @@ -146,16 +112,16 @@ Specifics
=========

This section focuses rendered documentation examples of pydantic specific
concepts such as root validators, required/optional fields or generic models.
concepts such as model validators, required/optional fields or generic models.

----------------------------
Asterisk and root validators
----------------------------
----------------
Model validators
----------------

This example highlights how `asterisk <https://pydantic-docs.helpmanual.io/usage/validators/#pre-and-per-item-validators>`_
(``@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.
This example highlights how ``model validators <https://docs.pydantic.dev/latest/usage/validators/#model-validators>`_
(``@model_validator`` or ``@field_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.

.. tabs::

Expand Down
Loading
Loading