|
7 | 7 | from griffe.docstrings import dataclasses as ds
|
8 | 8 | from griffe import dataclasses as dc
|
9 | 9 | from plum import dispatch
|
10 |
| -from typing import Union |
| 10 | +from typing import Type, Union |
11 | 11 |
|
12 | 12 | from ._pydantic_compat import BaseModel # for previewing
|
13 | 13 |
|
@@ -44,7 +44,7 @@ class DocstringSectionKindPatched(Enum):
|
44 | 44 |
|
45 | 45 |
|
46 | 46 | class _DocstringSectionPatched(ds.DocstringSection):
|
47 |
| - _registry: "dict[Enum, _DocstringSectionPatched]" = {} |
| 47 | + _registry: "dict[str, Type[_DocstringSectionPatched]]" = {} |
48 | 48 |
|
49 | 49 | def __init__(self, value: str, title: "str | None" = None):
|
50 | 50 | super().__init__(title)
|
@@ -102,18 +102,31 @@ def transform(cls, el: ds.DocstringSection) -> list[ds.DocstringSection]:
|
102 | 102 | class represents a section like See Also, etc..
|
103 | 103 | """
|
104 | 104 |
|
105 |
| - if not isinstance(el, ds.DocstringSectionText): |
| 105 | + if not isinstance(el, (ds.DocstringSectionText, ds.DocstringSectionAdmonition)): |
106 | 106 | return [el]
|
107 | 107 |
|
108 |
| - splits = cls.split_sections(el.value) |
109 | 108 | results = []
|
110 |
| - for title, body in splits: |
111 |
| - sub_cls = cls._registry.get(title.lower(), ds.DocstringSectionText) |
112 | 109 |
|
113 |
| - # note that griffe currently doesn't store the title anywhere, |
114 |
| - # but we add the exact title here, so we can be flexible about the |
115 |
| - # sections we parse (e.g. Note instead of Notes) in the future. |
116 |
| - results.append(sub_cls(body, title)) |
| 110 | + # griffe < 0.39 w/ numpydoc uses DocstringSectionText for unhandled section |
| 111 | + # but later versions always use Admonitions. Note it may still use Text |
| 112 | + # for areas of docstrings not associated with particular sections (e.g. freeform |
| 113 | + # text betwen a parameters section and the next section). |
| 114 | + if isinstance(el, ds.DocstringSectionText): |
| 115 | + # handle griffe < 0.39 case |
| 116 | + splits = cls.split_sections(el.value) |
| 117 | + for title, body in splits: |
| 118 | + sub_cls = cls._registry.get(title.lower(), ds.DocstringSectionText) |
| 119 | + |
| 120 | + # note that griffe currently doesn't store the title anywhere, |
| 121 | + # but we add the exact title here, so we can be flexible about the |
| 122 | + # sections we parse (e.g. Note instead of Notes) in the future. |
| 123 | + results.append(sub_cls(body, title)) |
| 124 | + elif isinstance(el, ds.DocstringSectionAdmonition): |
| 125 | + sub_cls = cls._registry.get(el.title.lower(), None) |
| 126 | + if sub_cls: |
| 127 | + results.append(sub_cls(el.value.contents, el.title)) |
| 128 | + else: |
| 129 | + results.append(el) |
117 | 130 |
|
118 | 131 | return results or [el]
|
119 | 132 |
|
|
0 commit comments