fix_newstyle.py 888 B

123456789101112131415161718192021222324252627282930313233
  1. u"""
  2. Fixer for "class Foo: ..." -> "class Foo(object): ..."
  3. """
  4. from lib2to3 import fixer_base
  5. from lib2to3.fixer_util import LParen, RParen, Name
  6. from libfuturize.fixer_util import touch_import_top
  7. def insert_object(node, idx):
  8. node.insert_child(idx, RParen())
  9. node.insert_child(idx, Name(u"object"))
  10. node.insert_child(idx, LParen())
  11. class FixNewstyle(fixer_base.BaseFix):
  12. # Match:
  13. # class Blah:
  14. # and:
  15. # class Blah():
  16. PATTERN = u"classdef< 'class' NAME ['(' ')'] colon=':' any >"
  17. def transform(self, node, results):
  18. colon = results[u"colon"]
  19. idx = node.children.index(colon)
  20. if (node.children[idx-2].value == '(' and
  21. node.children[idx-1].value == ')'):
  22. del node.children[idx-2:idx]
  23. idx -= 2
  24. insert_object(node, idx)
  25. touch_import_top(u'builtins', 'object', node)