byteify-strings: add --treat-as-kwargs argument to handle kwargs-like objects
authorRaphaël Gomès <rgomes@octobus.net>
Fri, 02 Aug 2019 10:18:22 +0200
changeset 42678 f95b59ffc307
parent 42677 c9fd8163131f
child 42679 5e296f618920
byteify-strings: add --treat-as-kwargs argument to handle kwargs-like objects This argument will help extensions move to Python 3 as keyword arguments should not be byte-prefixed. Most of the time, code bases will call this object `kwargs`, but other conventions exist like `opts`, so it should make sense to allow for custom names. This is a best effort solution that does minimal static checking; cases like `options = [o for o in ('a', 'b', 'c') if kwargs.get(o)]` and other just as complicated will not be detected.
contrib/byteify-strings.py
--- a/contrib/byteify-strings.py	Fri Aug 02 10:10:23 2019 +0200
+++ b/contrib/byteify-strings.py	Fri Aug 02 10:18:22 2019 +0200
@@ -228,6 +228,12 @@
                 yield adjusttokenpos(t._replace(string=fn[4:]), coloffset)
                 continue
 
+        if t.type == token.NAME and t.string in opts['treat-as-kwargs']:
+            if _isitemaccess(i):
+                _ensuresysstr(i + 2)
+            if _ismethodcall(i, 'get', 'pop', 'setdefault', 'popitem'):
+                _ensuresysstr(i + 4)
+
         # Looks like "if __name__ == '__main__'".
         if (t.type == token.NAME and t.string == '__name__'
             and _isop(i + 1, '==')):
@@ -270,10 +276,15 @@
                     help='edit files in place')
     ap.add_argument('--dictiter', action='store_true', default=False,
                     help='rewrite iteritems() and itervalues()'),
+    ap.add_argument('--treat-as-kwargs', nargs="+",
+                    help="ignore kwargs-like objects"),
     ap.add_argument('files', metavar='FILE', nargs='+', help='source file')
     args = ap.parse_args()
     opts = {
         'dictiter': args.dictiter,
+        'treat-as-kwargs': set(
+            args.treat_as_kwargs
+        ) if args.treat_as_kwargs else set()
     }
     for fname in args.files:
         if args.inplace: