项目作者: douglasdavis

项目描述 :
Insert NumPy style docstrings in Python functions.
高级语言: Emacs Lisp
项目地址: git://github.com/douglasdavis/numpydoc.el.git
创建时间: 2021-02-11T18:21:11Z
项目社区:https://github.com/douglasdavis/numpydoc.el

开源协议:GNU General Public License v3.0

下载


numpydoc.el

MELPA
CI

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.

Setup

Pick your favorite method of Emacs Lisp package setup:

  1. ;; use-package with :ensure t to intall from MELPA.
  2. (use-package numpydoc
  3. :ensure t)
  4. ;; use the straight.el package manager.
  5. (straight-use-package 'numpydoc)
  6. ;; clone the git respository and require
  7. (add-to-list 'load-path "/path/to/numpydoc.el")
  8. (require 'numpydoc)
  9. ;; or point use-package to the local clone
  10. (use-package numpydoc
  11. :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:

  1. ;; with use-package
  2. (use-package numpydoc
  3. :ensure t
  4. :bind (:map python-mode-map
  5. ("C-c C-n" . numpydoc-generate)))
  6. ;; without
  7. (add-to-list 'load-path "/path/to/numpydoc.el")
  8. (require 'numpydoc)
  9. (define-key python-mode-map (kbd "C-c C-n") #'numpydoc-generate)

Customization

View customizations without leaving Emacs via M-x customize-group
RET numpydoc


numpydoc-insertion-style


The method used to insert components of the docstring (default is
‘prompt).

  • ‘prompt will trigger a request for each description
    in the minibuffer.

  • ‘yas (requires yasnippet to be
    installed) will generate a template and call
    yas-expand-snippet, providing an insertion method
    familiar to yasnippet users.

  • nil will disable any interactive insertion (template
    text will be inserted).



numpydoc-quote-char


Quote character to use (the default is a double quote,
?\”, used throughout the numpydoc docstring guide and
the black formatting tool).

numpydoc-insert-examples-block


If t (the default) an Examples block will be added to
the docstring.

numpydoc-insert-parameter-types


If t (the default) type hints from the function
signature will be used to add a type to each argument in the
Parameters block of the docstring.

numpydoc-insert-raises-block


If t (the default) a Raises bock will be added to the
docstring if exceptions are detected in the function body.

numpydoc-insert-return-without-typehint


If t a Returns block will be inserted in the absence of
a return type hint.

numpydoc-template-short


Template text that will be used as the short description if
numpydoc-insertion-style is nil.

numpydoc-template-long


Template text that will be used as the long description if
numpydoc-insertion-style is nil.

numpydoc-template-arg-desc


Template text that will be used for each function argument
description if numpydoc-insertion-style is
nil.

numpydoc-template-type-desc


Template text that will be used for each function argument type
description if numpydoc-insertion-style is
nil.

numpydoc-ignored-params


All function parameters with names listed here will be ignored
when generating a docstring.

numpydoc-auto-fill-paragraphs


If t text that is inserted in a prompt will be
automatically paragraph-filled.

Examples

M-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:

  1. def plot_histogram(
  2. x: np.ndarray,
  3. bins: int = 10,
  4. range: tuple[float, float] | None = None,
  5. weights: np.ndarray | None = None,
  6. flow: bool = False,
  7. ax: plt.Axes | None = None,
  8. ) -> tuple[plt.Figure, plt.Axes]:
  9. if weights is not None:
  10. if weights.shape != np.shape:
  11. raise ValueError("x and weights must have same shape.")
  12. pass

After M-x numpydoc-generate:

  1. def plot_histogram(
  2. x: np.ndarray,
  3. bins: int = 10,
  4. range: tuple[float, float] | None = None,
  5. weights: np.ndarray | None = None,
  6. flow: bool = False,
  7. ax: plt.Axes | None = None,
  8. ) -> tuple[plt.Figure, plt.Axes]:
  9. """FIXME: Short description.
  10. FIXME: Long description.
  11. Parameters
  12. ----------
  13. x : np.ndarray
  14. FIXME: Add docs.
  15. bins : int
  16. FIXME: Add docs.
  17. range : tuple[float, float], optional
  18. FIXME: Add docs.
  19. weights : np.ndarray, optional
  20. FIXME: Add docs.
  21. flow : bool
  22. FIXME: Add docs.
  23. ax : plt.Axes | None
  24. FIXME: Add docs.
  25. Returns
  26. -------
  27. tuple[plt.Figure, plt.Axes]
  28. FIXME: Add docs.
  29. Raises
  30. ------
  31. ValueError
  32. FIXME: Add docs.
  33. Examples
  34. --------
  35. FIXME: Add docs.
  36. """
  37. if weights is not None:
  38. if weights.shape != np.shape:
  39. raise ValueError("x and weights must have same shape.")
  40. pass

Similar packages

  • sphinx-doc.el: Inserts
    sphinx-compatible docstrings (does not offer customizations or
    automatically formatted insertions from the minibuffer or yasnippet).
  • docstr: Docstring insertion
    support for any programming language, including NumPy style Python
    (it has a programmable interface but requires a bit more setup to
    get the utility provided numpydoc.el).