Skip to content

Commit c23ba7c

Browse files
committed
fix: Don't crash on new config based on ConfigDict
Issue-6: #6
1 parent 6a3f3d2 commit c23ba7c

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/griffe_pydantic/templates/material/_base/pydantic_model.html.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<p>Config:</p>
1818
<ul>
1919
{% for name, value in class.extra.griffe_pydantic.config.items() %}
20-
<li><code>{{ name }}</code>: {{ value|highlight(language="python", inline=True) }}</li>
20+
<li><code>{{ name }}</code>: {{ value|string|highlight(language="python", inline=True) }}</li>
2121
{% endfor %}
2222
</ul>
2323
{% endif %}

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
"""Configuration for the pytest test suite."""
2+
3+
import pytest
4+
from markdown import Markdown
5+
from mkdocstrings_handlers.python.handler import PythonHandler
6+
7+
8+
@pytest.fixture(name="python_handler")
9+
def fixture_python_handler() -> PythonHandler:
10+
"""Return a PythonHandler instance."""
11+
handler = PythonHandler("python", "material")
12+
handler.update_env(md=Markdown(extensions=["toc"]), config={})
13+
handler.env.filters["convert_markdown"] = lambda *args, **kwargs: str(args) + str(kwargs)
14+
return handler

tests/test_extension.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
from __future__ import annotations
44

5+
from typing import TYPE_CHECKING
6+
57
from griffe import Extensions, temporary_visited_package
68

79
from griffe_pydantic.extension import PydanticExtension
810

11+
if TYPE_CHECKING:
12+
from mkdocstrings_handlers.python.handler import PythonHandler
13+
14+
915
code = """
1016
from pydantic import field_validator, ConfigDict, BaseModel, Field
1117
@@ -80,7 +86,37 @@ def test_imported_models() -> None:
8086
"__init__.py": "from ._private import MyModel\n\n__all__ = ['MyModel']",
8187
"_private.py": "from pydantic import BaseModel\n\nclass MyModel(BaseModel):\n field1: str\n '''Some field.'''\n",
8288
},
83-
extensions=Extensions(PydanticExtension(schema=True)),
89+
extensions=Extensions(PydanticExtension(schema=False)),
8490
) as package:
8591
assert package["MyModel"].labels == {"pydantic-model"}
8692
assert package["MyModel.field1"].labels == {"pydantic-field"}
93+
94+
95+
def test_rendering_model_config_using_configdict(python_handler: PythonHandler) -> None:
96+
"""Test the extension with model config using ConfigDict."""
97+
code = """
98+
from pydantic import BaseModel, ConfigDict, Field
99+
100+
class Model(BaseModel):
101+
usage: str | None = Field(
102+
None,
103+
description="Some description.",
104+
example="Some example.",
105+
)
106+
model_config = ConfigDict(
107+
json_schema_extra={
108+
"example": {
109+
"usage": "Some usage.",
110+
"limitations": "Some limitations.",
111+
"billing": "Some value.",
112+
"notice_period": "Some value.",
113+
}
114+
}
115+
)
116+
"""
117+
with temporary_visited_package(
118+
"package",
119+
modules={"__init__.py": code},
120+
extensions=Extensions(PydanticExtension(schema=False)),
121+
) as package:
122+
python_handler.render(package["Model"], {}) # Assert no errors.

0 commit comments

Comments
 (0)