[PATCH 6 of 6] byteify-strings: add --treat-as-kwargs argument to handle kwargs-like objects

Raphaël Gomès raphael.gomes at octobus.net
Fri Aug 2 04:22:17 EDT 2019


# HG changeset patch
# User Raphaël Gomès <rgomes at octobus.net>
# Date 1564733902 -7200
#      Fri Aug 02 10:18:22 2019 +0200
# Node ID e6e13ca43b23038dd34e2c35b799f68868674f4b
# Parent  d28c8bac8d4bc1eddc4a899eb081563e4823f6a6
# EXP-Topic byteify-strings
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.

diff -r d28c8bac8d4b -r e6e13ca43b23 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
@@ -230,6 +230,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, '==')):
@@ -272,10 +278,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:


More information about the Mercurial-devel mailing list