Skip to content

Instantly share code, notes, and snippets.

@rksm
Created January 6, 2024 04:28
Show Gist options
  • Save rksm/7cdd5ddcfaf23d52ba3e7aa35e6adc4b to your computer and use it in GitHub Desktop.
Save rksm/7cdd5ddcfaf23d52ba3e7aa35e6adc4b to your computer and use it in GitHub Desktop.
Fix for `M-x elpy-doc`
(use-package elpy
:ensure
:demand flycheck
:init (elpy-enable)
:config
(advice-add 'elpy-doc :around #'rk/elpy-doc-fallback-to-help))
;; help from python, see https://github.com/jorgenschaefer/elpy/pull/1509 and
;; https://github.com/jorgenschaefer/elpy/issues/1507
(defun rk/elpy-doc-get-docstring-from-shell ()
"Return the docstring for the symbol at point using the shell."
(when (elpy-shell--check-if-shell-available)
(let* ((process (python-shell-get-process))
(symbol (elpy-doc--symbol-at-point))
(docstring
(when symbol
(python-shell-send-string-no-output
(format "import pydoc, io\noutput = io.StringIO()\nh = pydoc.Helper(output=output)\nh.help(%s)\nprint(output.getvalue())" symbol)
process))))
(unless (or (not docstring)
(zerop (length docstring))
(string-match "Traceback (most recent call last):"
docstring)
(string-match "No Python documentation found for"
docstring))
;; Remove potential warnings in the output
(substring docstring (string-match "^Help on " docstring))))))
(defun rk/elpy-doc-fallback-to-help (orig-fun &rest args)
(unwind-protect
(progn
(funcall orig-fun))
(when-let ((doc (rk/elpy-doc-get-docstring-from-shell)))
(elpy-doc--show doc))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment