|
29 | 29 | # stdlib
|
30 | 30 | import builtins
|
31 | 31 | from textwrap import dedent
|
32 |
| -from typing import Any, Callable, Optional, Sequence, Type, TypeVar, Union |
| 32 | +from typing import Any, Callable, Dict, Optional, Sequence, Type, TypeVar, Union |
| 33 | + |
| 34 | +# this package |
| 35 | +from domdf_python_tools.typing import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType |
33 | 36 |
|
34 | 37 | __all__ = [
|
35 | 38 | "F",
|
|
40 | 43 | "is_documented_by",
|
41 | 44 | "append_docstring_from",
|
42 | 45 | "sphinxify_docstring",
|
| 46 | + "prettify_docstrings", |
43 | 47 | ]
|
44 | 48 |
|
45 | 49 | F = TypeVar('F', bound=Callable[..., Any])
|
@@ -203,3 +207,150 @@ def wrapper(target: F) -> F:
|
203 | 207 | return target
|
204 | 208 |
|
205 | 209 | return wrapper
|
| 210 | + |
| 211 | + |
| 212 | +# Check against object |
| 213 | +base_new_docstrings = { |
| 214 | + "__delattr__": "Implement :func:`delattr(self, name) <delattr>`.", |
| 215 | + "__dir__": "Default :func:`dir` implementation.", |
| 216 | + "__eq__": "Return ``self == other``.", # __format__ |
| 217 | + "__getattribute__": "Return :func:`getattr(self, name) <getattr>`.", |
| 218 | + "__ge__": "Return ``self >= other``.", |
| 219 | + "__gt__": "Return ``self > other``.", |
| 220 | + "__hash__": "Return :func:`hash(self) <hash>`.", |
| 221 | + # __init_subclass__ |
| 222 | + # __init__ # not usually shown in sphinx |
| 223 | + "__lt__": "Return ``self < other``.", |
| 224 | + "__le__": "Return ``self <= other``.", # __new__ |
| 225 | + "__ne__": "Return ``self != other``.", |
| 226 | + # __reduce_ex__ |
| 227 | + # __reduce__ |
| 228 | + # __repr__ is defined within the function |
| 229 | + "__setattr__": "Implement :func:`setattr(self, name) <setattr>`.", |
| 230 | + "__sizeof__": "Returns the size of the object in memory, in bytes.", |
| 231 | + "__str__": "Return :func:`str(self) <str>`.", # __subclasshook__ |
| 232 | + } |
| 233 | + |
| 234 | +# Check against dict |
| 235 | +container_docstrings = { |
| 236 | + "__contains__": "Return ``key in self``.", |
| 237 | + "__getitem__": "Return ``self[key]``.", |
| 238 | + "__setitem__": "Set ``self[key]`` to ``value``.", |
| 239 | + "__delitem__": "Delete ``self[key]````.", |
| 240 | + } |
| 241 | + |
| 242 | +# Check against int |
| 243 | +operator_docstrings = { |
| 244 | + "__and__": "Return ``self & value``.", |
| 245 | + "__add__": "Return ``self + value``.", |
| 246 | + "__abs__": "Return :func:`abs(self) <abs>`.", |
| 247 | + "__divmod__": "Return :func:`divmod(self, value) <divmod>`.", |
| 248 | + "__floordiv__": "Return ``self // value``.", |
| 249 | + "__invert__": "Return ``~ self``.", |
| 250 | + "__lshift__": "Return ``self << value``.", |
| 251 | + "__mod__": "Return ``self % value``.", |
| 252 | + "__mul__": "Return ``self * value``.", |
| 253 | + "__neg__": "Return ``- self``.", |
| 254 | + "__or__": "Return ``self | value``.", |
| 255 | + "__pos__": "Return ``+ self``.", |
| 256 | + "__pow__": "Return :func:`pow(self, value, mod) <pow>`.", |
| 257 | + "__radd__": "Return ``value + self``.", |
| 258 | + "__rand__": "Return ``value & self``.", |
| 259 | + "__rdivmod__": "Return :func:`divmod(value, self) <divmod>`.", |
| 260 | + "__rfloordiv__": "Return ``value // self``.", |
| 261 | + "__rlshift__": "Return ``value << self``.", |
| 262 | + "__rmod__": "Return ``value % self``.", |
| 263 | + "__rmul__": "Return ``value * self``.", |
| 264 | + "__ror__": "Return ``value | self``.", |
| 265 | + "__rpow__": "Return :func:`pow(value, self, mod) <pow>`.", |
| 266 | + "__rrshift__": "Return ``self >> value``.", |
| 267 | + "__rshift__": "Return ``self >> value``.", |
| 268 | + "__rsub__": "Return ``value - self``.", |
| 269 | + "__rtruediv__": "Return ``value / self``.", |
| 270 | + "__rxor__": "Return ``value ^ self``.", |
| 271 | + "__sub__": "Return ``value - self``.", |
| 272 | + "__truediv__": "Return ``self / value``.", |
| 273 | + "__xor__": "Return ``self ^ value``.", |
| 274 | + } |
| 275 | + |
| 276 | +# Check against int |
| 277 | +base_int_docstrings = { |
| 278 | + # "__bool__": "Return ``self != 0`.", # TODO |
| 279 | + # __ceil__ |
| 280 | + "__float__": "Return :func:`float(self) <float>`.", # __floor__ |
| 281 | + "__int__": "Return :func:`int(self) <int>`.", # __round__ |
| 282 | + } |
| 283 | + |
| 284 | +new_return_types = { |
| 285 | + "__eq__": bool, |
| 286 | + "__ge__": bool, |
| 287 | + "__gt__": bool, |
| 288 | + "__lt__": bool, |
| 289 | + "__le__": bool, |
| 290 | + "__ne__": bool, |
| 291 | + "__repr__": str, |
| 292 | + "__str__": str, |
| 293 | + "__int__": int, |
| 294 | + "__float__": float, |
| 295 | + "__bool__": bool, |
| 296 | + } |
| 297 | + |
| 298 | + |
| 299 | +def _do_prettify(obj: Type, base: Type, new_docstrings: Dict[str, str]): |
| 300 | + """ |
| 301 | + Perform the actual prettification for :func`~.prettify_docstrings`. |
| 302 | +
|
| 303 | + :param obj: |
| 304 | + :param base: |
| 305 | + :param new_docstrings: |
| 306 | +
|
| 307 | + :return: |
| 308 | + """ |
| 309 | + |
| 310 | + for attr_name in new_docstrings: |
| 311 | + |
| 312 | + if hasattr(obj, attr_name): |
| 313 | + attribute = getattr(obj, attr_name) |
| 314 | + |
| 315 | + if not isinstance(attribute, (WrapperDescriptorType, MethodDescriptorType, MethodWrapperType)): |
| 316 | + |
| 317 | + doc: Optional[str] = getattr(obj, attr_name).__doc__ |
| 318 | + |
| 319 | + if doc in {None, getattr(base, attr_name).__doc__}: |
| 320 | + getattr(obj, attr_name).__doc__ = new_docstrings[attr_name] |
| 321 | + |
| 322 | + |
| 323 | +def prettify_docstrings(obj: Type) -> Type: |
| 324 | + """ |
| 325 | + Prettify the default :class:`object` docstrings for use in Sphinx documentation. |
| 326 | +
|
| 327 | + :param obj: The object to prettify the docstrings for. |
| 328 | +
|
| 329 | + :return: The object |
| 330 | +
|
| 331 | + Default :func:`dir` implementation. |
| 332 | + """ |
| 333 | + |
| 334 | + new_docstrings = { |
| 335 | + **base_new_docstrings, |
| 336 | + "__repr__": f"Return a string representation of the :class:`~{obj.__module__}.{obj.__name__}`.", |
| 337 | + } |
| 338 | + |
| 339 | + _do_prettify(obj, object, new_docstrings) |
| 340 | + _do_prettify(obj, dict, container_docstrings) |
| 341 | + _do_prettify(obj, int, operator_docstrings) |
| 342 | + _do_prettify(obj, int, base_int_docstrings) |
| 343 | + |
| 344 | + for attribute in new_return_types: |
| 345 | + if hasattr(obj, attribute): |
| 346 | + annotations: Dict = getattr(getattr(obj, attribute), "__annotations__", {}) |
| 347 | + |
| 348 | + if "return" not in annotations or annotations["return"] is Any: |
| 349 | + annotations["return"] = new_return_types[attribute] |
| 350 | + |
| 351 | + try: |
| 352 | + getattr(obj, attribute).__annotations__ = annotations |
| 353 | + except AttributeError: # pragma: no cover |
| 354 | + pass |
| 355 | + |
| 356 | + return obj |
0 commit comments