Skip to content

Commit 311807b

Browse files
committed
fix: Use already parsed docstring sections when dumping full data
Deprecate the docstring parameters passed to the JSON encoder. Discussion griffe-typingdoc#6: mkdocstrings/griffe-typingdoc#6
1 parent bc4fa4e commit 311807b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/griffe/dataclasses.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import inspect
10+
import warnings
1011
from collections import defaultdict
1112
from contextlib import suppress
1213
from pathlib import Path
@@ -134,24 +135,34 @@ def parse(
134135
"""
135136
return parse(self, parser or self.parser, **(options or self.parser_options))
136137

137-
def as_dict(self, *, full: bool = False, docstring_parser: Parser | None = None, **kwargs: Any) -> dict[str, Any]:
138+
def as_dict(
139+
self,
140+
*,
141+
full: bool = False,
142+
docstring_parser: Parser | None = None,
143+
**kwargs: Any, # noqa: ARG002
144+
) -> dict[str, Any]:
138145
"""Return this docstring's data as a dictionary.
139146
140147
Parameters:
141148
full: Whether to return full info, or just base info.
142-
docstring_parser: The docstring parser to parse the docstring with. By default, no parsing is done.
143-
**kwargs: Additional serialization or docstring parsing options.
149+
docstring_parser: Deprecated. The docstring parser to parse the docstring with. By default, no parsing is done.
150+
**kwargs: Additional serialization options.
144151
145152
Returns:
146153
A dictionary.
147154
"""
155+
# TODO: Remove at some point.
156+
if docstring_parser is not None:
157+
warnings.warn("Parameter `docstring_parser` is deprecated and has no effect.", stacklevel=1)
158+
148159
base: dict[str, Any] = {
149160
"value": self.value,
150161
"lineno": self.lineno,
151162
"endlineno": self.endlineno,
152163
}
153164
if full:
154-
base["parsed"] = self.parse(docstring_parser, **kwargs)
165+
base["parsed"] = self.parsed
155166
return base
156167

157168

src/griffe/encoders.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
import json
11+
import warnings
1112
from pathlib import Path, PosixPath, WindowsPath
1213
from typing import TYPE_CHECKING, Any, Callable
1314

@@ -82,14 +83,20 @@ def __init__(
8283
you don't need the full data as it can be infered again
8384
using the base data. If you want to feed a non-Python
8485
tool instead, dump the full data.
85-
docstring_parser: The docstring parser to use. By default, no parsing is done.
86-
docstring_options: Additional docstring parsing options.
86+
docstring_parser: Deprecated. The docstring parser to use. By default, no parsing is done.
87+
docstring_options: Deprecated. Additional docstring parsing options.
8788
**kwargs: See [`json.JSONEncoder`][].
8889
"""
8990
super().__init__(*args, **kwargs)
9091
self.full: bool = full
92+
93+
# TODO: Remove at some point.
9194
self.docstring_parser: Parser | None = docstring_parser
9295
self.docstring_options: dict[str, Any] = docstring_options or {}
96+
if docstring_parser is not None:
97+
warnings.warn("Parameter `docstring_parser` is deprecated and has no effect.", stacklevel=1)
98+
if docstring_options is not None:
99+
warnings.warn("Parameter `docstring_options` is deprecated and has no effect.", stacklevel=1)
93100

94101
def default(self, obj: Any) -> Any:
95102
"""Return a serializable representation of the given object.
@@ -101,7 +108,7 @@ def default(self, obj: Any) -> Any:
101108
A serializable representation.
102109
"""
103110
try:
104-
return obj.as_dict(full=self.full, docstring_parser=self.docstring_parser, **self.docstring_options)
111+
return obj.as_dict(full=self.full)
105112
except AttributeError:
106113
return _json_encoder_map.get(type(obj), super().default)(obj)
107114

0 commit comments

Comments
 (0)