diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2014-07-03 19:15:41 +0300 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-07-03 19:15:41 +0300 |
| commit | 41aa1086a294c003fb50ef82d9bbbb4e1b17d113 (patch) | |
| tree | a76e3e6456822d0e31974bc8b668b221cc289048 | |
| parent | bae8340f044ed41e9b7cfde8d5a8880860862f67 (diff) | |
| parent | 99974751cd6c7fbb54b8b607f7960bfc188e677e (diff) | |
| download | astroid-git-41aa1086a294c003fb50ef82d9bbbb4e1b17d113.tar.gz | |
Merged in PCManticore/astroid/slots (pull request #37)
Add `slots` method to Class nodes.
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | scoped_nodes.py | 54 | ||||
| -rw-r--r-- | test/unittest_scoped_nodes.py | 60 |
3 files changed, 118 insertions, 0 deletions
@@ -5,10 +5,14 @@ Change log for the astroid package (used to be astng) * `Class.metaclass()` now handles module-level __metaclass__ declaration on python 2, and no longer looks at the __metaclass__ class attribute on python 3. + * Function nodes can detect if they are decorated with subclasses of builtin descriptors when determining their type (`classmethod` and `staticmethod`). + * Add `slots` method to `Class` nodes, for retrieving + the list of valid slots it defines. + 2014-04-30 -- 1.1.1 * `Class.metaclass()` looks in ancestors when the current class does not define explicitly a metaclass. diff --git a/scoped_nodes.py b/scoped_nodes.py index e354c5b8..95d45a5d 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -45,6 +45,8 @@ from astroid.mixins import FilterStmtsMixin from astroid.bases import Statement from astroid.manager import AstroidManager +ITER_METHODS = ('__iter__', '__getitem__') + def remove_nodes(func, cls): def wrapper(*args, **kwargs): @@ -1133,3 +1135,55 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin): if klass is not None: break return klass + + def _islots(self): + """ Return an iterator with the inferred slots. """ + if '__slots__' not in self.locals: + return + for slots in self.igetattr('__slots__'): + # check if __slots__ is a valid type + for meth in ITER_METHODS: + try: + slots.getattr(meth) + break + except NotFoundError: + continue + else: + continue + + if isinstance(slots, Const): + # a string. Ignore the following checks, + # but yield the node, only if it has a value + if slots.value: + yield slots + continue + if not hasattr(slots, 'itered'): + # we can't obtain the values, maybe a .deque? + continue + + if isinstance(slots, Dict): + values = [item[0] for item in slots.items] + else: + values = slots.itered() + if values is YES: + continue + + for elt in values: + try: + for infered in elt.infer(): + if infered is YES: + continue + if (not isinstance(infered, Const) or + not isinstance(infered.value, str)): + continue + if not infered.value: + continue + yield infered + except InferenceError: + continue + + # Cached, because inferring them all the time is expensive + @cached + def slots(self): + """ Return all the slots for this node. """ + return list(self._islots()) diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index b6f3434b..37ff3be8 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -869,6 +869,66 @@ def g2(): # https://bitbucket.org/logilab/astroid/issue/17 self.assertEqual(list(instance.infer()), [YES]) + def test_slots(self): + astroid = abuilder.string_build(dedent(""" + from collections import deque + from textwrap import dedent + + class First(object): + __slots__ = ("a", "b", 1) + class Second(object): + __slots__ = "a" + class Third(object): + __slots__ = deque(["a", "b", "c"]) + class Fourth(object): + __slots__ = {"a": "a", "b": "b"} + class Fifth(object): + __slots__ = list + class Sixth(object): + __slots__ = "" + class Seventh(object): + __slots__ = dedent.__name__ + class Eight(object): + __slots__ = ("parens") + """)) + first = astroid['First'] + first_slots = first.slots() + self.assertEqual(len(first_slots), 2) + self.assertIsInstance(first_slots[0], nodes.Const) + self.assertIsInstance(first_slots[1], nodes.Const) + self.assertEqual(first_slots[0].value, "a") + self.assertEqual(first_slots[1].value, "b") + + second_slots = astroid['Second'].slots() + self.assertEqual(len(second_slots), 1) + self.assertIsInstance(second_slots[0], nodes.Const) + self.assertEqual(second_slots[0].value, "a") + + third_slots = astroid['Third'].slots() + self.assertEqual(third_slots, []) + + fourth_slots = astroid['Fourth'].slots() + self.assertEqual(len(fourth_slots), 2) + self.assertIsInstance(fourth_slots[0], nodes.Const) + self.assertIsInstance(fourth_slots[1], nodes.Const) + self.assertEqual(fourth_slots[0].value, "a") + self.assertEqual(fourth_slots[1].value, "b") + + fifth_slots = astroid['Fifth'].slots() + self.assertEqual(fifth_slots, []) + + sixth_slots = astroid['Sixth'].slots() + self.assertEqual(sixth_slots, []) + + seventh_slots = astroid['Seventh'].slots() + self.assertEqual(len(seventh_slots), 0) + + eight_slots = astroid['Eight'].slots() + self.assertEqual(len(eight_slots), 1) + self.assertIsInstance(eight_slots[0], nodes.Const) + self.assertEqual(eight_slots[0].value, "parens") + + __all__ = ('ModuleNodeTC', 'ImportNodeTC', 'FunctionNodeTC', 'ClassNodeTC') if __name__ == '__main__': |
