fix_future_builtins.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Adds this import line:
  3. from builtins import XYZ
  4. for each of the functions XYZ that is used in the module.
  5. """
  6. from __future__ import unicode_literals
  7. from lib2to3 import fixer_base
  8. from lib2to3.pygram import python_symbols as syms
  9. from lib2to3.fixer_util import Name, Call, in_special_context
  10. from libfuturize.fixer_util import touch_import_top
  11. # All builtins are:
  12. # from future.builtins.iterators import (filter, map, zip)
  13. # from future.builtins.misc import (ascii, chr, hex, input, isinstance, oct, open, round, super)
  14. # from future.types import (bytes, dict, int, range, str)
  15. # We don't need isinstance any more.
  16. replaced_builtins = '''filter map zip
  17. ascii chr hex input next oct open round super
  18. bytes dict int range str'''.split()
  19. expression = '|'.join(["name='{0}'".format(name) for name in replaced_builtins])
  20. class FixFutureBuiltins(fixer_base.BaseFix):
  21. BM_compatible = True
  22. run_order = 9
  23. # Currently we only match uses as a function. This doesn't match e.g.:
  24. # if isinstance(s, str):
  25. # ...
  26. PATTERN = """
  27. power<
  28. ({0}) trailer< '(' args=[any] ')' >
  29. rest=any* >
  30. """.format(expression)
  31. def transform(self, node, results):
  32. name = results["name"]
  33. touch_import_top(u'builtins', name.value, node)
  34. # name.replace(Name(u"input", prefix=name.prefix))