mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:39:13 +00:00
Do not Change Quotation Style for PT006
Autofix (#1600)
This commit is contained in:
parent
e9a236f740
commit
d4d67e3014
5 changed files with 158 additions and 74 deletions
|
@ -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)])
|
||||
def test_tuple(param1, param2):
|
||||
...
|
||||
|
|
|
@ -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 crate::ast::helpers::create_expr;
|
||||
use crate::ast::types::Range;
|
||||
use crate::autofix::Fix;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::flake8_pytest_style::types;
|
||||
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> {
|
||||
decorators
|
||||
|
@ -44,12 +47,40 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
|
|||
Range::from_located(expr),
|
||||
);
|
||||
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(
|
||||
strings_to_python_tuple(&names),
|
||||
content,
|
||||
expr.location,
|
||||
expr.end_location.unwrap(),
|
||||
));
|
||||
}
|
||||
Err(e) => error!(
|
||||
"Failed to fix wrong name(s) type in \
|
||||
`@pytest.mark.parametrize`: {e}"
|
||||
),
|
||||
};
|
||||
}
|
||||
checker.add_check(check);
|
||||
}
|
||||
types::ParametrizeNameType::List => {
|
||||
|
@ -58,12 +89,40 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
|
|||
Range::from_located(expr),
|
||||
);
|
||||
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(
|
||||
strings_to_python_list(&names),
|
||||
content,
|
||||
expr.location,
|
||||
expr.end_location.unwrap(),
|
||||
));
|
||||
}
|
||||
Err(e) => error!(
|
||||
"Failed to fix wrong name(s) type in \
|
||||
`@pytest.mark.parametrize`: {e}"
|
||||
),
|
||||
};
|
||||
}
|
||||
checker.add_check(check);
|
||||
}
|
||||
types::ParametrizeNameType::CSV => {}
|
||||
|
@ -135,42 +194,28 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) {
|
|||
CheckKind::ParametrizeNamesWrongType(types::ParametrizeNameType::CSV),
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if let ExprKind::Constant {
|
||||
value: Constant::Str(string),
|
||||
..
|
||||
} = &value.node
|
||||
{
|
||||
|
||||
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(
|
||||
format!("\"{string}\""),
|
||||
content,
|
||||
expr.location,
|
||||
expr.end_location.unwrap(),
|
||||
));
|
||||
}
|
||||
Err(e) => error!("Failed to fix wrong name(s) type in `@pytest.mark.parametrize`: {e}"),
|
||||
};
|
||||
}
|
||||
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(
|
||||
checker: &mut Checker,
|
||||
elts: &[Expr],
|
||||
|
|
|
@ -5,55 +5,55 @@ expression: checks
|
|||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 19
|
||||
row: 24
|
||||
column: 25
|
||||
end_location:
|
||||
row: 19
|
||||
row: 24
|
||||
column: 45
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 25
|
||||
end_location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 36
|
||||
fix:
|
||||
content: "\"param1\""
|
||||
location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 25
|
||||
end_location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 36
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 29
|
||||
row: 34
|
||||
column: 25
|
||||
end_location:
|
||||
row: 29
|
||||
row: 34
|
||||
column: 45
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 25
|
||||
end_location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 35
|
||||
fix:
|
||||
content: "\"param1\""
|
||||
location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 25
|
||||
end_location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 35
|
||||
parent: ~
|
||||
|
||||
|
|
|
@ -37,47 +37,64 @@ expression: checks
|
|||
column: 56
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
ParametrizeNamesWrongType: tuple
|
||||
location:
|
||||
row: 24
|
||||
row: 19
|
||||
column: 25
|
||||
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
|
||||
fix:
|
||||
content: "\"param1\""
|
||||
location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 25
|
||||
end_location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 36
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: tuple
|
||||
location:
|
||||
row: 29
|
||||
row: 34
|
||||
column: 25
|
||||
end_location:
|
||||
row: 29
|
||||
row: 34
|
||||
column: 45
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 25
|
||||
end_location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 35
|
||||
fix:
|
||||
content: "\"param1\""
|
||||
location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 25
|
||||
end_location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 35
|
||||
parent: ~
|
||||
|
||||
|
|
|
@ -43,41 +43,58 @@ expression: checks
|
|||
column: 25
|
||||
end_location:
|
||||
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
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 25
|
||||
end_location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 36
|
||||
fix:
|
||||
content: "\"param1\""
|
||||
location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 25
|
||||
end_location:
|
||||
row: 24
|
||||
row: 29
|
||||
column: 36
|
||||
parent: ~
|
||||
- kind:
|
||||
ParametrizeNamesWrongType: csv
|
||||
location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 25
|
||||
end_location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 35
|
||||
fix:
|
||||
content: "\"param1\""
|
||||
location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 25
|
||||
end_location:
|
||||
row: 34
|
||||
row: 39
|
||||
column: 35
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue