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

fix(deps): update dependency erdantic to v1 #246

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/source/users/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ This example shows the rendered output of a pydantic model including an Entity-R

.. tab:: *rendered output*

.. autopydantic_model:: target.example_generics.Response
.. autopydantic_model:: target.example_erdantic.Order
:noindex:
:model-erdantic-figure: True
:model-erdantic-figure-collapsed: False
Expand All @@ -63,13 +63,13 @@ This example shows the rendered output of a pydantic model including an Entity-R

.. code-block::

.. autopydantic_model:: target.example_generics.Response
.. autopydantic_model:: target.example_erdantic.Order
:model-erdantic-figure: True
:model-erdantic-figure-collapsed: False

.. tab:: python

.. autocodeblock:: target.example_generics
.. autocodeblock:: target.example_erdantic


-----------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ coverage = { version ="^7", optional = true }
ruff = { version = "^0.3", optional = true }

# extras erdantic
erdantic = { version ="^0.7.0", optional = true }
erdantic = { version ="<2.0", optional = true }

[tool.poetry.extras]
docs = ["sphinx-rtd-theme",
Expand Down
6 changes: 5 additions & 1 deletion sphinxcontrib/autodoc_pydantic/directives/autodocumenters.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ def add_erdantic_figure(self) -> None:
raise RuntimeError(error_msg)

# Graphviz [DOT language](https://graphviz.org/doc/info/lang.html)
figure_dot = erd.to_dot(self.object).replace('\t', ' ').split('\n')
figure_dot = (
erd.to_dot(self.object, graph_attr={'label': ''})
.replace('\t', ' ')
.split('\n')
)
lines_dot = [' ' + line for line in figure_dot]
lines = ['.. graphviz::', '', *lines_dot, '']

Expand Down
9 changes: 0 additions & 9 deletions tests/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@
from sphinx.addnodes import desc_sig_punctuation, desc_annotation, pending_xref


def package_is_missing(package_name):
"""Check if a Python package is not available"""
try:
importlib.import_module(package_name)
return False
except ImportError:
return True


def desc_annotation_default_value(value: str):
"""Provides compatibility abstraction for `desc_annotation` for default
values for sphinx version smaller and greater equal sphinx 4.3.
Expand Down
34 changes: 34 additions & 0 deletions tests/roots/test-base/target/example_erdantic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

from pydantic import BaseModel


class ProductCategory(BaseModel):
"""Product category representation."""

id: int
name: str


class Product(BaseModel):
"""Product representation."""

id: int
name: str
category: ProductCategory


class Customer(BaseModel):
"""Customer representation."""

id: int
name: str


class Order(BaseModel):
"""Order representation."""

id: int
customer: Customer
products: list[Product]
total: float
54 changes: 28 additions & 26 deletions tests/test_configuration_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sphinx.testing.util import assert_node

from sphinxcontrib.autodoc_pydantic import PydanticModelDocumenter
from .compatibility import desc_annotation_directive_prefix, package_is_missing
from .compatibility import desc_annotation_directive_prefix

KWARGS = dict(documenter=PydanticModelDocumenter.objtype, deactivate_all=True)

Expand Down Expand Up @@ -97,8 +97,9 @@ def test_autodoc_pydantic_model_show_json_false(autodocument):
assert actual == result


@pytest.mark.skipif(package_is_missing('erdantic'), reason='erdantic missing')
def test_autodoc_pydantic_model_erdantic_figure_true(autodocument):
pytest.importorskip('erdantic', minversion='1.0', reason='erdantic missing')

kwargs = dict(object_path='target.configuration.ModelErdanticFigure', **KWARGS)

result = [
Expand All @@ -116,24 +117,27 @@ def test_autodoc_pydantic_model_erdantic_figure_true(autodocument):
'',
' .. graphviz::',
'',
' digraph "Entity Relationship Diagram" {',
' digraph "Entity Relationship Diagram created by erdantic" {',
' graph [fontcolor=gray66,',
' fontname="Times New Roman,Times,Liberation Serif,serif",',
' fontsize=9,',
' label="Created by erdantic v0.5.0 <https://github.com/drivendataorg/erdantic>",',
' nodesep=0.5,',
' rankdir=LR,',
' ranksep=1.5',
' ];',
' node [fontsize=14,',
' node [fontname="Times New Roman,Times,Liberation Serif,serif",',
' fontsize=14,',
' label="\\N",',
' shape=plain',
' ];',
' edge [dir=both];',
' "target.configuration.ModelErdanticFigure" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>ModelErdanticFigure</b></td></tr><tr><td>field1</td><td port="field1">int</td></tr><tr><td>field2</td><td port="field2">str</td></tr><tr><td>related</td><td port="related">ModelErdanticFigureRelated</td></tr></table>>,',
' tooltip="target.configuration.ModelErdanticFigure&#xA;&#xA;ModelErdanticFigure.&#xA;"];',
' "target.configuration.ModelErdanticFigureRelated" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>ModelErdanticFigureRelated</b></td></tr><tr><td>field1</td><td port="field1">int</td></tr><tr><td>field2</td><td port="field2">str</td></tr></table>>,',
' tooltip="target.configuration.ModelErdanticFigureRelated&#xA;&#xA;ModelErdanticFigureRelated.&#xA;"];',
' "target.configuration.ModelErdanticFigure":related:e -> "target.configuration.ModelErdanticFigureRelated":_root:w [arrowhead=noneteetee];',
' }' '',
' "target.configuration.ModelErdanticFigure":related:e -> "target.configuration.ModelErdanticFigureRelated":_root:w [arrowhead=noneteetee,',
' arrowtail=nonenone];',
' }',
'',
'',
'',
Expand All @@ -143,31 +147,28 @@ def test_autodoc_pydantic_model_erdantic_figure_true(autodocument):
'',
'',
]

# explicit global
actual = autodocument(
options_app={'autodoc_pydantic_model_erdantic_figure': True}, **kwargs
)
assert actual[:17] == result[:17]
assert actual[18:] == result[18:]
assert actual == result

# explicit local
actual = autodocument(options_doc={'model-erdantic-figure': True}, **kwargs)
assert actual[:17] == result[:17]
assert actual[18:] == result[18:]
assert actual == result

# explicit local overwrite global
actual = autodocument(
options_app={'autodoc_pydantic_model_erdantic_figure': False},
options_doc={'model-erdantic-figure': True},
**kwargs,
)
assert actual[:17] == result[:17]
assert actual[18:] == result[18:]
assert actual == result


@pytest.mark.skipif(package_is_missing('erdantic'), reason='erdantic missing')
def test_autodoc_pydantic_model_erdantic_figure_collapsed_false(autodocument):
pytest.importorskip('erdantic', minversion='1.0', reason='erdantic missing')

kwargs = dict(object_path='target.configuration.ModelErdanticFigure', **KWARGS)

result = [
Expand All @@ -179,23 +180,26 @@ def test_autodoc_pydantic_model_erdantic_figure_collapsed_false(autodocument):
'',
' .. graphviz::',
'',
' digraph "Entity Relationship Diagram" {',
' digraph "Entity Relationship Diagram created by erdantic" {',
' graph [fontcolor=gray66,',
' fontname="Times New Roman,Times,Liberation Serif,serif",',
' fontsize=9,',
' label="Created by erdantic v0.5.0 <https://github.com/drivendataorg/erdantic>",',
' nodesep=0.5,',
' rankdir=LR,',
' ranksep=1.5',
' ];',
' node [fontsize=14,',
' node [fontname="Times New Roman,Times,Liberation Serif,serif",',
' fontsize=14,',
' label="\\N",',
' shape=plain',
' ];',
' edge [dir=both];',
' "target.configuration.ModelErdanticFigure" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>ModelErdanticFigure</b></td></tr><tr><td>field1</td><td port="field1">int</td></tr><tr><td>field2</td><td port="field2">str</td></tr><tr><td>related</td><td port="related">ModelErdanticFigureRelated</td></tr></table>>,',
' tooltip="target.configuration.ModelErdanticFigure&#xA;&#xA;ModelErdanticFigure.&#xA;"];',
' "target.configuration.ModelErdanticFigureRelated" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>ModelErdanticFigureRelated</b></td></tr><tr><td>field1</td><td port="field1">int</td></tr><tr><td>field2</td><td port="field2">str</td></tr></table>>,',
' tooltip="target.configuration.ModelErdanticFigureRelated&#xA;&#xA;ModelErdanticFigureRelated.&#xA;"];',
' "target.configuration.ModelErdanticFigure":related:e -> "target.configuration.ModelErdanticFigureRelated":_root:w [arrowhead=noneteetee];',
' "target.configuration.ModelErdanticFigure":related:e -> "target.configuration.ModelErdanticFigureRelated":_root:w [arrowhead=noneteetee,',
' arrowtail=nonenone];',
' }',
'',
'',
Expand All @@ -209,8 +213,7 @@ def test_autodoc_pydantic_model_erdantic_figure_collapsed_false(autodocument):
},
**kwargs,
)
assert actual[:11] == result[:11]
assert actual[12:] == result[12:]
assert actual == result

# explicit local
actual = autodocument(
Expand All @@ -220,8 +223,7 @@ def test_autodoc_pydantic_model_erdantic_figure_collapsed_false(autodocument):
},
**kwargs,
)
assert actual[:11] == result[:11]
assert actual[12:] == result[12:]
assert actual == result

# explicit local overwrite global
actual = autodocument(
Expand All @@ -232,12 +234,12 @@ def test_autodoc_pydantic_model_erdantic_figure_collapsed_false(autodocument):
},
**kwargs,
)
assert actual[:11] == result[:11]
assert actual[12:] == result[12:]
assert actual == result


@pytest.mark.skipif(package_is_missing('erdantic'), reason='erdantic missing')
def test_autodoc_pydantic_model_erdantic_figure_false(autodocument):
pytest.importorskip('erdantic', minversion='1.0', reason='erdantic missing')

kwargs = dict(object_path='target.configuration.ModelErdanticFigure', **KWARGS)

result = [
Expand Down