Skip to content

Commit

Permalink
improved docstring adder
Browse files Browse the repository at this point in the history
  • Loading branch information
mcocdawc committed Nov 26, 2024
1 parent fc3e5c0 commit beda895
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/quemb/shared/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Callable, TypeVar

Function = TypeVar("Function", bound=Callable)


# Note that we have once Callable and once Function.
# This is **intentional**.
# The inner function `update_doc` takes a function
# and returns a function **with** the exact same signature.
def add_docstring(doc: str) -> Callable[[Function], Function]:
"""Add a docstring to a function.
Is useful for programmatically generating docstrings.
Parameters
----------
doc: str
A docstring.
Example
----------
>>> @add_docstring("Returns 'asdf'")
>>> def f():
>>> return 'asdf'
is equivalent to
>>> def f():
>>> "Returns 'asdf'"
>>> return 'asdf'
"""

def update_doc(f: Function) -> Function:
f.__doc__ = doc
return f

return update_doc

0 comments on commit beda895

Please sign in to comment.