Skip to content

Commit

Permalink
dont rewrite universal_newlines if text or **kwargs present
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Mar 13, 2022
1 parent d71f806 commit 7a6d4d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pyupgrade/_plugins/subprocess_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def visit_Call(
stdout_idx = None
stderr_idx = None
universal_newlines_idx = None
skip_universal_newlines_rewrite = False
for n, keyword in enumerate(node.keywords):
if keyword.arg == 'stdout' and is_name_attr(
keyword.value,
Expand All @@ -96,7 +97,12 @@ def visit_Call(
stderr_idx = n
elif keyword.arg == 'universal_newlines':
universal_newlines_idx = n
if universal_newlines_idx is not None:
elif keyword.arg == 'text' or keyword.arg is None:
skip_universal_newlines_rewrite = True
if (
universal_newlines_idx is not None and
not skip_universal_newlines_rewrite
):
func = functools.partial(
_replace_universal_newlines_with_text,
arg_idx=len(node.args) + universal_newlines_idx,
Expand Down
20 changes: 20 additions & 0 deletions tests/features/universal_newlines_to_text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@
(3, 7),
id='universal_newlines not used',
),
pytest.param(
'import subprocess\n'
'subprocess.run(\n'
' ["foo"],\n'
' text=True,\n'
' universal_newlines=True\n'
')\n',
(3, 7),
id='both text and universal_newlines',
),
pytest.param(
'import subprocess\n'
'subprocess.run(\n'
' ["foo"],\n'
' universal_newlines=True,\n'
' **kwargs,\n'
')\n',
(3, 7),
id='both **kwargs and universal_newlines',
),
),
)
def test_fix_universal_newlines_to_text_noop(s, version):
Expand Down

0 comments on commit 7a6d4d7

Please sign in to comment.