diff options
Diffstat (limited to 'astroid/objects.py')
-rw-r--r-- | astroid/objects.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/astroid/objects.py b/astroid/objects.py index 02fecef8..5318f439 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. |