Skip to content

Instantly share code, notes, and snippets.

@carlomazzaferro
Created November 22, 2018 00:21
Show Gist options
  • Save carlomazzaferro/884c902055325ee3b4adbfee2dc99c4b to your computer and use it in GitHub Desktop.
Save carlomazzaferro/884c902055325ee3b4adbfee2dc99c4b to your computer and use it in GitHub Desktop.
Inheriting docstrings from a top level function with decorators
"""
This is particularly useful when using sphinx autodoc, when you have functions that share the same functionality
(say a cli command that wraps a function) and you don't want to rewrite the docstrings for each of them.
"""
def register_docstrings(parent=None):
def doc_decorator(func):
func.__doc__ = parent.__doc__ + func.__doc__
return func
return doc_decorator
# Usage
def parent(a, b):
"""
This is the parent function
"""
@register_docstring(parent=parent)
def child(a, b):
""""
This is the child function
""""
# result: child.__doc__ = This is the parent functionThis is the child function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment