Skip to content

Commit

Permalink
fix: Don't crash on new config based on ConfigDict
Browse files Browse the repository at this point in the history
Issue-6: #6
  • Loading branch information
pawamoy committed Nov 3, 2024
1 parent 6a3f3d2 commit c23ba7c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<p>Config:</p>
<ul>
{% for name, value in class.extra.griffe_pydantic.config.items() %}
<li><code>{{ name }}</code>: {{ value|highlight(language="python", inline=True) }}</li>
<li><code>{{ name }}</code>: {{ value|string|highlight(language="python", inline=True) }}</li>
{% endfor %}
</ul>
{% endif %}
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
"""Configuration for the pytest test suite."""

import pytest
from markdown import Markdown
from mkdocstrings_handlers.python.handler import PythonHandler


@pytest.fixture(name="python_handler")
def fixture_python_handler() -> PythonHandler:
"""Return a PythonHandler instance."""
handler = PythonHandler("python", "material")
handler.update_env(md=Markdown(extensions=["toc"]), config={})
handler.env.filters["convert_markdown"] = lambda *args, **kwargs: str(args) + str(kwargs)
return handler
38 changes: 37 additions & 1 deletion tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from griffe import Extensions, temporary_visited_package

from griffe_pydantic.extension import PydanticExtension

if TYPE_CHECKING:
from mkdocstrings_handlers.python.handler import PythonHandler


code = """
from pydantic import field_validator, ConfigDict, BaseModel, Field
Expand Down Expand Up @@ -80,7 +86,37 @@ def test_imported_models() -> None:
"__init__.py": "from ._private import MyModel\n\n__all__ = ['MyModel']",
"_private.py": "from pydantic import BaseModel\n\nclass MyModel(BaseModel):\n field1: str\n '''Some field.'''\n",
},
extensions=Extensions(PydanticExtension(schema=True)),
extensions=Extensions(PydanticExtension(schema=False)),
) as package:
assert package["MyModel"].labels == {"pydantic-model"}
assert package["MyModel.field1"].labels == {"pydantic-field"}


def test_rendering_model_config_using_configdict(python_handler: PythonHandler) -> None:
"""Test the extension with model config using ConfigDict."""
code = """
from pydantic import BaseModel, ConfigDict, Field
class Model(BaseModel):
usage: str | None = Field(
None,
description="Some description.",
example="Some example.",
)
model_config = ConfigDict(
json_schema_extra={
"example": {
"usage": "Some usage.",
"limitations": "Some limitations.",
"billing": "Some value.",
"notice_period": "Some value.",
}
}
)
"""
with temporary_visited_package(
"package",
modules={"__init__.py": code},
extensions=Extensions(PydanticExtension(schema=False)),
) as package:
python_handler.render(package["Model"], {}) # Assert no errors.

0 comments on commit c23ba7c

Please sign in to comment.