summaryrefslogtreecommitdiff
path: root/astroid/tests/unittest_nodes.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-12-08 10:42:36 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2015-12-08 10:42:36 +0200
commit91747c50f885efe461c0054a7835992f49740ab9 (patch)
treebf730a97ef123b77e55d5f9eb528d9930e61f9d0 /astroid/tests/unittest_nodes.py
parent4b4cdeb07b670ef2366c159ef675c336f3c2fb6f (diff)
downloadastroid-91747c50f885efe461c0054a7835992f49740ab9.tar.gz
Some nodes got a new attribute, 'ctx', which tells in which context the said node was used.
The possible values for the contexts are `Load` ('a'), `Del` ('del a'), `Store` ('a = 4') and the nodes that got the new attribute are Starred, Subscript, List and Tuple. The builtin ast module provides contexts for Name and Attribute as well, but we took a different approach in the past, by having different nodes for each type of context. For instance, Name used in a Del context is a DelName, while Name used in a Store context is AssignName. Since this is ingrained in astroid since quite some time, it makes no sense to change them as well, even though it's a loss of consistency. The patch introduces a new dependency to enum34 on older Python versions, which is used for building the three possible enum values for the contexts. Closes issue #267.
Diffstat (limited to 'astroid/tests/unittest_nodes.py')
-rw-r--r--astroid/tests/unittest_nodes.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py
index 8abd85e..cf32ce9 100644
--- a/astroid/tests/unittest_nodes.py
+++ b/astroid/tests/unittest_nodes.py
@@ -25,6 +25,7 @@ import warnings
import six
+import astroid
from astroid import bases
from astroid import builder
from astroid import context as contextmod
@@ -757,5 +758,54 @@ class Python35AsyncTest(unittest.TestCase):
self._test_await_async_as_string(code)
+class ContextTest(unittest.TestCase):
+
+ def test_subscript_load(self):
+ node = test_utils.extract_node('f[1]')
+ self.assertIs(node.ctx, astroid.Load)
+
+ def test_subscript_del(self):
+ node = test_utils.extract_node('del f[1]')
+ self.assertIs(node.targets[0].ctx, astroid.Del)
+
+ def test_subscript_store(self):
+ node = test_utils.extract_node('f[1] = 2')
+ subscript = node.targets[0]
+ self.assertIs(subscript.ctx, astroid.Store)
+
+ def test_list_load(self):
+ node = test_utils.extract_node('[]')
+ self.assertIs(node.ctx, astroid.Load)
+
+ def test_list_del(self):
+ node = test_utils.extract_node('del []')
+ self.assertIs(node.targets[0].ctx, astroid.Del)
+
+ def test_list_store(self):
+ with self.assertRaises(exceptions.AstroidSyntaxError):
+ test_utils.extract_node('[0] = 2')
+
+ def test_tuple_load(self):
+ node = test_utils.extract_node('(1, )')
+ self.assertIs(node.ctx, astroid.Load)
+
+ def test_tuple_store(self):
+ with self.assertRaises(exceptions.AstroidSyntaxError):
+ test_utils.extract_node('(1, ) = 3')
+
+ @test_utils.require_version(minver='3.5')
+ def test_starred_load(self):
+ node = test_utils.extract_node('a = *b')
+ starred = node.value
+ self.assertIs(starred.ctx, astroid.Load)
+
+ @test_utils.require_version(minver='3.0')
+ def test_starred_store(self):
+ node = test_utils.extract_node('a, *b = 1, 2')
+ starred = node.targets[0].elts[1]
+ self.assertIs(starred.ctx, astroid.Store)
+
+
+
if __name__ == '__main__':
unittest.main()