Merge pull request #1590 from Kodiologist/no-lambda-unpacking

Remove tuple unpacking in lambda lists
This commit is contained in:
Kodi Arfer 2018-05-04 11:11:06 -07:00 committed by GitHub
commit 7a80aa92be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 36 deletions

View File

@ -9,6 +9,8 @@ Removals
These were redundant with Python's built-in data structures and Hy's most
common model types (`HyExpression`, `HyList`, etc.).
* `&key` is no longer special in lambda lists. Use `&optional` instead.
* Tuple unpacking is no longer built into special forms for function
definition (`fn` etc.)
Other Breaking Changes
------------------------------

View File

@ -553,6 +553,8 @@ class HyASTCompiler(object):
continue
if lambda_keyword is None:
if not isinstance(expr, HySymbol):
raise HyTypeError(expr, "Parameters must be symbols")
args.append(expr)
elif lambda_keyword == "&rest":
if varargs:
@ -1893,19 +1895,6 @@ class HyASTCompiler(object):
(ret, args, defaults, stararg,
kwonlyargs, kwonlydefaults, kwargs) = self._parse_lambda_list(arglist)
for i, arg in enumerate(args):
if isinstance(arg, HyList):
# Destructuring argument
if not arg:
raise HyTypeError(arglist,
"Cannot destruct empty list")
args[i] = var = HySymbol(self.get_anon_var())
expression = HyExpression([
HyExpression([
HySymbol("setv"), arg, var
])]
) + expression
expression = expression.replace(arg[0])
# Before Python 3.7, docstrings must come at the start, so ensure that
# happens even if we generate anonymous variables.

View File

@ -166,7 +166,7 @@ Arguments without a header are under None.
(for [[header section] (-> self (.tail) first lambda-list .items)]
(if header (.append argslist header))
(cond [(in header [None '&rest '&kwargs])
(.update protected (-> section flatten set))
(.update protected section)
(.extend argslist section)]
[(in header '[&optional &kwonly])
(for [pair section]

View File

@ -414,12 +414,6 @@ def test_ast_tuple():
assert type(code) == ast.Tuple
def test_argument_destructuring():
""" Ensure argument destructuring compilers. """
can_compile("(fn [[a b]] (print a b))")
cant_compile("(fn [[]] 0)")
def test_lambda_list_keywords_rest():
""" Ensure we can compile functions with lambda list keywords."""
can_compile("(fn [x &rest xs] (print xs))")

View File

@ -1761,11 +1761,6 @@ macros()
(= (identify-keywords 1 "bloo" :foo)
["other" "other" "keyword"])))
(defn test-argument-destr []
"Make sure argument destructuring works"
(defn f [[a b] [c]] (, a b c))
(assert (= (f [1 2] [3]) (, 1 2 3))))
#@(pytest.mark.xfail
(defn test-assert-multistatements []
; https://github.com/hylang/hy/issues/1390
@ -1789,21 +1784,11 @@ macros()
(defn f [] "docstring" 5)
(assert (= (. f __doc__) "docstring"))
; destructuring and the implicit variables it creates
; shouldn't interfere with docstrings
; (https://github.com/hylang/hy/issues/1409)
(defn f2 [[a b]] "docstring" 5)
(assert (= (. f2 __doc__) "docstring"))
; a single string is the return value, not a docstring
; (https://github.com/hylang/hy/issues/1402)
(defn f3 [] "not a docstring")
(assert (none? (. f3 __doc__)))
(assert (= (f3) "not a docstring"))
(defn f4 [[a b]] "not a docstring")
(assert (none? (. f4 __doc__)))
(assert (= (f4 [1 2]) "not a docstring")))
(assert (= (f3) "not a docstring")))
(defn test-module-docstring []
(import [tests.resources.module-docstring-example :as m])