Allow comprehensions with no looping parts

This commit is contained in:
Kodi Arfer 2018-06-12 11:00:01 -07:00
parent e1972c535f
commit 4754b152a9
2 changed files with 19 additions and 0 deletions

View File

@ -1094,6 +1094,13 @@ class HyASTCompiler(object):
# Compile the parts.
if is_for:
parts = parts[0]
if not parts:
return Result(expr=ast.parse({
asty.For: "None",
asty.ListComp: "[]",
asty.DictComp: "{}",
asty.SetComp: "{1}.__class__()",
asty.GeneratorExp: "(_ for _ in [])"}[node_class]).body[0].value)
parts = [
Tag(p.tag, self.compile(p.value) if p.tag in ["if", "do"] else [
self._storeize(p.value[0], self.compile(p.value[0])),

View File

@ -98,6 +98,18 @@
(assert (= (sorted result) answer) (str expr)))))
(defn test-fors-no-loopers []
(setv l [])
(for [] (.append l 1))
(assert (= l []))
(assert (= (lfor 1) []))
(assert (= (sfor 1) #{}))
(assert (= (list (gfor 1)) []))
(assert (= (dfor [1 2]) {})))
(defn test-raise-in-comp []
(defclass E [Exception] [])
(setv l [])