fix_reduce.py 837 B

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright 2008 Armin Ronacher.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Fixer for reduce().
  4. Makes sure reduce() is imported from the functools module if reduce is
  5. used in that module.
  6. """
  7. from lib2to3 import fixer_base
  8. from lib2to3.fixer_util import touch_import
  9. class FixReduce(fixer_base.BaseFix):
  10. BM_compatible = True
  11. order = "pre"
  12. PATTERN = """
  13. power< 'reduce'
  14. trailer< '('
  15. arglist< (
  16. (not(argument<any '=' any>) any ','
  17. not(argument<any '=' any>) any) |
  18. (not(argument<any '=' any>) any ','
  19. not(argument<any '=' any>) any ','
  20. not(argument<any '=' any>) any)
  21. ) >
  22. ')' >
  23. >
  24. """
  25. def transform(self, node, results):
  26. touch_import('functools', 'reduce', node)