summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2014-12-05 21:40:41 +0200
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2014-12-05 21:40:41 +0200
commit1527165f2b2324aff85bdca7d33bab37534829ed (patch)
tree33cec2aa545c42efce9127f124a83544e4ae29f4
parent379579fb327d88db7bbd669d067d97b0b7af1174 (diff)
downloadastroid-git-1527165f2b2324aff85bdca7d33bab37534829ed.tar.gz
Changed the API for Class.slots.
It returns None when the class doesn't define any slots. Previously, for both the cases where the class didn't have slots defined and when it had an empty list of slots, Class.slots returned an empty list, which is ambiguous.
-rw-r--r--ChangeLog5
-rw-r--r--astroid/scoped_nodes.py17
-rw-r--r--astroid/tests/unittest_scoped_nodes.py20
3 files changed, 36 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 684a0bba..c16e462f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -13,6 +13,11 @@ Change log for the astroid package (used to be astng)
* Add brain definition for most string and unicode methods
+ * Changed the API for Class.slots. It returns None when the class
+ doesn't define any slots. Previously, for both the cases where
+ the class didn't have slots defined and when it had an empty list
+ of slots, Class.slots returned an empty list.
+
2014-11-22 -- 1.3.2
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index 5a1462bd..89137bb2 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -1348,5 +1348,18 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin):
# Cached, because inferring them all the time is expensive
@cached
def slots(self):
- """ Return all the slots for this node. """
- return list(self._islots())
+ """Get all the slots for this node.
+
+ If the class doesn't define any slot, through `__slots__`
+ variable, then this function will return a None.
+ Also, it will return None in the case the slots weren't inferred.
+ Otherwise, it will return a list of slot names.
+ """
+ slots = self._islots()
+ try:
+ first = next(slots)
+ except StopIteration:
+ # The class doesn't have a __slots__ definition.
+ return None
+ return [first] + list(slots)
+
diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py
index 5a98b152..5a94ae12 100644
--- a/astroid/tests/unittest_scoped_nodes.py
+++ b/astroid/tests/unittest_scoped_nodes.py
@@ -973,6 +973,10 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase):
__slots__ = dedent.__name__
class Eight(object):
__slots__ = ("parens")
+ class Ninth(object):
+ pass
+ class Ten(object):
+ __slots__ = dict({"a": "b", "c": "d"})
""")
first = astroid['First']
first_slots = first.slots()
@@ -988,7 +992,7 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase):
self.assertEqual(second_slots[0].value, "a")
third_slots = astroid['Third'].slots()
- self.assertEqual(third_slots, [])
+ self.assertIsNone(third_slots)
fourth_slots = astroid['Fourth'].slots()
self.assertEqual(len(fourth_slots), 2)
@@ -998,19 +1002,27 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase):
self.assertEqual(fourth_slots[1].value, "b")
fifth_slots = astroid['Fifth'].slots()
- self.assertEqual(fifth_slots, [])
+ self.assertIsNone(fifth_slots)
sixth_slots = astroid['Sixth'].slots()
- self.assertEqual(sixth_slots, [])
+ self.assertIsNone(sixth_slots)
seventh_slots = astroid['Seventh'].slots()
- self.assertEqual(len(seventh_slots), 0)
+ self.assertIsNone(seventh_slots)
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")
+ self.assertIsNone(astroid['Ninth'].slots())
+
+ tenth_slots = astroid['Ten'].slots()
+ self.assertEqual(len(tenth_slots), 2)
+ self.assertEqual(
+ [slot.value for slot in tenth_slots],
+ ["a", "c"])
+
if __name__ == '__main__':
unittest.main()