Skip to content

Commit

Permalink
Merge pull request #529 from asottile/percent-format-no-string-widths
Browse files Browse the repository at this point in the history
don't rewrite %-format with width+s
  • Loading branch information
asottile committed Sep 10, 2021
2 parents 6b2d0d8 + 7931123 commit e43a16c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pyupgrade/_plugins/percent_format.py
Expand Up @@ -128,8 +128,6 @@ def _handle_part(part: PercentFormat) -> str:
if conversion == '%':
return s + '%'
parts = [s, '{']
if width and conversion == 's' and not conversion_flag:
conversion_flag = '>'
if conversion == 's':
conversion = ''
if key:
Expand Down Expand Up @@ -276,6 +274,9 @@ def visit_BinOp(
# no equivalent in format
if conversion in {'a', 'r'} and nontrivial_fmt:
break
# %s with None and width is not supported
if width and conversion == 's':
break
# all dict substitutions must be named
if isinstance(node.right, ast.Dict) and not key:
break
Expand Down
5 changes: 3 additions & 2 deletions tests/features/percent_format_test.py
Expand Up @@ -158,6 +158,7 @@ def test_simplify_conversion_flag(s, expected):
'"%4%" % ()',
# no equivalent in format specifier
'"%.2r" % (1.25)', '"%.2a" % (1.25)',
pytest.param('"%8s" % (None,)', id='unsafe width-string conversion'),
# non-string mod
'i % 3',
# dict format but not keyed arguments
Expand Down Expand Up @@ -208,8 +209,8 @@ def test_percent_format_noop_if_bug_16806():
('"%s" % ("%s" % ("nested",),)', '"{}".format("{}".format("nested"))'),
('"%s%% percent" % (15,)', '"{}% percent".format(15)'),
('"%3f" % (15,)', '"{:3f}".format(15)'),
('"%-5s" % ("hi",)', '"{:<5}".format("hi")'),
('"%9s" % (5,)', '"{:>9}".format(5)'),
('"%-5f" % (5,)', '"{:<5f}".format(5)'),
('"%9f" % (5,)', '"{:9f}".format(5)'),
('"brace {} %s" % (1,)', '"brace {{}} {}".format(1)'),
(
'"%s" % (\n'
Expand Down

0 comments on commit e43a16c

Please sign in to comment.