Fix PT006 autofix of parametrize name strings like ' first, , second ' (#1591)

This commit is contained in:
Ran Benita 2023-01-03 15:12:09 +02:00 committed by GitHub
parent b9e92affb1
commit ebb31dc29b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 32 deletions

View file

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

View file

@ -22,7 +22,19 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
value: Constant::Str(string),
..
} => {
let names = string.split(',').collect::<Vec<&str>>();
// Match the following pytest code:
// [x.strip() for x in argnames.split(",") if x.strip()]
let names = string
.split(',')
.filter_map(|s| {
let trimmed = s.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed)
}
})
.collect::<Vec<&str>>();
if names.len() > 1 {
match names_type {

View file

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

View file

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

View file

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