fix_xreadlines.py 689 B

12345678910111213141516171819202122232425
  1. """Fix "for x in f.xreadlines()" -> "for x in f".
  2. This fixer will also convert g(f.xreadlines) into g(f.__iter__)."""
  3. # Author: Collin Winter
  4. # Local imports
  5. from .. import fixer_base
  6. from ..fixer_util import Name
  7. class FixXreadlines(fixer_base.BaseFix):
  8. BM_compatible = True
  9. PATTERN = """
  10. power< call=any+ trailer< '.' 'xreadlines' > trailer< '(' ')' > >
  11. |
  12. power< any+ trailer< '.' no_call='xreadlines' > >
  13. """
  14. def transform(self, node, results):
  15. no_call = results.get("no_call")
  16. if no_call:
  17. no_call.replace(Name("__iter__", prefix=no_call.prefix))
  18. else:
  19. node.replace([x.clone() for x in results["call"]])