fix_reload.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Fixer for reload().
  2. reload(s) -> importlib.reload(s)"""
  3. # Local imports
  4. from .. import fixer_base
  5. from ..fixer_util import ImportAndCall, touch_import
  6. class FixReload(fixer_base.BaseFix):
  7. BM_compatible = True
  8. order = "pre"
  9. PATTERN = """
  10. power< 'reload'
  11. trailer< lpar='('
  12. ( not(arglist | argument<any '=' any>) obj=any
  13. | obj=arglist<(not argument<any '=' any>) any ','> )
  14. rpar=')' >
  15. after=any*
  16. >
  17. """
  18. def transform(self, node, results):
  19. if results:
  20. # I feel like we should be able to express this logic in the
  21. # PATTERN above but I don't know how to do it so...
  22. obj = results['obj']
  23. if obj:
  24. if (obj.type == self.syms.argument and
  25. obj.children[0].value in {'**', '*'}):
  26. return # Make no change.
  27. names = ('importlib', 'reload')
  28. new = ImportAndCall(node, results, names)
  29. touch_import(None, 'importlib', node)
  30. return new