Skip to content

[flake8-pytest-style] Don't recommend usefixtures for parametrize values in PT019 #17650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ def test_eval(_test_input, _expected): # OK defined in parametrize
@pytest.mark.parametrize("_foo", [1, 2, 3])
def test_thingy2(_foo, _bar): # Error _bar is not defined in parametrize
pass

@pytest.mark.parametrize(["_foo", "_bar"], [1, 2, 3])
def test_thingy3(_foo, _bar): # OK defined in parametrize
pass

@pytest.mark.parametrize(("_foo"), [1, 2, 3])
def test_thingy4(_foo, _bar): # Error _bar is not defined in parametrize
pass
54 changes: 30 additions & 24 deletions crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use ruff_diagnostics::{AlwaysFixableViolation, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::helpers::map_callable;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::Decorator;
use ruff_python_ast::{self as ast, Expr, Parameters, Stmt};
use ruff_python_ast::{visitor, ArgOrKeyword};
use ruff_python_semantic::analyze::visibility::is_abstract;
use ruff_python_semantic::SemanticModel;
use ruff_source_file::LineRanges;
Expand All @@ -18,8 +19,8 @@ use crate::fix::edits;
use crate::registry::Rule;

use super::helpers::{
get_mark_decorators, get_parametrize_decorators, is_pytest_fixture, is_pytest_yield_fixture,
keyword_is_literal, Parentheses,
get_mark_decorators, is_pytest_fixture, is_pytest_yield_fixture, keyword_is_literal,
Parentheses,
};

/// ## What it does
Expand Down Expand Up @@ -810,31 +811,36 @@ fn check_fixture_returns(checker: &Checker, name: &str, body: &[Stmt], returns:
/// PT019
fn check_test_function_args(checker: &Checker, parameters: &Parameters, decorators: &[Decorator]) {
let mut named_parametrize = FxHashSet::default();
for decorator in get_parametrize_decorators(decorators) {
for decorator in decorators.iter().filter(|decorator| {
UnqualifiedName::from_expr(map_callable(&decorator.expression))
.is_some_and(|name| matches!(name.segments(), ["pytest", "mark", "parametrize"]))
}) {
let Some(call_expr) = decorator.expression.as_call_expr() else {
continue;
};
let first_arg = call_expr
.arguments
.arguments_source_order()
.next()
.and_then(|arg| {
if let ArgOrKeyword::Arg(expr) = arg {
expr.as_string_literal_expr()
} else {
None
}
});

if let Some(arg) = first_arg {
for param_name in arg
.value
.to_str()
.split(',')
.filter(|param| !param.is_empty() && param.starts_with('_'))
{
named_parametrize.insert(param_name);
let first_arg = call_expr.arguments.find_argument_value("argnames", 0);

match first_arg {
Some(Expr::StringLiteral(ast::ExprStringLiteral { value, .. })) => {
named_parametrize.extend(
value
.to_str()
.split(',')
.map(str::trim)
.filter(|param| !param.is_empty() && param.starts_with('_')),
);
}
Some(
Expr::List(ast::ExprList { elts, .. }) | Expr::Tuple(ast::ExprTuple { elts, .. }),
) => {
named_parametrize.extend(
elts.iter()
.filter_map(Expr::as_string_literal_expr)
.map(|param| param.value.to_str().trim())
.filter(|param| !param.is_empty() && param.starts_with('_')),
);
}
_ => {}
}
}

Expand Down
13 changes: 0 additions & 13 deletions crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ pub(super) fn get_mark_decorators(
})
}

pub(super) fn get_parametrize_decorators(
decorators: &[Decorator],
) -> impl Iterator<Item = &Decorator> {
decorators.iter().filter_map(|decorator| {
let name = UnqualifiedName::from_expr(map_callable(&decorator.expression))?;
let ["pytest", "mark", "parametrize"] = name.segments() else {
return None;
};

Some(decorator)
})
}

pub(super) fn is_pytest_fail(call: &Expr, semantic: &SemanticModel) -> bool {
semantic
.resolve_qualified_name(call)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ PT019.py:31:24: PT019 Fixture `_bar` without value is injected as parameter, use
| ^^^^ PT019
32 | pass
|

PT019.py:39:24: PT019 Fixture `_bar` without value is injected as parameter, use `@pytest.mark.usefixtures` instead
|
38 | @pytest.mark.parametrize(("_foo"), [1, 2, 3])
39 | def test_thingy4(_foo, _bar): # Error _bar is not defined in parametrize
| ^^^^ PT019
40 | pass
|
Loading