Skip to content

Commit 77e2893

Browse files
authoredAug 11, 2023
Emit a meaningful error if function signature parsing fails (#18)
1 parent 5cb26db commit 77e2893

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed
 

‎NEWS.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# numpydoc.el NEWS -- history of user visible changes
22

3+
## Unreleased
4+
5+
### Changes
6+
7+
- Emit a useful warning if function signature parsing fails
8+
([#18](https://github.com/douglasdavis/numpydoc.el/pull/18)).
9+
310
## 0.9 (July 24, 2023)
411

512
### Changes

‎numpydoc.el

+32-29
Original file line numberDiff line numberDiff line change
@@ -256,35 +256,38 @@ This function assumes the cursor to be in the function body."
256256
(defun numpydoc--parse-def (buffer-substr)
257257
"Parse the BUFFER-SUBSTR; return instance of numpydoc--def."
258258
(save-excursion
259-
(let* ((fnsig buffer-substr)
260-
;; trimmed string of the function signature
261-
(trimmed (s-collapse-whitespace fnsig))
262-
;; split into parts (args and return type)
263-
(parts (s-split "->" trimmed))
264-
;; raw return
265-
(rawret (if (nth 1 parts)
266-
(s-trim (nth 1 parts))
267-
nil))
268-
;; save return type as a string (or nil)
269-
(rtype (when rawret
270-
(substring rawret 0 (1- (length rawret)))))
271-
;; raw signature without return type as a string
272-
(rawsig (cond (rtype (substring (s-trim (car parts)) 0 -1))
273-
(t (substring (s-trim (car parts)) 0 -2))))
274-
;; function args as strings
275-
(rawargs (-map #'s-trim
276-
(numpydoc--split-args
277-
(substring rawsig
278-
(1+ (string-match-p (regexp-quote "(")
279-
rawsig))))))
280-
;; function args as a list of structures (remove some special cases)
281-
(args (-remove (lambda (x)
282-
(-contains-p numpydoc-ignored-params
283-
(numpydoc--arg-name x)))
284-
(-map #'numpydoc--arg-str-to-struct rawargs)))
285-
;; look for exceptions in the function body
286-
(exceptions (numpydoc--find-exceptions)))
287-
(make-numpydoc--def :args args :rtype rtype :raises exceptions))))
259+
(condition-case nil
260+
(progn
261+
(let* ((fnsig buffer-substr)
262+
;; trimmed string of the function signature
263+
(trimmed (s-collapse-whitespace fnsig))
264+
;; split into parts (args and return type)
265+
(parts (s-split "->" trimmed))
266+
;; raw return
267+
(rawret (if (nth 1 parts)
268+
(s-trim (nth 1 parts))
269+
nil))
270+
;; save return type as a string (or nil)
271+
(rtype (when rawret
272+
(substring rawret 0 (1- (length rawret)))))
273+
;; raw signature without return type as a string
274+
(rawsig (cond (rtype (substring (s-trim (car parts)) 0 -1))
275+
(t (substring (s-trim (car parts)) 0 -2))))
276+
;; function args as strings
277+
(rawargs (-map #'s-trim
278+
(numpydoc--split-args
279+
(substring rawsig
280+
(1+ (string-match-p (regexp-quote "(")
281+
rawsig))))))
282+
;; function args as a list of structures (remove some special cases)
283+
(args (-remove (lambda (x)
284+
(-contains-p numpydoc-ignored-params
285+
(numpydoc--arg-name x)))
286+
(-map #'numpydoc--arg-str-to-struct rawargs)))
287+
;; look for exceptions in the function body
288+
(exceptions (numpydoc--find-exceptions)))
289+
(make-numpydoc--def :args args :rtype rtype :raises exceptions)))
290+
(error "Failed to parse function signature (bad Python syntax)."))))
288291

289292
(defun numpydoc--has-existing-docstring-p ()
290293
"Check for an existing docstring.

0 commit comments

Comments
 (0)
Please sign in to comment.