From 9f9f25ff7ce93030a8ce2ddb4016a6d07e9a67eb Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 8 Feb 2023 11:13:24 -0500 Subject: [PATCH] Accommodate multiple `@pytest.mark.parametrize` decorators (#2662) --- .../fixtures/flake8_pytest_style/PT007.py | 6 +++ .../flake8_pytest_style/rules/parametrize.rs | 45 +++++++++---------- ...est_style__tests__PT007_list_of_lists.snap | 38 +++++++++++++++- ...st_style__tests__PT007_list_of_tuples.snap | 14 +++++- ...st_style__tests__PT007_tuple_of_lists.snap | 38 +++++++++++++++- ...t_style__tests__PT007_tuple_of_tuples.snap | 14 +++++- 6 files changed, 126 insertions(+), 29 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT007.py b/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT007.py index 11021d60e9..af49e0389a 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT007.py +++ b/crates/ruff/resources/test/fixtures/flake8_pytest_style/PT007.py @@ -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 diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs index 7119c65dc6..f65628bd30 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -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 { 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); + } } } } diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_lists.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_lists.snap index 2380120e83..ddf693b88d 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_lists.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_lists.snap @@ -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: ~ diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_tuples.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_tuples.snap index 1bb5a55844..02c9c826b9 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_tuples.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_list_of_tuples.snap @@ -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: ~ diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_lists.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_lists.snap index 488e266183..1e4428c7d3 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_lists.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_lists.snap @@ -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: ~ diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_tuples.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_tuples.snap index 83380d8c64..ca0cef9196 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_tuples.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT007_tuple_of_tuples.snap @@ -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: ~