From ebb31dc29bd3ad47ccdc8f975cb598555a77d19d Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 3 Jan 2023 15:12:09 +0200 Subject: [PATCH] Fix PT006 autofix of parametrize name strings like `' first, , second '` (#1591) --- .../fixtures/flake8_pytest_style/PT006.py | 5 +++ .../plugins/parametrize.rs | 14 ++++++- ...flake8_pytest_style__tests__PT006_csv.snap | 24 ++++++------ ...e8_pytest_style__tests__PT006_default.snap | 39 +++++++++++++------ ...lake8_pytest_style__tests__PT006_list.snap | 33 ++++++++++++---- 5 files changed, 83 insertions(+), 32 deletions(-) diff --git a/resources/test/fixtures/flake8_pytest_style/PT006.py b/resources/test/fixtures/flake8_pytest_style/PT006.py index 55d3a1a0e6..2f007ec5a5 100644 --- a/resources/test/fixtures/flake8_pytest_style/PT006.py +++ b/resources/test/fixtures/flake8_pytest_style/PT006.py @@ -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): ... diff --git a/src/flake8_pytest_style/plugins/parametrize.rs b/src/flake8_pytest_style/plugins/parametrize.rs index 5959cc1b42..8b423184af 100644 --- a/src/flake8_pytest_style/plugins/parametrize.rs +++ b/src/flake8_pytest_style/plugins/parametrize.rs @@ -22,7 +22,19 @@ fn check_names(checker: &mut Checker, expr: &Expr) { value: Constant::Str(string), .. } => { - let names = string.split(',').collect::>(); + // 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::>(); if names.len() > 1 { match names_type { diff --git a/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_csv.snap b/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_csv.snap index f5031e4648..a1f6189684 100644 --- a/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_csv.snap +++ b/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_csv.snap @@ -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: ~ diff --git a/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_default.snap b/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_default.snap index d573540495..2ab4cab00d 100644 --- a/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_default.snap +++ b/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_default.snap @@ -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: ~ diff --git a/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_list.snap b/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_list.snap index aca878ce02..7094b9b875 100644 --- a/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_list.snap +++ b/src/flake8_pytest_style/snapshots/ruff__flake8_pytest_style__tests__PT006_list.snap @@ -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: ~