Repository: asottile/pyupgrade

Issue:
Behaviour change after upgrading '%9s' to '{:9}'
This pyupgrade change (https://github.com/hugovk/yamllint/commit/25e8e9be0838bff930b3b204360fc24385b6edbf) fails indentation-related unit tests which previously passed:

```diff
-            output += '%9s %s\n' % (token_type,
+            output += '{:9} {}\n'.format(token_type,
                                     self.format_stack(context['stack']))
```


https://travis-ci.org/hugovk/yamllint/jobs/459076859#L860

Small example:

```python
Python 3.7.1 (default, Nov  6 2018, 18:45:35)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> '%9s %s\n' % ("abc", "def")
'      abc def\n'
>>> '{:9} {}\n'.format("abc", "def")
'abc       def\n'
```

It would have worked if they were integers:
```python
>>> '%9s %s\n' % (123, 456)
'      123 456\n'
>>> '{:9} {}\n'.format(123, 456)
'      123 456\n'
```

---

Another issue with this example, it'd be nice if the indentation of the second line was adjusted, as this often causes flake8 errors. But I understand if this is out of scope of pyupgrade.

For example, instead of this:

```diff
-            output += '%9s %s\n' % (token_type,
+            output += '{:9} {}\n'.format(token_type,
                                     self.format_stack(context['stack']))
```

Do this:

```diff
-            output += '%9s %s\n' % (token_type,
-                                    self.format_stack(context['stack']))
+            output += '{:9} {}\n'.format(token_type,
+                                         self.format_stack(context['stack']))
```



Discussion:

Patch:
diff --git a/pyupgrade.py b/pyupgrade.py
index 66e1094..18345dc 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -609,6 +609,8 @@ def _percent_to_format(s):
             if conversion == '%':
                 return s + '%'
             parts = [s, '{']
+            if width and conversion == 's' and not conversion_flag:
+                conversion_flag = '>'
             if conversion == 's':
                 conversion = None
             if key:


Test patch:
diff --git a/tests/pyupgrade_test.py b/tests/pyupgrade_test.py
index ad9d751..3818f20 100644
--- a/tests/pyupgrade_test.py
+++ b/tests/pyupgrade_test.py
@@ -615,6 +615,7 @@ def test_percent_format_noop_if_bug_16806():
         ('"%s%% percent" % (15,)', '"{}% percent".format(15)'),
         ('"%3f" % (15,)', '"{:3f}".format(15)'),
         ('"%-5s" % ("hi",)', '"{:<5}".format("hi")'),
+        ('"%9s" % (5,)', '"{:>9}".format(5)'),
         ('"brace {} %s" % (1,)', '"brace {{}} {}".format(1)'),
         (
             '"%s" % (\n'