summaryrefslogtreecommitdiff
path: root/astroid/objects.py
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-09-26 18:08:51 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2015-09-26 18:08:51 +0300
commit9362707c3279c13480f16d5f90758c1eafd05bdf (patch)
tree6c14c71f53f84720d70719634a37c0729a2047b8 /astroid/objects.py
parent10879854328dd72ab4815a9f21d943d3573808c6 (diff)
downloadastroid-9362707c3279c13480f16d5f90758c1eafd05bdf.tar.gz
Add a separate Slice object, which is used when inferring the slice builtin call
We do this because of an internal distinction we're making between AST nodes and inference objects. The following distinction might help for understanding better where an AST node is used and where an inference object is used: * if the AST node can be used on its own, e.g. List, Tuple, etc, then the AST can be used in the inference. They have a syntactic display and can be written on its own, e.g. "[]" is a valid list. That's why the inference of their respective builtins (list, tuple, set etc) returns an AST node when inferring. * if the AST node is part of syntax and can't be used on its own. This is the case for slices for instance, e.g "[2:3:4]". In this case, trying to write "2:3:4" on its own will fail. The respective builtin (slice) returns a custom object when inferring and not an AST node per se.
Diffstat (limited to 'astroid/objects.py')
-rw-r--r--astroid/objects.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/astroid/objects.py b/astroid/objects.py
index 02fecef..5318f43 100644
--- a/astroid/objects.py
+++ b/astroid/objects.py
@@ -24,6 +24,23 @@ leads to an inferred FrozenSet:
Call(func=Name('frozenset'), args=Tuple(...))
+
+This distinction might help for understanding better where to use
+an AST node and where to use an inference object:
+
+ * if the AST node can be used on its own, e.g. List, Tuple, etc,
+ then the AST can be used.
+ They have a syntactic display and can be written on its own,
+ e.g. "[]" is a valid list. That's why the inference of their
+ respective builtins (list, tuple, set etc) returns an AST node
+ when inferring.
+
+ * if the AST node is part of syntax and can't be used
+ on its own. This is the case for slices for instance, e.g "[2:3:4]".
+ In this case, trying to write "2:3:4" on its own will fail.
+ The respective builtin (slice) returns a custom object when inferring
+ and not an AST node per se.
+
"""
import six
@@ -55,6 +72,32 @@ class FrozenSet(node_classes._BaseContainer):
return builtins.getattr('frozenset')[0]
+class Slice(node_classes.Slice):
+ """Custom object for representing slices internally."""
+
+ @decorators.cachedproperty
+ def _proxied(self):
+ builtins = MANAGER.astroid_cache[BUILTINS]
+ return builtins.getattr('slice')[0]
+
+ def pytype(self):
+ return '%s.slice' % BUILTINS
+
+ def igetattr(self, attrname, context=None):
+ if attrname == 'start':
+ yield self.lower
+ elif attrname == 'stop':
+ yield self.upper
+ elif attrname == 'step':
+ yield self.step
+ else:
+ for value in self.getattr(attrname, context=context):
+ yield value
+
+ def getattr(self, attrname, context=None):
+ return self._proxied.getattr(attrname, context)
+
+
class Super(bases.NodeNG):
"""Proxy class over a super call.