Skip to content

Commit ec00297

Browse files
committed
feature: Detect and show ClassVar in generated docs
ClassVar is shown as `pydantic-field` right now, this commit fixes it, and shows `pydantic-classvar` instead of it. Issue #18: feature: greater distinction for model `ClassVar`
1 parent c726fc7 commit ec00297

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ Discussions = "https://github.com/mkdocstrings/griffe-pydantic/discussions"
4242
Gitter = "https://gitter.im/mkdocstrings/griffe-pydantic"
4343
Funding = "https://github.com/sponsors/pawamoy"
4444

45-
[project.entry-points."mkdocstrings.python.templates"]
46-
griffe-pydantic = "griffe_pydantic:get_templates_path"
45+
[project.entry-points."mkdocstrings.python.templates"]
46+
griffe-pydantic = "griffe_pydantic:get_templates_path"
4747

4848
[tool.pdm]
4949
version = {source = "scm"}

src/griffe_pydantic/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def get_templates_path() -> Path:
1515
return Path(__file__).parent / "templates"
1616

1717

18-
__all__: list[str] = ["get_templates_path", "PydanticExtension"]
18+
__all__: list[str] = ["PydanticExtension", "get_templates_path"]

src/griffe_pydantic/static.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def process_attribute(attr: Attribute, cls: Class, *, processed: set[str]) -> No
9393
cls.extra[common.self_namespace]["config"] = kwargs
9494
return
9595

96-
attr.labels = {"pydantic-field"}
96+
# If attr.labels is ONLY "class-attribute", then it is ClassVar
97+
# otherwise attr.label == {"class-attribute", "instance-attribute"}
98+
attr.labels = {"pydantic-field"} if {"class-attribute"} != attr.labels else {"pydantic-classvar"}
99+
97100
attr.value = kwargs.get("default", None)
98101
constraints = {kwarg: value for kwarg, value in kwargs.items() if kwarg not in {"default", "description"}}
99102
attr.extra[common.self_namespace]["constraints"] = constraints

tests/test_extension.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import logging
66
from typing import TYPE_CHECKING
77

8-
import pytest
98
from griffe import Extensions, temporary_visited_package
109

1110
from griffe_pydantic.extension import PydanticExtension
1211

1312
if TYPE_CHECKING:
13+
import pytest
1414
from mkdocstrings_handlers.python.handler import PythonHandler
1515

1616

0 commit comments

Comments
 (0)