Repository: asottile/pyupgrade

Issue:
Add the ability to disable specific upgrades
E.g. our style guide forces printf-like syntax (we switch to C-like languages often and cut the corners) and we have to revert the corresponding upgrades as in https://github.com/src-d/ml/pull/322#pullrequestreview-162382042

So would be nice to enable/disable features besides the master switch by Python version. 

Discussion:

Patch:
diff --git a/README.md b/README.md
index 7612556..7285a1c 100644
--- a/README.md
+++ b/README.md
@@ -56,6 +56,9 @@ dict([(a, b) for a, b in y)  # {a: b for a, b in y}
 
 ### printf-style string formatting
 
+Availability:
+- Unless `--keep-percent-format` is passed.
+
 ```python
 '%s %s' % (a, b)                  # '{} {}'.format(a, b)
 '%r %2f' % (a, b)                 # '{!r} {:2f}'.format(a, b)
diff --git a/pyupgrade.py b/pyupgrade.py
index 08fd6c2..a228b56 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -1114,7 +1114,8 @@ def fix_file(filename, args):
     contents_text = _fix_unicode_literals(contents_text, args.py3_plus)
     contents_text = _fix_long_literals(contents_text)
     contents_text = _fix_octal_literals(contents_text)
-    contents_text = _fix_percent_format(contents_text)
+    if not args.keep_percent_format:
+        contents_text = _fix_percent_format(contents_text)
     if args.py3_plus:
         contents_text = _fix_super(contents_text)
         contents_text = _fix_new_style_classes(contents_text)
@@ -1134,6 +1135,7 @@ def fix_file(filename, args):
 def main(argv=None):
     parser = argparse.ArgumentParser()
     parser.add_argument('filenames', nargs='*')
+    parser.add_argument('--keep-percent-format', action='store_true')
     parser.add_argument('--py3-plus', '--py3-only', action='store_true')
     parser.add_argument('--py36-plus', action='store_true')
     args = parser.parse_args(argv)


Test patch:
diff --git a/tests/pyupgrade_test.py b/tests/pyupgrade_test.py
index a30205a..ed20753 100644
--- a/tests/pyupgrade_test.py
+++ b/tests/pyupgrade_test.py
@@ -994,6 +994,15 @@ def test_main_non_utf8_bytes(tmpdir, capsys):
     assert out == '{} is non-utf-8 (not supported)\n'.format(f.strpath)
 
 
+def test_keep_percent_format(tmpdir):
+    f = tmpdir.join('f.py')
+    f.write('"%s" % (1,)')
+    assert main((f.strpath, '--keep-percent-format')) == 0
+    assert f.read() == '"%s" % (1,)'
+    assert main((f.strpath,)) == 1
+    assert f.read() == '"{}".format(1)'
+
+
 def test_py3_plus_argument_unicode_literals(tmpdir):
     f = tmpdir.join('f.py')
     f.write('u""')