diff options
-rw-r--r-- | astroid/node_classes.py | 72 | ||||
-rw-r--r-- | astroid/objects.py | 14 |
2 files changed, 31 insertions, 55 deletions
diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 200ea28f..944598a6 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -18,6 +18,8 @@ """Module for some node classes. More nodes in scoped_nodes.py """ +import abc + import six from astroid import bases @@ -107,6 +109,31 @@ def are_exclusive(stmt1, stmt2, exceptions=None): return False +@six.add_metaclass(abc.ABCMeta) +class _BaseContainer(mixins.ParentAssignTypeMixin, + bases.NodeNG, + bases.Instance): + """Base class for Set, FrozenSet, Tuple and list.""" + + _astroid_fields = ('elts',) + + def __init__(self, elts=None): + if elts is None: + self.elts = [] + else: + self.elts = [const_factory(e) for e in elts] + + def itered(self): + return self.elts + + def bool_value(self): + return bool(self.elts) + + @abc.abstractmethod + def pytype(self): + pass + + class LookupMixIn(object): """Mixin looking up a name in the right scope """ @@ -782,15 +809,8 @@ class Keyword(bases.NodeNG): value = None -class List(mixins.ParentAssignTypeMixin, bases.NodeNG, bases.Instance): +class List(_BaseContainer): """class representing a List node""" - _astroid_fields = ('elts',) - - def __init__(self, elts=None): - if elts is None: - self.elts = [] - else: - self.elts = [const_factory(e) for e in elts] def pytype(self): return '%s.list' % BUILTINS @@ -798,12 +818,6 @@ class List(mixins.ParentAssignTypeMixin, bases.NodeNG, bases.Instance): def getitem(self, index, context=None): return self.elts[index] - def itered(self): - return self.elts - - def bool_value(self): - return bool(self.elts) - class Nonlocal(bases.Statement): """class representing a Nonlocal node""" @@ -852,25 +866,12 @@ class Return(bases.Statement): value = None -class Set(mixins.ParentAssignTypeMixin, bases.NodeNG, bases.Instance): +class Set(_BaseContainer): """class representing a Set node""" - _astroid_fields = ('elts',) - - def __init__(self, elts=None): - if elts is None: - self.elts = [] - else: - self.elts = [const_factory(e) for e in elts] def pytype(self): return '%s.set' % BUILTINS - def itered(self): - return self.elts - - def bool_value(self): - return bool(self.elts) - class Slice(bases.NodeNG): """class representing a Slice node""" @@ -932,15 +933,8 @@ class TryFinally(mixins.BlockRangeMixIn, bases.Statement): return self._elsed_block_range(lineno, self.finalbody) -class Tuple(mixins.ParentAssignTypeMixin, bases.NodeNG, bases.Instance): +class Tuple(_BaseContainer): """class representing a Tuple node""" - _astroid_fields = ('elts',) - - def __init__(self, elts=None): - if elts is None: - self.elts = [] - else: - self.elts = [const_factory(e) for e in elts] def pytype(self): return '%s.tuple' % BUILTINS @@ -948,12 +942,6 @@ class Tuple(mixins.ParentAssignTypeMixin, bases.NodeNG, bases.Instance): def getitem(self, index, context=None): return self.elts[index] - def itered(self): - return self.elts - - def bool_value(self): - return bool(self.elts) - class UnaryOp(bases.NodeNG): """class representing an UnaryOp node""" diff --git a/astroid/objects.py b/astroid/objects.py index c2ac9a5f..aa3848a3 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -40,21 +40,12 @@ from astroid import scoped_nodes BUILTINS = six.moves.builtins.__name__ -class FrozenSet(bases.NodeNG, bases.Instance, mixins.ParentAssignTypeMixin): +class FrozenSet(node_classes._BaseContainer): """class representing a FrozenSet composite node""" - def __init__(self, elts=None): - if elts is None: - self.elts = [] - else: - self.elts = [node_classes.const_factory(e) for e in elts] - def pytype(self): return '%s.frozenset' % BUILTINS - def itered(self): - return self.elts - def _infer(self, context=None): yield self @@ -63,9 +54,6 @@ class FrozenSet(bases.NodeNG, bases.Instance, mixins.ParentAssignTypeMixin): builtins = MANAGER.astroid_cache[BUILTINS] return builtins.getattr('frozenset')[0] - def bool_value(self): - return bool(self.elts) - class Super(bases.NodeNG): """Proxy class over a super call. |