Accommodate multiple @pytest.mark.parametrize decorators (#2662)

This commit is contained in:
Charlie Marsh 2023-02-08 11:13:24 -05:00 committed by GitHub
parent 9cd1bf9c03
commit 9f9f25ff7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 126 additions and 29 deletions

View file

@ -75,3 +75,9 @@ def test_csv_name_list_of_lists(param1, param2):
)
def test_single_list_of_lists(param):
...
@pytest.mark.parametrize("a", [1, 2])
@pytest.mark.parametrize(("b", "c"), ((3, 4), (5, 6)))
def test_multiple_decorators(a, b, c):
pass

View file

@ -43,12 +43,6 @@ impl Violation for ParametrizeValuesWrongType {
}
}
fn get_parametrize_decorator<'a>(checker: &Checker, decorators: &'a [Expr]) -> Option<&'a Expr> {
decorators
.iter()
.find(|decorator| is_pytest_parametrize(decorator, checker))
}
fn elts_to_csv(elts: &[Expr], checker: &Checker) -> Option<String> {
let all_literals = elts.iter().all(|e| {
matches!(
@ -377,26 +371,27 @@ fn handle_value_rows(
}
pub fn parametrize(checker: &mut Checker, decorators: &[Expr]) {
let decorator = get_parametrize_decorator(checker, decorators);
if let Some(decorator) = decorator {
if let ExprKind::Call { args, .. } = &decorator.node {
if checker
.settings
.rules
.enabled(&Rule::ParametrizeNamesWrongType)
{
if let Some(names) = args.get(0) {
check_names(checker, names);
for decorator in decorators {
if is_pytest_parametrize(decorator, checker) {
if let ExprKind::Call { args, .. } = &decorator.node {
if checker
.settings
.rules
.enabled(&Rule::ParametrizeNamesWrongType)
{
if let Some(names) = args.get(0) {
check_names(checker, names);
}
}
}
if checker
.settings
.rules
.enabled(&Rule::ParametrizeValuesWrongType)
{
if let Some(names) = args.get(0) {
if let Some(values) = args.get(1) {
check_values(checker, names, values);
if checker
.settings
.rules
.enabled(&Rule::ParametrizeValuesWrongType)
{
if let Some(names) = args.get(0) {
if let Some(values) = args.get(1) {
check_values(checker, names, values);
}
}
}
}

View file

@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@ -86,4 +86,40 @@ expression: diagnostics
column: 14
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: list
row: list
location:
row: 81
column: 37
end_location:
row: 81
column: 53
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: list
row: list
location:
row: 81
column: 38
end_location:
row: 81
column: 44
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: list
row: list
location:
row: 81
column: 46
end_location:
row: 81
column: 52
fix: ~
parent: ~

View file

@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@ -110,4 +110,16 @@ expression: diagnostics
column: 14
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: list
row: tuple
location:
row: 81
column: 37
end_location:
row: 81
column: 53
fix: ~
parent: ~

View file

@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@ -110,4 +110,40 @@ expression: diagnostics
column: 5
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: tuple
row: list
location:
row: 80
column: 30
end_location:
row: 80
column: 36
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: tuple
row: list
location:
row: 81
column: 38
end_location:
row: 81
column: 44
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: tuple
row: list
location:
row: 81
column: 46
end_location:
row: 81
column: 52
fix: ~
parent: ~

View file

@ -1,5 +1,5 @@
---
source: src/rules/flake8_pytest_style/mod.rs
source: crates/ruff/src/rules/flake8_pytest_style/mod.rs
expression: diagnostics
---
- kind:
@ -134,4 +134,16 @@ expression: diagnostics
column: 5
fix: ~
parent: ~
- kind:
ParametrizeValuesWrongType:
values: tuple
row: tuple
location:
row: 80
column: 30
end_location:
row: 80
column: 36
fix: ~
parent: ~