Skip to content

Commit 35b48f7

Browse files
committed
fixup! refactor: Use dataclasses for configuration/options and automate schema generation
1 parent 55da25c commit 35b48f7

File tree

5 files changed

+302
-69
lines changed

5 files changed

+302
-69
lines changed

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ plugins:
163163
docstring_options:
164164
ignore_init_summary: true
165165
docstring_section_style: list
166+
extensions:
167+
- scripts/griffe_extensions.py
166168
filters: ["!^_"]
167169
heading_level: 1
168170
inherited_members: true

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies = [
3333
"mkdocstrings>=0.28",
3434
"mkdocs-autorefs>=1.2",
3535
"griffe>=0.49",
36+
"typing-extensions>=4.0; python_version < '3.11'",
3637
]
3738

3839
[project.urls]
@@ -106,6 +107,7 @@ dev = [
106107
"mkdocs-git-revision-date-localized-plugin>=1.2",
107108
"mkdocs-literate-nav>=0.6",
108109
"mkdocs-material>=9.5",
110+
"pydantic>=2.10",
109111
"mkdocs-minify-plugin>=0.8",
110112
# YORE: EOL 3.10: Remove line.
111113
"tomli>=2.0; python_version < '3.11'",

scripts/griffe_extensions.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Custom extensions for Griffe."""
2+
3+
from __future__ import annotations
4+
5+
import ast
6+
from typing import Any
7+
8+
import griffe
9+
10+
logger = griffe.get_logger("griffe_extensions")
11+
12+
13+
class CustomFields(griffe.Extension):
14+
"""Support our custom dataclass fields."""
15+
16+
def on_attribute_instance(
17+
self,
18+
*,
19+
attr: griffe.Attribute,
20+
agent: griffe.Visitor | griffe.Inspector,
21+
**kwargs: Any, # noqa: ARG002
22+
) -> None:
23+
"""Fetch descriptions from `Field` annotations."""
24+
if attr.docstring:
25+
return
26+
try:
27+
field: griffe.ExprCall = attr.annotation.slice.elements[1] # type: ignore[union-attr]
28+
except AttributeError:
29+
return
30+
31+
if field.canonical_path == "mkdocstrings_handler.python.config.Field":
32+
description = next(
33+
attr.value
34+
for attr in field.arguments
35+
if isinstance(attr, griffe.ExprKeyword) and attr.name == "description"
36+
)
37+
if not isinstance(description, str):
38+
logger.warning(f"Field description of {attr.path} is not a static string")
39+
description = str(description)
40+
41+
attr.docstring = griffe.Docstring(
42+
ast.literal_eval(description),
43+
parent=attr,
44+
parser=agent.docstring_parser,
45+
parser_options=agent.docstring_options,
46+
)

scripts/mkdocs_hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
def on_post_build(config: MkDocsConfig, **kwargs: Any) -> None: # noqa: ARG001
2323
"""Write `schema.json` to the site directory."""
2424
if TypeAdapter is None:
25-
logger.info("pydantic is not installed, skipping JSON schema generation")
25+
logger.info("Pydantic is not installed, skipping JSON schema generation")
2626
return
2727
adapter = TypeAdapter(PythonInputConfig)
2828
schema = adapter.json_schema()
2929
schema["$schema"] = "https://json-schema.org/draft-07/schema"
3030
with open(join(config.site_dir, "schema.json"), "w") as file:
3131
json.dump(schema, file, indent=2)
32-
print(f"Dumped JSON schema {file}")
32+
logger.debug("Generated JSON schema")

0 commit comments

Comments
 (0)