Skip to content

Commit

Permalink
" | None" --> ", optional" (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasdavis authored Mar 4, 2022
1 parent cfec608 commit 1c0a2f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
9 changes: 7 additions & 2 deletions numpydoc.el
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ text, and below the Examples section."
(defun numpydoc--yas-p ()
(eq numpydoc-insertion-style 'yas))

(defun numpydoc--none-to-optional (type)
(replace-regexp-in-string (rx " | None" eos) ", optional" type t t))

(defun numpydoc--arg-str-to-struct (argstr)
"Convert ARGSTR to an instance of `numpydoc--arg'.
The argument takes on one of four possible styles:
Expand All @@ -167,7 +170,9 @@ The argument takes on one of four possible styles:
(name (s-trim (car comps2)))
(type (cadr comps2)))
(make-numpydoc--arg :name name
:type (if type (s-trim type) nil)
:type (if type
(numpydoc--none-to-optional (s-trim type))
nil)
:defval defval)))
;; only a typehint
((and (string-match-p ":" argstr)
Expand All @@ -176,7 +181,7 @@ The argument takes on one of four possible styles:
(name (s-trim (car comps1)))
(type (s-trim (cadr comps1))))
(make-numpydoc--arg :name name
:type type
:type (numpydoc--none-to-optional type)
:defval nil)))
;; only a default value
((s-contains-p "=" argstr)
Expand Down
28 changes: 14 additions & 14 deletions tests/test-numpydoc.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,33 @@
(describe "Function signature parsing"
:var ((fsig1 "\
def f(
a: int, b: float = 5.5, c: Optional[Union[str, int]] = None
a: int, b: float = 5.5, c: str | int | None = None
) -> float:")
(fsig2 "def f(x, y=5, z=None):")
(fsig3 "\
def somelongerfunc(
a1: np.ndarray,
a2: Optional[np.ndarray] = None,
a3: Optional[Sequence[float]] = None,
) -> Tuple[int, float]:")
a2: np.ndarray | None = None,
a3: Sequence[float | None] | None = None,
) -> tuple[int, float]:")
(fsig4 "\
def f(
aa,
bb=(4, 6),
cc: set = {1, 2},
dd: Dict[str, int] = dict(a=1, b=2, c = 3),
ee: Dict[str, int] = {\"a\": 5, \"b\": 6},
dd: dict[str, int] = dict(a=1, b=2, c = 3),
ee: dict[str, int] = {\"a\": 5, \"b\": 6},
ff={\"a\": 5, \"b\": 6},
gg=\"str,str\",
hh: str = \"str, str, str, str\",
ii: Tuple[int, ...] = (4, 6),
ii: tuple[int, ...] = (4, 6),
jj: str = \"str,str\",
)"))
(it "Checks arg parsing 1"
(let ((a (make-numpydoc--arg :name "a" :type "int" :defval nil))
(b (make-numpydoc--arg :name "b" :type "float" :defval "5.5"))
(c (make-numpydoc--arg :name "c"
:type "Optional[Union[str, int]]"
:type "str | int, optional"
:defval "None"))
(args (numpydoc--def-args (numpydoc--parse-def fsig1)))
(ret (numpydoc--def-rtype (numpydoc--parse-def fsig1))))
Expand All @@ -82,17 +82,17 @@ def f(
:type "np.ndarray"
:defval nil))
(a2 (make-numpydoc--arg :name "a2"
:type "Optional[np.ndarray]"
:type "np.ndarray, optional"
:defval "None"))
(a3 (make-numpydoc--arg :name "a3"
:type "Optional[Sequence[float]]"
:type "Sequence[float | None], optional"
:defval "None"))
(args (numpydoc--def-args (numpydoc--parse-def fsig3)))
(ret (numpydoc--def-rtype (numpydoc--parse-def fsig3))))
(expect a1 :to-equal (car args))
(expect a2 :to-equal (nth 1 args))
(expect a3 :to-equal (nth 2 args))
(expect ret :to-equal "Tuple[int, float]")))
(expect ret :to-equal "tuple[int, float]")))

(it "Checks arg parsing 4"
(let ((aa (make-numpydoc--arg :name "aa"
Expand All @@ -105,10 +105,10 @@ def f(
:type "set"
:defval "{1, 2}"))
(dd (make-numpydoc--arg :name "dd"
:type "Dict[str, int]"
:type "dict[str, int]"
:defval "dict(a=1, b=2, c = 3)"))
(ee (make-numpydoc--arg :name "ee"
:type "Dict[str, int]"
:type "dict[str, int]"
:defval "{\"a\": 5, \"b\": 6}"))
(ff (make-numpydoc--arg :name "ff"
:type nil
Expand All @@ -120,7 +120,7 @@ def f(
:type "str"
:defval "\"str, str, str, str\""))
(ii (make-numpydoc--arg :name "ii"
:type "Tuple[int, ...]"
:type "tuple[int, ...]"
:defval "(4, 6)"))
(jj (make-numpydoc--arg :name "jj"
:type "str"
Expand Down

0 comments on commit 1c0a2f0

Please sign in to comment.