summaryrefslogtreecommitdiff
path: root/astroid/node_classes.py
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2015-08-21 00:54:39 +0300
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2015-08-21 00:54:39 +0300
commit74b27556ce98b5665cc72bbcfbbf4bdd8f2ab678 (patch)
tree9a69ff892dfc3b7a07677196d4ec908ddbb9576b /astroid/node_classes.py
parentd4e89a34559094d0d8bca0be4137b77caf53d874 (diff)
downloadastroid-74b27556ce98b5665cc72bbcfbbf4bdd8f2ab678.tar.gz
Understand slices of tuples, lists, strings and instances with support for slices.
Closes issue #137.
Diffstat (limited to 'astroid/node_classes.py')
-rw-r--r--astroid/node_classes.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/astroid/node_classes.py b/astroid/node_classes.py
index 944598a..5ea755e 100644
--- a/astroid/node_classes.py
+++ b/astroid/node_classes.py
@@ -109,6 +109,17 @@ def are_exclusive(stmt1, stmt2, exceptions=None):
return False
+def _container_getitem(instance, elts, index):
+ """Get a slice or an item, using the given *index*, for the given sequence."""
+ if isinstance(index, slice):
+ new_cls = instance.__class__()
+ new_cls.elts = elts[index]
+ new_cls.parent = instance.parent
+ return new_cls
+ else:
+ return elts[index]
+
+
@six.add_metaclass(abc.ABCMeta)
class _BaseContainer(mixins.ParentAssignTypeMixin,
bases.NodeNG,
@@ -816,7 +827,7 @@ class List(_BaseContainer):
return '%s.list' % BUILTINS
def getitem(self, index, context=None):
- return self.elts[index]
+ return _container_getitem(self, self.elts, index)
class Nonlocal(bases.Statement):
@@ -940,7 +951,7 @@ class Tuple(_BaseContainer):
return '%s.tuple' % BUILTINS
def getitem(self, index, context=None):
- return self.elts[index]
+ return _container_getitem(self, self.elts, index)
class UnaryOp(bases.NodeNG):