From 2b40dea54d3fc069c6df4b2c8c13163c43101e22 Mon Sep 17 00:00:00 2001 From: redraiment Date: Sun, 15 Mar 2020 14:39:21 +0800 Subject: [PATCH 1/2] Fixes #1875: parse-args requires values to be representable as Hy models * Update argument spec parse logic of parse-args function. * Update test case of parse-args function. * Update document of parse-args function. * Describe the change in NEWS file. --- NEWS.rst | 8 ++++++++ docs/language/core.rst | 2 +- hy/core/language.hy | 12 +++++++++++- tests/native_tests/core.hy | 5 +++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index b49caf7..8bff566 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -1,5 +1,13 @@ .. default-role:: code +Unreleased +============================== + +Other Breaking Changes +------------------------------ +* `parse-args` is no longer implemented with `eval`; so e.g. you should + now say `:type int` instead of `:type 'int`. + 0.18.0 ============================== diff --git a/docs/language/core.rst b/docs/language/core.rst index 6cb8c4d..97b0aac 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -804,7 +804,7 @@ may be a list of keyword arguments to pass to the .. code-block:: hy => (parse-args [["strings" :nargs "+" :help "Strings"] - ["-n" "--numbers" :action "append" :type 'int :help "Numbers"]] + ["-n" "--numbers" :action "append" :type int :help "Numbers"]] ["a" "b" "-n" "1" "-n" "2"] :description "Parse strings and numbers from args") Namespace(numbers=[1, 2], strings=['a', 'b']) diff --git a/hy/core/language.hy b/hy/core/language.hy index 4fed7ee..409d099 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -401,7 +401,17 @@ constructor." (import argparse) (setv parser (argparse.ArgumentParser #** parser-args)) (for [arg spec] - (eval `(.add-argument parser ~@arg))) + (setv positional-arguments [] + keyword-arguments [] + value-of-keyword? False) + (for [item arg] + (if value-of-keyword? + (.append (get keyword-arguments -1) item) + (if (keyword? item) + (.append keyword-arguments [(name item)]) + (.append positional-arguments item))) + (setv value-of-keyword? (and (not value-of-keyword?) (keyword? item)))) + (parser.add-argument #* positional-arguments #** (dict keyword-arguments))) (.parse-args parser args)) (setv EXPORTS diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 17f2451..b33819e 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -480,9 +480,10 @@ result['y in globals'] = 'y' in globals()") (defn test-parse-args [] "NATIVE: testing the parse-args function" + ; https://github.com/hylang/hy/issues/1875 (setv parsed-args (parse-args [["strings" :nargs "+" :help "Strings"] - ["-n" "--numbers" :action "append" :type 'int :help "Numbers"]] - ["a" "b" "-n" "1" "-n" "2"] + ["-n" :action "append" :type int :help "Numbers" "--numbers"]] + ["a" "b" "-n" "1" "--numbers" "2"] :description "Parse strings and numbers from args")) (assert-equal parsed-args.strings ["a" "b"]) (assert-equal parsed-args.numbers [1 2])) From 07513083606b399b0b1645466a7c8b9953e7b7eb Mon Sep 17 00:00:00 2001 From: redraiment Date: Tue, 31 Mar 2020 10:59:04 -0400 Subject: [PATCH 2/2] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index a85c55d..363a845 100644 --- a/AUTHORS +++ b/AUTHORS @@ -97,3 +97,4 @@ * Adam Porter * Gábor Lipták * Raymund MARTINEZ +* Zepeng Zhang