-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdoctools.py
More file actions
383 lines (291 loc) · 11 KB
/
Copy pathdoctools.py
File metadata and controls
383 lines (291 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# !/usr/bin/env python
#
# doctools.py
"""
Utilities for documenting functions, classes and methods.
"""
#
# Copyright © 2020 Dominic Davis-Foster <dominic@davis-foster.co.uk>
# Based on https://softwareengineering.stackexchange.com/a/386758
# Copyright © amon (https://softwareengineering.stackexchange.com/users/60357/amon)
# Licensed under CC BY-SA 4.0
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# stdlib
import builtins
from inspect import cleandoc
from types import MethodType
from typing import Any, Callable, Dict, Optional, Sequence, Type, TypeVar, Union
# this package
from domdf_python_tools.compat import PYPY
from domdf_python_tools.typing import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType
__all__ = [
'F',
"deindent_string",
"document_object_from_another",
"append_doctring_from_another",
"make_sphinx_links",
"is_documented_by",
"append_docstring_from",
"sphinxify_docstring",
"prettify_docstrings",
]
F = TypeVar('F', bound=Callable[..., Any])
def deindent_string(string: Optional[str]) -> str:
"""
Removes all indentation from the given string.
:param string: The string to deindent
:return: The string without indentation
"""
if not string:
# Short circuit if empty string or None
return ''
split_string = string.split('\n')
deindented_string = [line.lstrip("\t ") for line in split_string]
return '\n'.join(deindented_string)
# Functions that do the work
def document_object_from_another(target: Union[Type, Callable], original: Union[Type, Callable]):
"""
Sets the docstring of the ``target`` function to that of the ``original`` function.
This may be useful for subclasses or wrappers that use the same arguments.
:param target: The object to set the docstring for
:param original: The object to copy the docstring from
"""
target.__doc__ = original.__doc__
def append_doctring_from_another(target: Union[Type, Callable], original: Union[Type, Callable]):
"""
Sets the docstring of the ``target`` function to that of the ``original`` function.
This may be useful for subclasses or wrappers that use the same arguments.
Any indentation in either docstring is removed to
ensure consistent indentation between the two docstrings.
Bear this in mind if additional indentation is used in the docstring.
:param target: The object to append the docstring to
:param original: The object to copy the docstring from
"""
# this package
from domdf_python_tools.stringlist import StringList
target_doc = target.__doc__
original_doc = original.__doc__
if isinstance(original_doc, str) and isinstance(target_doc, str):
docstring = StringList(cleandoc(target_doc))
docstring.blankline(ensure_single=True)
docstring.append(cleandoc(original_doc))
docstring.blankline(ensure_single=True)
target.__doc__ = str(docstring)
elif not isinstance(target_doc, str) and isinstance(original_doc, str):
docstring = StringList(cleandoc(original_doc))
docstring.blankline(ensure_single=True)
target.__doc__ = str(docstring)
def make_sphinx_links(input_string: str, builtins_list: Optional[Sequence[str]] = None) -> str:
r"""
Make proper sphinx links out of double-backticked strings in docstring.
i.e.
.. code-block:: rest
``str``
becomes
.. code-block:: rest
:class:`str`
Make sure to have ``'python': ('https://docs.python.org/3/', None),`` in the
``intersphinx_mapping`` dict of your ``conf.py`` file for Sphinx.
:param input_string: The string to process.
:param builtins_list: A list of builtins to make links for.
:default builtins_list: dir(:py:obj:`builtins`)
:return: Processed string with links.
""" # noqa SXL001
if builtins_list is None:
builtins_list = dir(builtins)
working_string = f"{input_string}"
for builtin in builtins_list:
if builtin.startswith("__"):
continue
if builtin in {"None", "False", "None"}:
working_string = working_string.replace(f"``{builtin}``", f":py:obj:`{builtin}`")
else:
working_string = working_string.replace(f"``{builtin}``", f":class:`{builtin}`")
return working_string
# Decorators that call the above functions
def is_documented_by(original: F) -> Callable:
"""
Decorator to set the docstring of the ``target`` function to that of the ``original`` function.
This may be useful for subclasses or wrappers that use the same arguments.
"""
def wrapper(target: F) -> F:
document_object_from_another(target, original)
return target
return wrapper
def append_docstring_from(original: F) -> Callable:
"""
Decorator to appends the docstring from the ``original`` function to the ``target`` function.
This may be useful for subclasses or wrappers that use the same arguments.
Any indentation in either docstring is removed to
ensure consistent indentation between the two docstrings.
Bear this in mind if additional indentation is used in the docstring.
"""
def wrapper(target: F) -> F:
append_doctring_from_another(target, original)
return target
return wrapper
def sphinxify_docstring() -> Callable[[F], F]:
r"""
Decorator to make proper sphinx links out of double-backticked strings in the docstring.
i.e.
.. code-block:: rest
``str``
becomes
.. code-block:: rest
:class:`str`
Make sure to have ``'python': ('https://docs.python.org/3/', None),`` in the
``intersphinx_mapping`` dict of your ``conf.py`` file for Sphinx.
""" # noqa SXL001
def wrapper(target: F) -> F:
target_doc = target.__doc__
if target_doc:
target.__doc__ = make_sphinx_links(target_doc)
return target
return wrapper
# Check against object
base_new_docstrings = {
"__delattr__": "Implement :func:`delattr(self, name) <delattr>`.",
"__dir__": "Default :func:`dir` implementation.",
"__eq__": "Return ``self == other``.", # __format__
"__getattribute__": "Return :func:`getattr(self, name) <getattr>`.",
"__ge__": "Return ``self >= other``.",
"__gt__": "Return ``self > other``.",
"__hash__": "Return :func:`hash(self) <hash>`.",
# __init_subclass__
# __init__ # not usually shown in sphinx
"__lt__": "Return ``self < other``.",
"__le__": "Return ``self <= other``.", # __new__
"__ne__": "Return ``self != other``.",
# __reduce_ex__
# __reduce__
# __repr__ is defined within the function
"__setattr__": "Implement :func:`setattr(self, name) <setattr>`.",
"__sizeof__": "Returns the size of the object in memory, in bytes.",
"__str__": "Return :class:`str(self) <str>`.", # __subclasshook__
}
# Check against dict
container_docstrings = {
"__contains__": "Return ``key in self``.",
"__getitem__": "Return ``self[key]``.",
"__setitem__": "Set ``self[key]`` to ``value``.",
"__delitem__": "Delete ``self[key]``.",
}
# Check against int
operator_docstrings = {
"__and__": "Return ``self & value``.",
"__add__": "Return ``self + value``.",
"__abs__": "Return :func:`abs(self) <abs>`.",
"__divmod__": "Return :func:`divmod(self, value) <divmod>`.",
"__floordiv__": "Return ``self // value``.",
"__invert__": "Return ``~ self``.",
"__lshift__": "Return ``self << value``.",
"__mod__": "Return ``self % value``.",
"__mul__": "Return ``self * value``.",
"__neg__": "Return ``- self``.",
"__or__": "Return ``self | value``.",
"__pos__": "Return ``+ self``.",
"__pow__": "Return :func:`pow(self, value, mod) <pow>`.",
"__radd__": "Return ``value + self``.",
"__rand__": "Return ``value & self``.",
"__rdivmod__": "Return :func:`divmod(value, self) <divmod>`.",
"__rfloordiv__": "Return ``value // self``.",
"__rlshift__": "Return ``value << self``.",
"__rmod__": "Return ``value % self``.",
"__rmul__": "Return ``value * self``.",
"__ror__": "Return ``value | self``.",
"__rpow__": "Return :func:`pow(value, self, mod) <pow>`.",
"__rrshift__": "Return ``self >> value``.",
"__rshift__": "Return ``self >> value``.",
"__rsub__": "Return ``value - self``.",
"__rtruediv__": "Return ``value / self``.",
"__rxor__": "Return ``value ^ self``.",
"__sub__": "Return ``value - self``.",
"__truediv__": "Return ``self / value``.",
"__xor__": "Return ``self ^ value``.",
}
# Check against int
base_int_docstrings = {
# "__bool__": "Return ``self != 0`.", # TODO
# __ceil__
"__float__": "Return :class:`float(self) <float>`.", # __floor__
"__int__": "Return :class:`int(self) <int>`.", # __round__
}
new_return_types = {
"__eq__": bool,
"__ge__": bool,
"__gt__": bool,
"__lt__": bool,
"__le__": bool,
"__ne__": bool,
"__repr__": str,
"__str__": str,
"__int__": int,
"__float__": float,
"__bool__": bool,
}
def _do_prettify(obj: Type, base: Type, new_docstrings: Dict[str, str]):
"""
Perform the actual prettifying for :func`~.prettify_docstrings`.
.. versionadded:: 0.8.0
:param obj:
:param base:
:param new_docstrings:
"""
for attr_name in new_docstrings:
if not hasattr(obj, attr_name):
continue
attribute = getattr(obj, attr_name)
if not PYPY and isinstance(
attribute,
(WrapperDescriptorType, MethodDescriptorType, MethodWrapperType, MethodType),
):
continue
elif PYPY and isinstance(attribute, MethodType):
continue # pragma: no cover (!PyPy)
if attribute is None:
continue
base_docstring: Optional[str] = None
if hasattr(base, attr_name):
base_docstring = getattr(base, attr_name).__doc__
doc: Optional[str] = attribute.__doc__
if doc in {None, base_docstring}:
attribute.__doc__ = new_docstrings[attr_name]
def prettify_docstrings(obj: Type) -> Type:
"""
Decorator to prettify the default :class:`object` docstrings for use in Sphinx documentation.
.. versionadded:: 0.8.0
:param obj: The object to prettify the method docstrings for.
"""
repr_docstring = f"Return a string representation of the :class:`~{obj.__module__}.{obj.__name__}`."
new_docstrings = {**base_new_docstrings, "__repr__": repr_docstring}
_do_prettify(obj, object, new_docstrings)
_do_prettify(obj, dict, container_docstrings)
_do_prettify(obj, int, operator_docstrings)
_do_prettify(obj, int, base_int_docstrings)
for attribute in new_return_types:
if hasattr(obj, attribute):
annotations: Dict = getattr(getattr(obj, attribute), "__annotations__", {})
if "return" not in annotations or annotations["return"] is Any:
annotations["return"] = new_return_types[attribute]
try:
getattr(obj, attribute).__annotations__ = annotations
except AttributeError: # pragma: no cover
pass
if issubclass(obj, tuple) and obj.__repr__.__doc__ == "Return a nicely formatted representation string":
obj.__repr__.__doc__ = repr_docstring
return obj