[Python-checkins] bpo-38870: Throw ValueError on invalid yield from usage (GH-17798)

Pablo Galindo webhook-mailer at python.org
Thu Jan 2 13:20:34 EST 2020

commit: 7b35bef9787cd80ed1e12124f759b4be03c849db
branch: master
author: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>
committer: Pablo Galindo <Pablogsal at gmail.com>
date: 2020-01-02T18:20:04Z

bpo-38870: Throw ValueError on invalid yield from usage (GH-17798)

M Lib/ast.py
M Lib/test/test_unparse.py

diff --git a/Lib/ast.py b/Lib/ast.py
index 62f6e075a09fd..ece8b139e460e 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -735,10 +735,10 @@ def visit_Yield(self, node):
     def visit_YieldFrom(self, node):
         with self.delimit("(", ")"):
-            self.write("yield from")
-            if node.value:
-                self.write(" ")
-                self.traverse(node.value)
+            self.write("yield from ")
+            if not node.value:
+                raise ValueError("Node can't be used without a value attribute.")
+            self.traverse(node.value)
     def visit_Raise(self, node):
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 49767dbac16d1..e8b0d4b06f9e9 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -278,6 +278,8 @@ def test_invalid_fstring_conversion(self):
     def test_invalid_set(self):
+    def test_invalid_yield_from(self):
+        self.check_invalid(ast.YieldFrom(value=None))
 class DirectoryTestCase(ASTTestCase):
     """Test roundtrip behaviour on all files in Lib and Lib/test."""

More information about the Python-checkins mailing list