Do not Change Quotation Style for PT006 Autofix (#1600)

This commit is contained in:
Maksudul Haque 2023-01-03 21:16:49 +06:00 committed by GitHub
parent e9a236f740
commit d4d67e3014
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 158 additions and 74 deletions

View file

@ -16,6 +16,11 @@ def test_csv_with_whitespace(param1, param2):
... ...
@pytest.mark.parametrize("param1,param2", [(1, 2), (3, 4)])
def test_csv_bad_quotes(param1, param2):
...
@pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)]) @pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
def test_tuple(param1, param2): def test_tuple(param1, param2):
... ...

View file

@ -1,11 +1,14 @@
use rustpython_ast::{Constant, Expr, ExprKind}; use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind};
use super::helpers::is_pytest_parametrize; use super::helpers::is_pytest_parametrize;
use crate::ast::helpers::create_expr;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::autofix::Fix; use crate::autofix::Fix;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::flake8_pytest_style::types; use crate::flake8_pytest_style::types;
use crate::registry::{Check, CheckCode, CheckKind}; use crate::registry::{Check, CheckCode, CheckKind};
use crate::source_code_generator::SourceCodeGenerator;
fn get_parametrize_decorator<'a>(checker: &Checker, decorators: &'a [Expr]) -> Option<&'a Expr> { fn get_parametrize_decorator<'a>(checker: &Checker, decorators: &'a [Expr]) -> Option<&'a Expr> {
decorators decorators
@ -44,12 +47,40 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(check.kind.code()) { if checker.patch(check.kind.code()) {
let mut generator = SourceCodeGenerator::new(
checker.style.indentation(),
checker.style.quote(),
checker.style.line_ending(),
);
generator.unparse_expr(
&create_expr(ExprKind::Tuple {
elts: names
.iter()
.map(|&name| {
create_expr(ExprKind::Constant {
value: Constant::Str(name.to_string()),
kind: None,
})
})
.collect(),
ctx: ExprContext::Load,
}),
1,
);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement( check.amend(Fix::replacement(
strings_to_python_tuple(&names), content,
expr.location, expr.location,
expr.end_location.unwrap(), expr.end_location.unwrap(),
)); ));
} }
Err(e) => error!(
"Failed to fix wrong name(s) type in \
`@pytest.mark.parametrize`: {e}"
),
};
}
checker.add_check(check); checker.add_check(check);
} }
types::ParametrizeNameType::List => { types::ParametrizeNameType::List => {
@ -58,12 +89,40 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(check.kind.code()) { if checker.patch(check.kind.code()) {
let mut generator = SourceCodeGenerator::new(
checker.style.indentation(),
checker.style.quote(),
checker.style.line_ending(),
);
generator.unparse_expr(
&create_expr(ExprKind::List {
elts: names
.iter()
.map(|&name| {
create_expr(ExprKind::Constant {
value: Constant::Str(name.to_string()),
kind: None,
})
})
.collect(),
ctx: ExprContext::Load,
}),
0,
);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement( check.amend(Fix::replacement(
strings_to_python_list(&names), content,
expr.location, expr.location,
expr.end_location.unwrap(), expr.end_location.unwrap(),
)); ));
} }
Err(e) => error!(
"Failed to fix wrong name(s) type in \
`@pytest.mark.parametrize`: {e}"
),
};
}
checker.add_check(check); checker.add_check(check);
} }
types::ParametrizeNameType::CSV => {} types::ParametrizeNameType::CSV => {}
@ -135,42 +194,28 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) {
CheckKind::ParametrizeNamesWrongType(types::ParametrizeNameType::CSV), CheckKind::ParametrizeNamesWrongType(types::ParametrizeNameType::CSV),
Range::from_located(expr), Range::from_located(expr),
); );
if let ExprKind::Constant {
value: Constant::Str(string),
..
} = &value.node
{
if checker.patch(check.kind.code()) { if checker.patch(check.kind.code()) {
let mut generator = SourceCodeGenerator::new(
checker.style.indentation(),
checker.style.quote(),
checker.style.line_ending(),
);
generator.unparse_expr(&create_expr(value.node.clone()), 0);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement( check.amend(Fix::replacement(
format!("\"{string}\""), content,
expr.location, expr.location,
expr.end_location.unwrap(), expr.end_location.unwrap(),
)); ));
} }
Err(e) => error!("Failed to fix wrong name(s) type in `@pytest.mark.parametrize`: {e}"),
};
} }
checker.add_check(check); checker.add_check(check);
} }
fn strings_to_python_tuple(strings: &[&str]) -> String {
let result = strings
.iter()
.map(|s| format!("\"{s}\""))
.collect::<Vec<String>>()
.join(", ");
format!("({result})")
}
fn strings_to_python_list(strings: &[&str]) -> String {
let result = strings
.iter()
.map(|s| format!("\"{s}\""))
.collect::<Vec<String>>()
.join(", ");
format!("[{result}]")
}
fn handle_value_rows( fn handle_value_rows(
checker: &mut Checker, checker: &mut Checker,
elts: &[Expr], elts: &[Expr],

View file

@ -5,55 +5,55 @@ expression: checks
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 19 row: 24
column: 25 column: 25
end_location: end_location:
row: 19 row: 24
column: 45 column: 45
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 24 row: 29
column: 25 column: 25
end_location: end_location:
row: 24 row: 29
column: 36 column: 36
fix: fix:
content: "\"param1\"" content: "\"param1\""
location: location:
row: 24 row: 29
column: 25 column: 25
end_location: end_location:
row: 24 row: 29
column: 36 column: 36
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 29 row: 34
column: 25 column: 25
end_location: end_location:
row: 29 row: 34
column: 45 column: 45
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 34 row: 39
column: 25 column: 25
end_location: end_location:
row: 34 row: 39
column: 35 column: 35
fix: fix:
content: "\"param1\"" content: "\"param1\""
location: location:
row: 34 row: 39
column: 25 column: 25
end_location: end_location:
row: 34 row: 39
column: 35 column: 35
parent: ~ parent: ~

View file

@ -37,47 +37,64 @@ expression: checks
column: 56 column: 56
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: tuple
location: location:
row: 24 row: 19
column: 25 column: 25
end_location: end_location:
row: 24 row: 19
column: 40
fix:
content: "(\"param1\", \"param2\")"
location:
row: 19
column: 25
end_location:
row: 19
column: 40
parent: ~
- kind:
ParametrizeNamesWrongType: csv
location:
row: 29
column: 25
end_location:
row: 29
column: 36 column: 36
fix: fix:
content: "\"param1\"" content: "\"param1\""
location: location:
row: 24 row: 29
column: 25 column: 25
end_location: end_location:
row: 24 row: 29
column: 36 column: 36
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: tuple ParametrizeNamesWrongType: tuple
location: location:
row: 29 row: 34
column: 25 column: 25
end_location: end_location:
row: 29 row: 34
column: 45 column: 45
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 34 row: 39
column: 25 column: 25
end_location: end_location:
row: 34 row: 39
column: 35 column: 35
fix: fix:
content: "\"param1\"" content: "\"param1\""
location: location:
row: 34 row: 39
column: 25 column: 25
end_location: end_location:
row: 34 row: 39
column: 35 column: 35
parent: ~ parent: ~

View file

@ -43,41 +43,58 @@ expression: checks
column: 25 column: 25
end_location: end_location:
row: 19 row: 19
column: 40
fix:
content: "[\"param1\", \"param2\"]"
location:
row: 19
column: 25
end_location:
row: 19
column: 40
parent: ~
- kind:
ParametrizeNamesWrongType: list
location:
row: 24
column: 25
end_location:
row: 24
column: 45 column: 45
fix: ~ fix: ~
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 24 row: 29
column: 25 column: 25
end_location: end_location:
row: 24 row: 29
column: 36 column: 36
fix: fix:
content: "\"param1\"" content: "\"param1\""
location: location:
row: 24 row: 29
column: 25 column: 25
end_location: end_location:
row: 24 row: 29
column: 36 column: 36
parent: ~ parent: ~
- kind: - kind:
ParametrizeNamesWrongType: csv ParametrizeNamesWrongType: csv
location: location:
row: 34 row: 39
column: 25 column: 25
end_location: end_location:
row: 34 row: 39
column: 35 column: 35
fix: fix:
content: "\"param1\"" content: "\"param1\""
location: location:
row: 34 row: 39
column: 25 column: 25
end_location: end_location:
row: 34 row: 39
column: 35 column: 35
parent: ~ parent: ~