Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyUpgrade: Replace pipes with capture_output=True #1415

Merged
merged 11 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added fixes
  • Loading branch information
colin99d committed Dec 28, 2022
commit 5f735d3a20e8b3bbacbafea5165a6f567a63fab1
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ For more, see [pyupgrade](https://pypi.org/project/pyupgrade/3.2.0/) on PyPI.
| UP019 | TypingTextStrAlias | `typing.Text` is deprecated, use `str` | 🛠 |
| UP020 | OpenAlias | Use builtin `open` | 🛠 |
| UP021 | ReplaceUniversalNewlines | `universal_newlines` is deprecated, use `text` | 🛠 |
| UP022 | ReplaceStdoutStderr | Sending stdout and stderr to pipe is deprecated, use `capture_output` | 🛠 |

### pep8-naming (N)

Expand Down
10 changes: 10 additions & 0 deletions resources/test/fixtures/pyupgrade/UP022.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@
encoding="utf-8",
close_fds=True,
)

if output:
output = subprocess.run(
["foo"],
stdout=subprocess.PIPE,
check=True,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
)
1 change: 1 addition & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@
"UP02",
"UP020",
"UP021",
"UP022",
"W",
"W2",
"W29",
Expand Down
1 change: 1 addition & 0 deletions src/checks_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,7 @@ impl CheckCodePrefix {
CheckCode::UP019,
CheckCode::UP020,
CheckCode::UP021,
CheckCode::UP022,
],
CheckCodePrefix::UP00 => vec![
CheckCode::UP001,
Expand Down
23 changes: 18 additions & 5 deletions src/pyupgrade/plugins/replace_stdout_stderr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustpython_ast::{Expr, Keyword, KeywordData, Located};

use crate::ast::helpers::{find_keyword, match_module_member};
use crate::ast::types::Range;
use crate::ast::whitespace::indentation;
use crate::autofix::Fix;
use crate::checkers::ast::Checker;
use crate::checks::{Check, CheckKind};
Expand Down Expand Up @@ -34,10 +35,12 @@ fn dirty_count(iter: impl Iterator<Item = char>) -> usize {
}

fn clean_middle_args(checker: &Checker, range: &Range) -> MiddleContent {
let mut contents = checker.locator.slice_source_code_range(&range).to_string();
let mut contents = checker.locator.slice_source_code_range(range).to_string();
let is_multi_line = contents.contains('\n');
let start_gap = dirty_count(contents.chars());
if contents.len() == start_gap { return MiddleContent::new(None, false); }
if contents.len() == start_gap {
return MiddleContent::new(None, false);
}
for _ in 0..start_gap {
contents.remove(0);
}
Expand Down Expand Up @@ -82,13 +85,23 @@ pub fn replace_stdout_stderr(checker: &mut Checker, expr: &Expr, kwargs: &[Keywo
location: kwarg_vec.first().unwrap().end_location.unwrap(),
end_location: kwarg_vec.last().unwrap().location,
};
let middle_str = clean_middle_args(checker, &keep_range);
println!("{:?}\n", middle_str);
let indent_str = indentation(checker, kwarg_vec.first().unwrap());
let mut replace_str = String::from("capture_output=True");
let middle_content = clean_middle_args(checker, &keep_range);
if let Some(middle_str) = middle_content.content {
if middle_content.is_multi_line {
replace_str.push_str(",\n");
replace_str.push_str(&indent_str);
} else {
replace_str.push_str(", ");
}
replace_str.push_str(&middle_str);
}

let mut check = Check::new(CheckKind::ReplaceStdoutStderr, replace_range);
if checker.patch(check.kind.code()) {
check.amend(Fix::replacement(
"capture_output=True".to_string(),
replace_str,
replace_range.location,
replace_range.end_location,
));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
source: src/pyupgrade/mod.rs
expression: checks
---
- kind: ReplaceStdoutStderr
location:
row: 3
column: 33
end_location:
row: 3
column: 79
fix:
content: capture_output=True
location:
row: 3
column: 33
end_location:
row: 3
column: 79
- kind: ReplaceStdoutStderr
location:
row: 5
column: 24
end_location:
row: 5
column: 85
fix:
content: "capture_output=True, args=[\"foo\"]"
location:
row: 5
column: 24
end_location:
row: 5
column: 85
- kind: ReplaceStdoutStderr
location:
row: 8
column: 13
end_location:
row: 8
column: 71
fix:
content: "capture_output=True, check=True"
location:
row: 8
column: 13
end_location:
row: 8
column: 71
- kind: ReplaceStdoutStderr
location:
row: 12
column: 13
end_location:
row: 12
column: 71
fix:
content: "capture_output=True, check=True"
location:
row: 12
column: 13
end_location:
row: 12
column: 71
- kind: ReplaceStdoutStderr
location:
row: 17
column: 4
end_location:
row: 19
column: 26
fix:
content: "capture_output=True,\n check=True"
location:
row: 17
column: 4
end_location:
row: 19
column: 26
- kind: ReplaceStdoutStderr
location:
row: 28
column: 8
end_location:
row: 30
column: 30
fix:
content: "capture_output=True,\n check=True"
location:
row: 28
column: 8
end_location:
row: 30
column: 30