Home | History | Annotate | Download | only in fixes
      1 """
      2 Fixer that changes zip(seq0, seq1, ...) into list(zip(seq0, seq1, ...)
      3 unless there exists a 'from future_builtins import zip' statement in the
      4 top-level namespace.
      5 
      6 We avoid the transformation if the zip() call is directly contained in
      7 iter(<>), list(<>), tuple(<>), sorted(<>), ...join(<>), or for V in <>:.
      8 """
      9 
     10 # Local imports
     11 from .. import fixer_base
     12 from ..pytree import Node
     13 from ..pygram import python_symbols as syms
     14 from ..fixer_util import Name, ArgList, in_special_context
     15 
     16 
     17 class FixZip(fixer_base.ConditionalFix):
     18 
     19     BM_compatible = True
     20     PATTERN = """
     21     power< 'zip' args=trailer< '(' [any] ')' > [trailers=trailer*]
     22     >
     23     """
     24 
     25     skip_on = "future_builtins.zip"
     26 
     27     def transform(self, node, results):
     28         if self.should_skip(node):
     29             return
     30 
     31         if in_special_context(node):
     32             return None
     33 
     34         args = results['args'].clone()
     35         args.prefix = ""
     36 
     37         trailers = []
     38         if 'trailers' in results:
     39             trailers = [n.clone() for n in results['trailers']]
     40             for n in trailers:
     41                 n.prefix = ""
     42 
     43         new = Node(syms.power, [Name("zip"), args], prefix="")
     44         new = Node(syms.power, [Name("list"), ArgList([new])] + trailers)
     45         new.prefix = node.prefix
     46         return new
     47