Skip to content

Commit

Permalink
[flake8-pytest-style] Stabilize "Detect more `pytest.mark.parametri…
Browse files Browse the repository at this point in the history
…ze` calls" (`PT006`) (#15327)

Co-authored-by: Micha Reiser <[email protected]>
Resolves #15324. Stabilizes the behavior changes introduced in #14515.
  • Loading branch information
InSyncWithFoo authored and MichaReiser committed Jan 8, 2025
1 parent 4644c64 commit 59b0b59
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 498 deletions.
12 changes: 5 additions & 7 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&[
Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType,
Rule::PytestDuplicateParametrizeTestCases,
])
{
if checker.any_enabled(&[
Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType,
Rule::PytestDuplicateParametrizeTestCases,
]) {
flake8_pytest_style::rules::parametrize(checker, call);
}
if checker.enabled(Rule::PytestUnittestAssertion) {
Expand Down
16 changes: 1 addition & 15 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 0 additions & 18 deletions crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,)])
Expand Down
Loading

0 comments on commit 59b0b59

Please sign in to comment.