diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2014-03-02 12:07:33 +0200 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-03-02 12:07:33 +0200 |
| commit | 2e0df240db0cf677b0821371af7c2dfc021452ab (patch) | |
| tree | dceb3a67c05a44221177473b682b979359a736c0 | |
| parent | 8506cd2a4577bd0a2a6cdff412201c628912c8d1 (diff) | |
| download | astroid-git-2e0df240db0cf677b0821371af7c2dfc021452ab.tar.gz | |
Add yield_from property for Yield nodes.
--HG--
branch : yield_from
| -rw-r--r-- | ChangeLog | 3 | ||||
| -rw-r--r-- | as_string.py | 6 | ||||
| -rw-r--r-- | node_classes.py | 6 | ||||
| -rw-r--r-- | rebuilder.py | 4 | ||||
| -rw-r--r-- | test/unittest_python3.py | 26 |
5 files changed, 40 insertions, 5 deletions
@@ -14,6 +14,9 @@ Change log for the astroid package (used to be astng) * Don't crash when inferring nodes from `with` clauses with multiple context managers. Closes #18. + + * Add `yield_from` property to `Yield` nodes, in order + to differentiate between the classic `yield` and `yield from`. 2013-10-18 -- 1.0.1 diff --git a/as_string.py b/as_string.py index 72c5b6c3..1863b937 100644 --- a/as_string.py +++ b/as_string.py @@ -429,7 +429,11 @@ class AsStringVisitor(object): def visit_yield(self, node): """yield an ast.Yield node as string""" yi_val = node.value and (" " + node.value.accept(self)) or "" - expr = 'yield' + yi_val + if node.yield_from: + yield_ = 'yield from' + else: + yield_ = 'yield' + expr = yield_ + yi_val if node.parent.is_statement: return expr else: diff --git a/node_classes.py b/node_classes.py index 97bd9079..3873127e 100644 --- a/node_classes.py +++ b/node_classes.py @@ -886,7 +886,11 @@ class With(BlockRangeMixIn, AssignTypeMixin, Statement): class Yield(NodeNG): """class representing a Yield node""" _astroid_fields = ('value',) - value = None + value = _from = None + + @property + def yield_from(self): + return self._from # constants ############################################################## diff --git a/rebuilder.py b/rebuilder.py index b9a9adbb..24ce8027 100644 --- a/rebuilder.py +++ b/rebuilder.py @@ -954,7 +954,9 @@ class TreeRebuilder3k(TreeRebuilder): return newnode def visit_yieldfrom(self, node, parent): - return self.visit_yield(node, parent) + newnode = self.visit_yield(node, parent) + newnode._from = True + return newnode def visit_class(self, node, parent): newnode = super(TreeRebuilder3k, self).visit_class(node, parent) diff --git a/test/unittest_python3.py b/test/unittest_python3.py index d17b1432..e04be837 100644 --- a/test/unittest_python3.py +++ b/test/unittest_python3.py @@ -20,10 +20,10 @@ from textwrap import dedent from logilab.common.testlib import TestCase, unittest_main, require_version -from astroid.node_classes import Assign +from astroid.node_classes import Assign, Discard, Yield from astroid.manager import AstroidManager from astroid.builder import AstroidBuilder -from astroid.scoped_nodes import Class +from astroid.scoped_nodes import Class, Function class Python3TC(TestCase): @@ -41,6 +41,28 @@ class Python3TC(TestCase): self.assertTrue(isinstance(node.ass_type(), Assign)) + @require_version('3.3') + def test_yield_from(self): + body = dedent(""" + def func(): + yield from iter([1, 2]) + yield 1 + """) + astroid = self.builder.string_build(body) + func = astroid.body[0] + self.assertIsInstance(func, Function) + yield_from = func.body[0] + yield_ = func.body[1] + self.assertIsInstance(yield_from, Discard) + self.assertIsInstance(yield_from.value, Yield) + self.assertTrue(yield_from.value.yield_from) + self.assertEqual(yield_from.as_string(), + 'yield from iter([1, 2])') + + self.assertIsInstance(yield_, Discard) + self.assertIsInstance(yield_.value, Yield) + self.assertFalse(yield_.value.yield_from) + # metaclass tests @require_version('3.0') |
