Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Gate behind preview
  • Loading branch information
harupy committed Nov 25, 2024
commit 509c5c6e14558476026ea3812fc2d5de146f7de3
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,10 @@ def test_csv_with_parens(param1, param2):
parametrize = pytest.mark.parametrize(("param1,param2"), [(1, 2), (3, 4)])

@parametrize
def test_csv_with_parens_decorator(param1, param2):
def test_not_decorator(param1, param2):
...


@pytest.mark.parametrize(argnames=("param1,param2"), argvalues=[(1, 2), (3, 4)])
def test_keyword_arguments(param1, param2):
...
12 changes: 7 additions & 5 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,13 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
checker.diagnostics.push(diagnostic);
}
}
if checker.any_enabled(&[
Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType,
Rule::PytestDuplicateParametrizeTestCases,
]) {
if checker.settings.preview.is_enabled()
&& checker.any_enabled(&[
Rule::PytestParametrizeNamesWrongType,
Rule::PytestParametrizeValuesWrongType,
Rule::PytestDuplicateParametrizeTestCases,
])
{
flake8_pytest_style::rules::parametrize(checker, call);
}
if checker.enabled(Rule::PytestUnittestAssertion) {
Expand Down
13 changes: 13 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
body,
);
}
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
25 changes: 25 additions & 0 deletions crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tests {

use crate::registry::Rule;
use crate::settings::types::IdentifierPattern;
use crate::settings::types::PreviewMode;
use crate::test::test_path;
use crate::{assert_messages, settings};

Expand Down Expand Up @@ -291,4 +292,28 @@ mod tests {
assert_messages!(name, diagnostics);
Ok(())
}

#[test_case(
Rule::PytestParametrizeNamesWrongType,
Path::new("PT006.py"),
Settings::default(),
"PT006_default"
)]
fn test_pytest_style_preview(
rule_code: Rule,
path: &Path,
plugin_settings: Settings,
name: &str,
) -> Result<()> {
let diagnostics = test_path(
Path::new("flake8_pytest_style").join(path).as_path(),
&settings::LinterSettings {
preview: PreviewMode::Enabled,
flake8_pytest_style: plugin_settings,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(name, diagnostics);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,35 @@ pub(crate) fn parametrize(checker: &mut Checker, call: &ExprCall) {
}

if checker.enabled(Rule::PytestParametrizeNamesWrongType) {
if let Some(names) = call.arguments.find_argument("argnames", 0) {
if let Some(names) = if checker.settings.preview.is_enabled() {
call.arguments.find_argument("argnames", 0)
} else {
call.arguments.find_positional(0)
} {
check_names(checker, call, names);
}
}
if checker.enabled(Rule::PytestParametrizeValuesWrongType) {
let names = call.arguments.find_argument("argnames", 0);
let values = call.arguments.find_argument("argvalues", 1);
let names = if checker.settings.preview.is_enabled() {
call.arguments.find_argument("argnames", 0)
} else {
call.arguments.find_positional(0)
};
let values = if checker.settings.preview.is_enabled() {
call.arguments.find_argument("argvalues", 1)
} else {
call.arguments.find_positional(1)
};
if let (Some(names), Some(values)) = (names, values) {
check_values(checker, names, values);
}
}
if checker.enabled(Rule::PytestDuplicateParametrizeTestCases) {
if let Some(values) = call.arguments.find_argument("argvalues", 1) {
if let Some(values) = if checker.settings.preview.is_enabled() {
call.arguments.find_argument("argvalues", 1)
} else {
call.arguments.find_positional(1)
} {
check_duplicates(checker, values);
Comment on lines +803 to +833
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This aligns with the upstream implementation:

https://github.com/m-burst/flake8-pytest-style/blob/2addab8b533ea101e25d022a37dc32d89db6c688/flake8_pytest_style/utils.py#L96

def extract_parametrize_call_args(node: ast.Call) -> Optional[ParametrizeArgs]:
    """Extracts argnames, argvalues and ids from a parametrize call."""
    args = get_simple_call_args(node)

    names_arg = args.get_argument('argnames', 0)
    if names_arg is None:
        return None

    values_arg = args.get_argument('argvalues', 1)
    ids_arg = args.get_argument('ids')
    return ParametrizeArgs(names_arg, values_arg, ids_arg)

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ PT006.py:74:39: PT006 [*] Wrong type passed to first argument of `@pytest.mark.p
74 |+parametrize = pytest.mark.parametrize(("param1", "param2"), [(1, 2), (3, 4)])
75 75 |
76 76 | @parametrize
77 77 | def test_csv_with_parens_decorator(param1, param2):
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 | ...
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,4 @@ PT006.py:69:26: PT006 [*] Wrong type passed to first argument of `@pytest.mark.p
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 `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_csv_with_parens_decorator(param1, param2):
72 72 |