1. Joe Amenta
  2. lib3to2

Commits

Joe Amenta  committed 9209a42

Run after function annotations are removed, and make the kwargs dict appear to the client code as though 3to2 never touched it.

  • Participants
  • Parent commits 4b2f407
  • Branches default

Comments (0)

Files changed (2)

File lib3to2/fixes/fix_kwargs.py

View file
  • Ignore whitespace
 from lib2to3 import fixer_base
 from ..fixer_util import token, indentation, suitify, String, Newline, Comma, DoubleStar, Name
 
-_assign_template = "%(name)s = %(kwargs)s['%(name)s']"
+_assign_template = "%(name)s = %(kwargs)s['%(name)s']; del %(kwargs)s['%(name)s']"
 _if_template = "if '%(name)s' in %(kwargs)s: %(assign)s"
 _else_template = "else: %(name)s = %(default)s"
 _kwargs_default_name = "_3to2kwargs"
 
 class FixKwargs(fixer_base.BaseFix):
 
-    explicit = True # not sufficiently tested
-    
+    run_order = 7 # Run after function annotations are removed
+
     PATTERN = "funcdef< 'def' NAME parameters< '(' arglist=typedargslist< params=any* > ')' > ':' suite=any >"
 
     def transform(self, node, results):

File lib3to2/tests/test_kwargs.py

View file
  • Ignore whitespace
             funky()"""
         a = """
         def spam(ham, *args, **_3to2kwargs):
-            monkeys = _3to2kwargs['monkeys']
-            eggs = _3to2kwargs['eggs']
+            monkeys = _3to2kwargs['monkeys']; del _3to2kwargs['monkeys']
+            eggs = _3to2kwargs['eggs']; del _3to2kwargs['eggs']
             funky()"""
         self.check(b, a)
 
             funky()"""
         a = """
         def spam(ham, *args, **stuff):
-            monkeys = stuff['monkeys']
-            eggs = stuff['eggs']
+            monkeys = stuff['monkeys']; del stuff['monkeys']
+            eggs = stuff['eggs']; del stuff['eggs']
             funky()"""
         self.check(b, a)
 
             funky()"""
         a = """
         def spam(ham, **_3to2kwargs):
-            monkeys = _3to2kwargs['monkeys']
-            eggs = _3to2kwargs['eggs']
+            monkeys = _3to2kwargs['monkeys']; del _3to2kwargs['monkeys']
+            eggs = _3to2kwargs['eggs']; del _3to2kwargs['eggs']
             funky()"""
         self.check(b, a)
 
             funky()"""
         a = """
         def spam(ham, **_3to2kwargs):
-            if 'monkeys' in _3to2kwargs: monkeys = _3to2kwargs['monkeys']
+            if 'monkeys' in _3to2kwargs: monkeys = _3to2kwargs['monkeys']; del _3to2kwargs['monkeys']
             else: monkeys = 2
-            if 'eggs' in _3to2kwargs: eggs = _3to2kwargs['eggs']
+            if 'eggs' in _3to2kwargs: eggs = _3to2kwargs['eggs']; del _3to2kwargs['eggs']
             else: eggs = 3
-            dinosaurs = _3to2kwargs['dinosaurs']
+            dinosaurs = _3to2kwargs['dinosaurs']; del _3to2kwargs['dinosaurs']
             funky()"""
         self.check(b, a)
 
             funky()"""
         a = """
         def spam(ham, **stuff):
-            if 'monkeys' in stuff: monkeys = stuff['monkeys']
+            if 'monkeys' in stuff: monkeys = stuff['monkeys']; del stuff['monkeys']
             else: monkeys = 2
-            if 'eggs' in stuff: eggs = stuff['eggs']
+            if 'eggs' in stuff: eggs = stuff['eggs']; del stuff['eggs']
             else: eggs = 3
-            dinosaurs = stuff['dinosaurs']
+            dinosaurs = stuff['dinosaurs']; del stuff['dinosaurs']
             funky()"""
         self.check(b, a)
         
             funky()"""
         a = """
         def spam(ham, **_3to2kwargs):
-            if 'monkeys' in _3to2kwargs: monkeys = _3to2kwargs['monkeys']
+            if 'monkeys' in _3to2kwargs: monkeys = _3to2kwargs['monkeys']; del _3to2kwargs['monkeys']
             else: monkeys = [i.split() for i in something(args)]
-            if 'eggs' in _3to2kwargs: eggs = _3to2kwargs['eggs']
+            if 'eggs' in _3to2kwargs: eggs = _3to2kwargs['eggs']; del _3to2kwargs['eggs']
             else: eggs = call_fn(lambda a: b)
-            dinosaurs = _3to2kwargs['dinosaurs']
+            dinosaurs = _3to2kwargs['dinosaurs']; del _3to2kwargs['dinosaurs']
             funky()"""
         self.check(b, a)
 
             funky()"""
         a = """
         def spam(ham, **stuff):
-            if 'monkeys' in stuff: monkeys = stuff['monkeys']
+            if 'monkeys' in stuff: monkeys = stuff['monkeys']; del stuff['monkeys']
             else: monkeys = [i.split() for i in something(args)]
-            if 'eggs' in stuff: eggs = stuff['eggs']
+            if 'eggs' in stuff: eggs = stuff['eggs']; del stuff['eggs']
             else: eggs = call_fn(lambda a: b)
-            dinosaurs = stuff['dinosaurs']
+            dinosaurs = stuff['dinosaurs']; del stuff['dinosaurs']
             funky()"""
         self.check(b, a)