Insert NumPy style docstrings in Python functions.
An Emacs Lisp package to automatically insert NumPy style
docstrings in
Python function definitions.
Calling numpydoc-generate
parses the function at point (the cursor
can be anywhere in the function body). The parsing detects argument
names, type hints, exceptions, and the return type hint. This
information is used to generate a docstring.
The default behavior is to prompt the user (in the minibuffer) for a
short & long description of the function, a description for each
function argument, a description for each possible exception, and a
description for the return. It’s also possible to either disable the
minibuffer prompt or use
yasnippet insertion. See
customization for more information. You’ll also find
a few examples below. See the
NEWS
file to see recent changes.
Pick your favorite method of Emacs Lisp package setup:
;; use-package with :ensure t to intall from MELPA.
(use-package numpydoc
:ensure t)
;; use the straight.el package manager.
(straight-use-package 'numpydoc)
;; clone the git respository and require
(add-to-list 'load-path "/path/to/numpydoc.el")
(require 'numpydoc)
;; or point use-package to the local clone
(use-package numpydoc
:load-path "/path/to/numpydoc.el")
The C-c C-n
binding is vacant (not used in python.el
, as of
writing this), so you may want to give yourself a convenient shortcut:
;; with use-package
(use-package numpydoc
:ensure t
:bind (:map python-mode-map
("C-c C-n" . numpydoc-generate)))
;; without
(add-to-list 'load-path "/path/to/numpydoc.el")
(require 'numpydoc)
(define-key python-mode-map (kbd "C-c C-n") #'numpydoc-generate)
View customizations without leaving Emacs via M-x customize-group
RET numpydoc
‘prompt
).‘prompt
will trigger a request for each description‘yas
(requires yasnippet
to beyas-expand-snippet
, providing an insertion methodyasnippet
users.nil
will disable any interactive insertion (template?\”
, used throughout the numpydoc docstring guide andt
(the default) an Examples block will be added tot
(the default) type hints from the functiont
(the default) a Raises bock will be added to thet
a Returns block will be inserted in the absence ofnumpydoc-insertion-style
is nil
.numpydoc-insertion-style
is nil
.numpydoc-insertion-style
isnil
.numpydoc-insertion-style
isnil
.t
text that is inserted in a prompt will beM-x numpydoc-generate with the default configuration,numpydoc-insertion-style
set to 'prompt
(notice how long text is
automatically paragraph-filled):
Using yasnippet
(numpydoc-insertion-style
set to 'yas
):
With numpydoc-insertion-style
set to nil
; before:
def plot_histogram(
x: np.ndarray,
bins: int = 10,
range: tuple[float, float] | None = None,
weights: np.ndarray | None = None,
flow: bool = False,
ax: plt.Axes | None = None,
) -> tuple[plt.Figure, plt.Axes]:
if weights is not None:
if weights.shape != np.shape:
raise ValueError("x and weights must have same shape.")
pass
After M-x numpydoc-generate:
def plot_histogram(
x: np.ndarray,
bins: int = 10,
range: tuple[float, float] | None = None,
weights: np.ndarray | None = None,
flow: bool = False,
ax: plt.Axes | None = None,
) -> tuple[plt.Figure, plt.Axes]:
"""FIXME: Short description.
FIXME: Long description.
Parameters
----------
x : np.ndarray
FIXME: Add docs.
bins : int
FIXME: Add docs.
range : tuple[float, float], optional
FIXME: Add docs.
weights : np.ndarray, optional
FIXME: Add docs.
flow : bool
FIXME: Add docs.
ax : plt.Axes | None
FIXME: Add docs.
Returns
-------
tuple[plt.Figure, plt.Axes]
FIXME: Add docs.
Raises
------
ValueError
FIXME: Add docs.
Examples
--------
FIXME: Add docs.
"""
if weights is not None:
if weights.shape != np.shape:
raise ValueError("x and weights must have same shape.")
pass
numpydoc.el
).