Commits

Steven Silvester  committed a27cd50

Enhanced fix_metaclass - ensure the class inherits from 'object'

  • Participants
  • Parent commits b9455c6

Comments (0)

Files changed (2)

File lib3to2/fixes/fix_metaclass.py

         stmt_node = Node(syms.atom, [target, equal, name])
         
         suitify(node)
-        for item in node.children:
+        left_ind, right_ind = 0, 0
+        for (ind, item) in enumerate(node.children):
+            if item.type == token.LPAR:
+                left_ind = ind
+            elif item.type == token.RPAR:
+                right_ind = ind
             if item.type == syms.suite:
                 for stmt in item.children:
                     if stmt.type == token.INDENT:
                         item.insert_child(loc, Newline())
                         item.insert_child(loc, stmt_node)
                         break
+        if right_ind - left_ind == 1:
+            node.insert_child(left_ind + 1, Name(u"object"))

File lib3to2/tests/test_metaclass.py

 
 class Test_metaclass(lib3to2FixerTestCase):
 
-    fixer = 'metaclass'
+    fixer = u'metaclass'
 
     def test_unchanged(self):
-        self.unchanged("class X(): pass")
-        self.unchanged("class X(object): pass")
-        self.unchanged("class X(object1, object2): pass")
-        self.unchanged("class X(object1, object2, object3): pass")
+        self.unchanged(u"class X(): pass")
+        self.unchanged(u"class X(object): pass")
+        self.unchanged(u"class X(object1, object2): pass")
+        self.unchanged(u"class X(object1, object2, object3): pass")
 
-        s = """
+        s = u"""
         class X():
             def __metaclass__(self): pass
         """
         self.unchanged(s)
 
-        s = """
+        s = u"""
         class X():
             a[23] = 74
         """
         self.unchanged(s)
 
     def test_comments(self):
-        a = """
-        class X():
+        a = u"""
+        class X(object):
             # hi
             __metaclass__ = AppleMeta
             pass
         """
-        b = """
+        b = u"""
         class X(metaclass=AppleMeta):
             # hi
             pass
         """
         self.check(b, a)
 
-        a = """
-        class X():
+        a = u"""
+        class X(object):
             __metaclass__ = Meta
             pass
             # Bedtime!
         """
-        b = """
+        b = u"""
         class X(metaclass=Meta):
             pass
             # Bedtime!
 
     def test_meta_noparent_odd_body(self):
         # no-parent class, odd body
-        a = """
-        class X():
+        a = u"""
+        class X(object):
             __metaclass__ = Q
             pass
         """
-        b = """
+        b = u"""
         class X(metaclass=Q):
             pass
         """
 
     def test_meta_oneparent_no_body(self):
         # one parent class, no body
-        a = """
+        a = u"""
         class X(object):
             __metaclass__ = Q
             pass"""
-        b = """
+        b = u"""
         class X(object, metaclass=Q): pass"""
         self.check(b, a)
 
     def test_meta_oneparent_simple_body_1(self):
         # one parent, simple body
-        a = """
+        a = u"""
         class X(object):
             __metaclass__ = Meta
             bar = 7
         """
-        b = """
+        b = u"""
         class X(object, metaclass=Meta):
             bar = 7
         """
         self.check(b, a)
 
     def test_meta_oneparent_simple_body_2(self):
-        a = """
-        class X():
+        a = u"""
+        class X(object):
             __metaclass__ = Meta
             x = 4; g = 23
         """
-        b = """
+        b = u"""
         class X(metaclass=Meta):
             x = 4; g = 23
         """
         self.check(b, a)
 
     def test_meta_oneparent_simple_body_3(self):
-        a = """
+        a = u"""
         class X(object):
             __metaclass__ = Meta
             bar = 7
         """
-        b = """
+        b = u"""
         class X(object, metaclass=Meta):
             bar = 7
         """
 
     def test_meta_multiparent_simple_body_1(self):
         # multiple inheritance, simple body
-        a = """
+        a = u"""
         class X(clsA, clsB):
             __metaclass__ = Meta
             bar = 7
         """
-        b = """
+        b = u"""
         class X(clsA, clsB, metaclass=Meta):
             bar = 7
         """
 
     def test_meta_multiparent_simple_body_2(self):
         # keywords in the class statement
-        a = """
+        a = u"""
         class m(a, arg=23):
             __metaclass__ = Meta
             pass"""
-        b = """
+        b = u"""
         class m(a, arg=23, metaclass=Meta):
             pass"""
         self.check(b, a)
 
     def test_meta_expression_simple_body_1(self):
-        a = """
+        a = u"""
         class X(expression(2 + 4)):
             __metaclass__ = Meta
             pass
         """
-        b = """
+        b = u"""
         class X(expression(2 + 4), metaclass=Meta):
             pass
         """
         self.check(b, a)
 
     def test_meta_expression_simple_body_2(self):
-        a = """
+        a = u"""
         class X(expression(2 + 4), x**4):
             __metaclass__ = Meta
             pass
         """
-        b = """
+        b = u"""
         class X(expression(2 + 4), x**4, metaclass=Meta):
             pass
         """
 
     def test_meta_noparent_simple_body(self):
 
-        a = """
-        class X():
+        a = u"""
+        class X(object):
             __metaclass__ = Meta
             save.py = 23
             out = 5
         """
-        b = """
+        b = u"""
         class X(metaclass=Meta):
             save.py = 23
             out = 5