fix_apply.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Fixer for apply().
  4. This converts apply(func, v, k) into (func)(*v, **k)."""
  5. # Local imports
  6. from .. import pytree
  7. from ..pgen2 import token
  8. from .. import fixer_base
  9. from ..fixer_util import Call, Comma, parenthesize
  10. class FixApply(fixer_base.BaseFix):
  11. BM_compatible = True
  12. PATTERN = """
  13. power< 'apply'
  14. trailer<
  15. '('
  16. arglist<
  17. (not argument<NAME '=' any>) func=any ','
  18. (not argument<NAME '=' any>) args=any [','
  19. (not argument<NAME '=' any>) kwds=any] [',']
  20. >
  21. ')'
  22. >
  23. >
  24. """
  25. def transform(self, node, results):
  26. syms = self.syms
  27. assert results
  28. func = results["func"]
  29. args = results["args"]
  30. kwds = results.get("kwds")
  31. # I feel like we should be able to express this logic in the
  32. # PATTERN above but I don't know how to do it so...
  33. if args:
  34. if (args.type == self.syms.argument and
  35. args.children[0].value in {'**', '*'}):
  36. return # Make no change.
  37. if kwds and (kwds.type == self.syms.argument and
  38. kwds.children[0].value == '**'):
  39. return # Make no change.
  40. prefix = node.prefix
  41. func = func.clone()
  42. if (func.type not in (token.NAME, syms.atom) and
  43. (func.type != syms.power or
  44. func.children[-2].type == token.DOUBLESTAR)):
  45. # Need to parenthesize
  46. func = parenthesize(func)
  47. func.prefix = ""
  48. args = args.clone()
  49. args.prefix = ""
  50. if kwds is not None:
  51. kwds = kwds.clone()
  52. kwds.prefix = ""
  53. l_newargs = [pytree.Leaf(token.STAR, "*"), args]
  54. if kwds is not None:
  55. l_newargs.extend([Comma(),
  56. pytree.Leaf(token.DOUBLESTAR, "**"),
  57. kwds])
  58. l_newargs[-2].prefix = " " # that's the ** token
  59. # XXX Sometimes we could be cleverer, e.g. apply(f, (x, y) + t)
  60. # can be translated into f(x, y, *t) instead of f(*(x, y) + t)
  61. #new = pytree.Node(syms.power, (func, ArgList(l_newargs)))
  62. return Call(func, l_newargs, prefix=prefix)