AndreaCrotti / yasnippet-snippets

a collection of yasnippet snippets for many languages
GNU General Public License v3.0
1.17k stars 464 forks source link

Any snippet using python-args-to-docstring returns Wrong type argument: listp #505

Open quazgar opened 4 weeks ago

quazgar commented 4 weeks ago

Maybe issue #1200 from yasnippet rather belongs here? Possibly introduced by #465,

Expanding e.g. function_docstring and tabbing the cursor to args, shows error Wrong type argument: listp [...]

I can reproduce this with plain Emacs:

def fd(foo):
    """
    Wrong type argument: listp, "foo"
    """

def fdn(bar):
    """

    Wrong type argument: listp, "bar"
    """ 
quazgar commented 1 week ago

Simple test (after loading the definitions python-args-to-docstring and python-split-args in .yas-setup.el):

(let ((yas-text "foo"))
  (python-args-to-docstring))

Output:

Debugger entered--Lisp error: (wrong-type-argument listp "foo")
  nth(0 "foo")
  (length (nth 0 x))
  (closure (t) (x) (length (nth 0 x)))("foo")
  mapcar((closure (t) (x) (length (nth 0 x))) ("foo" "bar"))
  (apply 'max (mapcar #'(lambda (x) (length (nth 0 x))) args))
  (if args (apply 'max (mapcar #'(lambda (x) (length (nth 0 x))) args)) 0)
  (let* ((indent (concat "\n" (make-string (current-column) 32))) (args (python-split-args yas-text)) (max-len (if args (apply 'max (mapcar #'(lambda ... ...) args)) 0)) (formatted-args (mapconcat #'(lambda (x) (concat (nth 0 x) (make-string ... 32) " -- " (if ... ...))) args indent))) (if (string= formatted-args "") nil (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent)))
  python-args-to-docstring()
  (let* ((yas-text "foo: str, bar: int = 3")) (python-args-to-docstring))
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  command-execute(eval-last-sexp)
quazgar commented 1 week ago

As a quick bugfix, this snippet works:

(defun python-split-args (arg-string)
  "Split a python argument string ARG-STRING into a list ((name), ...) of argument names."
  (mapcar 'list
          (mapcar (lambda (x)
            (when (string-match python-split-arg-arg-regex x)
              (match-string-no-properties 1 x)))
          (split-string arg-string python-split-arg-separator t))))

It does not keep the default values or types, though.