[flake8-pytest-style] Stabilize "Detect more pytest.mark.parametrize calls" (PT006) (#15327)

Co-authored-by: Micha Reiser <micha@reiser.io>
Resolves #15324. Stabilizes the behavior changes introduced in #14515.
This commit is contained in:
InSync 2025-01-08 15:27:46 +07:00 committed by Micha Reiser
parent 1eda27d1a5
commit 8c620b9b4b
7 changed files with 89 additions and 498 deletions

View file

@ -879,13 +879,11 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
checker.diagnostics.push(diagnostic);
}
}
if checker.settings.preview.is_enabled()
&& checker.any_enabled(&[
if checker.any_enabled(&[
Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType,
Rule::PytestDuplicateParametrizeTestCases,
])
{
]) {
flake8_pytest_style::rules::parametrize(checker, call);
}
if checker.enabled(Rule::PytestUnittestAssertion) {

View file

@ -309,21 +309,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
body,
);
}
// In preview mode, calls are analyzed. To avoid duplicate diagnostics,
// skip analyzing the decorators.
if !checker.settings.preview.is_enabled()
&& checker.any_enabled(&[
Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType,
Rule::PytestDuplicateParametrizeTestCases,
])
{
for decorator in decorator_list {
if let Some(call) = decorator.expression.as_call_expr() {
flake8_pytest_style::rules::parametrize(checker, call);
}
}
}
if checker.any_enabled(&[
Rule::PytestIncorrectMarkParenthesesStyle,
Rule::PytestUseFixturesWithoutParameters,

View file

@ -312,22 +312,4 @@ mod tests {
assert_messages!("PT006_and_PT007", diagnostics);
Ok(())
}
#[test_case(Rule::PytestParametrizeNamesWrongType, Path::new("PT006.py"))]
fn test_pytest_style_preview(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
let diagnostics = test_path(
Path::new("flake8_pytest_style").join(path).as_path(),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
}

View file

@ -863,41 +863,23 @@ pub(crate) fn parametrize(checker: &mut Checker, call: &ExprCall) {
}
if checker.enabled(Rule::PytestParametrizeNamesWrongType) {
let names = if checker.settings.preview.is_enabled() {
call.arguments.find_argument_value("argnames", 0)
} else {
call.arguments.find_positional(0)
};
let values = if checker.settings.preview.is_enabled() {
call.arguments.find_argument_value("argvalues", 1)
} else {
call.arguments.find_positional(1)
};
let names = call.arguments.find_argument_value("argnames", 0);
let values = call.arguments.find_argument_value("argvalues", 1);
if let (Some(names), Some(values)) = (names, values) {
check_names(checker, call, names, values);
}
}
if checker.enabled(Rule::PytestParametrizeValuesWrongType) {
let names = if checker.settings.preview.is_enabled() {
call.arguments.find_argument_value("argnames", 0)
} else {
call.arguments.find_positional(0)
};
let values = if checker.settings.preview.is_enabled() {
call.arguments.find_argument_value("argvalues", 1)
} else {
call.arguments.find_positional(1)
};
let names = call.arguments.find_argument_value("argnames", 0);
let values = call.arguments.find_argument_value("argvalues", 1);
if let (Some(names), Some(values)) = (names, values) {
check_values(checker, names, values);
}
}
if checker.enabled(Rule::PytestDuplicateParametrizeTestCases) {
if let Some(values) = if checker.settings.preview.is_enabled() {
call.arguments.find_argument_value("argvalues", 1)
} else {
call.arguments.find_positional(1)
} {
if let Some(values) = call.arguments.find_argument_value("argvalues", 1) {
check_duplicates(checker, values);
}
}

View file

@ -230,6 +230,44 @@ PT006.py:69:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.pa
71 71 | ...
72 72 |
PT006.py:74:39: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
74 | parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
75 |
76 | @parametrize
|
= help: Use a `tuple` for the first argument
Unsafe fix
71 71 | ...
72 72 |
73 73 |
74 |-parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
74 |+parametrize = pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
75 75 |
76 76 | @parametrize
77 77 | def test_not_decorator(param1, param2):
PT006.py:81:35: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
81 | @pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
82 | def test_keyword_arguments(param1, param2):
83 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
78 78 | ...
79 79 |
80 80 |
81 |-@pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
81 |+@pytest.mark.parametrize(argnames=("param1", "param2"), argvalues=[(1, 2), (3, 4)])
82 82 | def test_keyword_arguments(param1, param2):
83 83 | ...
84 84 |
PT006.py:86:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
86 | @pytest.mark.parametrize(("param",), [(1,), (2,)])

View file

@ -192,6 +192,44 @@ PT006.py:69:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.pa
71 71 | ...
72 72 |
PT006.py:74:39: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `list`
|
74 | parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
75 |
76 | @parametrize
|
= help: Use a `list` for the first argument
Unsafe fix
71 71 | ...
72 72 |
73 73 |
74 |-parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
74 |+parametrize = pytest.mark.parametrize(["param1", "param2"], [(1, 2), (3, 4)])
75 75 |
76 76 | @parametrize
77 77 | def test_not_decorator(param1, param2):
PT006.py:81:35: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `list`
|
81 | @pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
82 | def test_keyword_arguments(param1, param2):
83 | ...
|
= help: Use a `list` for the first argument
Unsafe fix
78 78 | ...
79 79 |
80 80 |
81 |-@pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
81 |+@pytest.mark.parametrize(argnames=["param1", "param2"], argvalues=[(1, 2), (3, 4)])
82 82 | def test_keyword_arguments(param1, param2):
83 83 | ...
84 84 |
PT006.py:86:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
86 | @pytest.mark.parametrize(("param",), [(1,), (2,)])

View file

@ -1,433 +0,0 @@
---
source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
snapshot_kind: text
---
PT006.py:9:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
9 | @pytest.mark.parametrize("param1,param2", [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^ PT006
10 | def test_csv(param1, param2):
11 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
6 6 | ...
7 7 |
8 8 |
9 |-@pytest.mark.parametrize("param1,param2", [(1, 2), (3, 4)])
9 |+@pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
10 10 | def test_csv(param1, param2):
11 11 | ...
12 12 |
PT006.py:14:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
14 | @pytest.mark.parametrize(" param1, , param2 , ", [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT006
15 | def test_csv_with_whitespace(param1, param2):
16 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
11 11 | ...
12 12 |
13 13 |
14 |-@pytest.mark.parametrize(" param1, , param2 , ", [(1, 2), (3, 4)])
14 |+@pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
15 15 | def test_csv_with_whitespace(param1, param2):
16 16 | ...
17 17 |
PT006.py:19:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
19 | @pytest.mark.parametrize("param1,param2", [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^ PT006
20 | def test_csv_bad_quotes(param1, param2):
21 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
16 16 | ...
17 17 |
18 18 |
19 |-@pytest.mark.parametrize("param1,param2", [(1, 2), (3, 4)])
19 |+@pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
20 20 | def test_csv_bad_quotes(param1, param2):
21 21 | ...
22 22 |
PT006.py:29:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
29 | @pytest.mark.parametrize(("param1",), [1, 2, 3])
| ^^^^^^^^^^^ PT006
30 | def test_tuple_one_elem(param1, param2):
31 | ...
|
= help: Use a string for the first argument
Safe fix
26 26 | ...
27 27 |
28 28 |
29 |-@pytest.mark.parametrize(("param1",), [1, 2, 3])
29 |+@pytest.mark.parametrize("param1", [1, 2, 3])
30 30 | def test_tuple_one_elem(param1, param2):
31 31 | ...
32 32 |
PT006.py:34:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
34 | @pytest.mark.parametrize(["param1", "param2"], [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^^^^ PT006
35 | def test_list(param1, param2):
36 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
31 31 | ...
32 32 |
33 33 |
34 |-@pytest.mark.parametrize(["param1", "param2"], [(1, 2), (3, 4)])
34 |+@pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
35 35 | def test_list(param1, param2):
36 36 | ...
37 37 |
PT006.py:39:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
39 | @pytest.mark.parametrize(["param1"], [1, 2, 3])
| ^^^^^^^^^^ PT006
40 | def test_list_one_elem(param1, param2):
41 | ...
|
= help: Use a string for the first argument
Safe fix
36 36 | ...
37 37 |
38 38 |
39 |-@pytest.mark.parametrize(["param1"], [1, 2, 3])
39 |+@pytest.mark.parametrize("param1", [1, 2, 3])
40 40 | def test_list_one_elem(param1, param2):
41 41 | ...
42 42 |
PT006.py:44:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
44 | @pytest.mark.parametrize([some_expr, another_expr], [1, 2, 3])
| ^^^^^^^^^^^^^^^^^^^^^^^^^ PT006
45 | def test_list_expressions(param1, param2):
46 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
41 41 | ...
42 42 |
43 43 |
44 |-@pytest.mark.parametrize([some_expr, another_expr], [1, 2, 3])
44 |+@pytest.mark.parametrize((some_expr, another_expr), [1, 2, 3])
45 45 | def test_list_expressions(param1, param2):
46 46 | ...
47 47 |
PT006.py:49:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
49 | @pytest.mark.parametrize([some_expr, "param2"], [1, 2, 3])
| ^^^^^^^^^^^^^^^^^^^^^ PT006
50 | def test_list_mixed_expr_literal(param1, param2):
51 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
46 46 | ...
47 47 |
48 48 |
49 |-@pytest.mark.parametrize([some_expr, "param2"], [1, 2, 3])
49 |+@pytest.mark.parametrize((some_expr, "param2"), [1, 2, 3])
50 50 | def test_list_mixed_expr_literal(param1, param2):
51 51 | ...
52 52 |
PT006.py:54:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
54 | @pytest.mark.parametrize(("param1, " "param2, " "param3"), [(1, 2, 3), (4, 5, 6)])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT006
55 | def test_implicit_str_concat_with_parens(param1, param2, param3):
56 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
51 51 | ...
52 52 |
53 53 |
54 |-@pytest.mark.parametrize(("param1, " "param2, " "param3"), [(1, 2, 3), (4, 5, 6)])
54 |+@pytest.mark.parametrize(("param1", "param2", "param3"), [(1, 2, 3), (4, 5, 6)])
55 55 | def test_implicit_str_concat_with_parens(param1, param2, param3):
56 56 | ...
57 57 |
PT006.py:59:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
59 | @pytest.mark.parametrize("param1, " "param2, " "param3", [(1, 2, 3), (4, 5, 6)])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT006
60 | def test_implicit_str_concat_no_parens(param1, param2, param3):
61 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
56 56 | ...
57 57 |
58 58 |
59 |-@pytest.mark.parametrize("param1, " "param2, " "param3", [(1, 2, 3), (4, 5, 6)])
59 |+@pytest.mark.parametrize(("param1", "param2", "param3"), [(1, 2, 3), (4, 5, 6)])
60 60 | def test_implicit_str_concat_no_parens(param1, param2, param3):
61 61 | ...
62 62 |
PT006.py:64:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
64 | @pytest.mark.parametrize((("param1, " "param2, " "param3")), [(1, 2, 3), (4, 5, 6)])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT006
65 | def test_implicit_str_concat_with_multi_parens(param1, param2, param3):
66 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
61 61 | ...
62 62 |
63 63 |
64 |-@pytest.mark.parametrize((("param1, " "param2, " "param3")), [(1, 2, 3), (4, 5, 6)])
64 |+@pytest.mark.parametrize(("param1", "param2", "param3"), [(1, 2, 3), (4, 5, 6)])
65 65 | def test_implicit_str_concat_with_multi_parens(param1, param2, param3):
66 66 | ...
67 67 |
PT006.py:69:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
69 | @pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
70 | def test_csv_with_parens(param1, param2):
71 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
66 66 | ...
67 67 |
68 68 |
69 |-@pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
69 |+@pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
70 70 | def test_csv_with_parens(param1, param2):
71 71 | ...
72 72 |
PT006.py:74:39: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
74 | parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
75 |
76 | @parametrize
|
= help: Use a `tuple` for the first argument
Unsafe fix
71 71 | ...
72 72 |
73 73 |
74 |-parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])
74 |+parametrize = pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
75 75 |
76 76 | @parametrize
77 77 | def test_not_decorator(param1, param2):
PT006.py:81:35: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `tuple`
|
81 | @pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
| ^^^^^^^^^^^^^^^^^ PT006
82 | def test_keyword_arguments(param1, param2):
83 | ...
|
= help: Use a `tuple` for the first argument
Unsafe fix
78 78 | ...
79 79 |
80 80 |
81 |-@pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
81 |+@pytest.mark.parametrize(argnames=("param1", "param2"), argvalues=[(1, 2), (3, 4)])
82 82 | def test_keyword_arguments(param1, param2):
83 83 | ...
84 84 |
PT006.py:86:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
86 | @pytest.mark.parametrize(("param",), [(1,), (2,)])
| ^^^^^^^^^^ PT006
87 | def test_single_element_tuple(param):
88 | ...
|
= help: Use a string for the first argument
Safe fix
83 83 | ...
84 84 |
85 85 |
86 |-@pytest.mark.parametrize(("param",), [(1,), (2,)])
86 |+@pytest.mark.parametrize("param", [1, 2])
87 87 | def test_single_element_tuple(param):
88 88 | ...
89 89 |
PT006.py:91:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
91 | @pytest.mark.parametrize(("param",), [[1], [2]])
| ^^^^^^^^^^ PT006
92 | def test_single_element_list(param):
93 | ...
|
= help: Use a string for the first argument
Safe fix
88 88 | ...
89 89 |
90 90 |
91 |-@pytest.mark.parametrize(("param",), [[1], [2]])
91 |+@pytest.mark.parametrize("param", [1, 2])
92 92 | def test_single_element_list(param):
93 93 | ...
94 94 |
PT006.py:96:26: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
96 | @pytest.mark.parametrize(("param",), [[1], [2]])
| ^^^^^^^^^^ PT006
97 | def test_single_element_list(param):
98 | ...
|
= help: Use a string for the first argument
Safe fix
93 93 | ...
94 94 |
95 95 |
96 |-@pytest.mark.parametrize(("param",), [[1], [2]])
96 |+@pytest.mark.parametrize("param", [1, 2])
97 97 | def test_single_element_list(param):
98 98 | ...
99 99 |
PT006.py:103:5: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
101 | # Unsafe fix
102 | @pytest.mark.parametrize(
103 | (
| _____^
104 | | # comment
105 | | "param",
106 | | ),
| |_____^ PT006
107 | [[1], [2]],
108 | )
|
= help: Use a string for the first argument
Unsafe fix
100 100 |
101 101 | # Unsafe fix
102 102 | @pytest.mark.parametrize(
103 |- (
104 |- # comment
105 |- "param",
106 |- ),
107 |- [[1], [2]],
103 |+ "param",
104 |+ [1, 2],
108 105 | )
109 106 | def test_comment_in_argnames(param):
110 107 | ...
PT006.py:114:5: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
112 | # Unsafe fix
113 | @pytest.mark.parametrize(
114 | ("param",),
| ^^^^^^^^^^ PT006
115 | [
116 | (
|
= help: Use a string for the first argument
Unsafe fix
111 111 |
112 112 | # Unsafe fix
113 113 | @pytest.mark.parametrize(
114 |- ("param",),
114 |+ "param",
115 115 | [
116 |- (
117 |- # comment
118 |- 1,
119 |- ),
120 |- (2,),
116 |+ 1,
117 |+ 2,
121 118 | ],
122 119 | )
123 120 | def test_comment_in_argvalues(param):
PT006.py:129:5: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
127 | # Safe fix
128 | @pytest.mark.parametrize(
129 | ("param",),
| ^^^^^^^^^^ PT006
130 | [
131 | (1,),
|
= help: Use a string for the first argument
Safe fix
126 126 |
127 127 | # Safe fix
128 128 | @pytest.mark.parametrize(
129 |- ("param",),
129 |+ "param",
130 130 | [
131 |- (1,),
131 |+ 1,
132 132 | # comment
133 |- (2,),
133 |+ 2,
134 134 | ],
135 135 | )
136 136 | def test_comment_between_argvalues_items(param):
PT006.py:142:5: PT006 [*] Wrong type passed to first argument of `pytest.mark.parametrize`; expected `str`
|
140 | # A fix should be suggested for `argnames`, but not for `argvalues`.
141 | @pytest.mark.parametrize(
142 | ("param",),
| ^^^^^^^^^^ PT006
143 | [
144 | (1,),
|
= help: Use a string for the first argument
Safe fix
139 139 |
140 140 | # A fix should be suggested for `argnames`, but not for `argvalues`.
141 141 | @pytest.mark.parametrize(
142 |- ("param",),
142 |+ "param",
143 143 | [
144 144 | (1,),
145 145 | (2, 3),