-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix pytest.mark.parametrize
rules to check calls instead of decorators
#14515
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
778177e
Check calls
harupy e791dca
Fix
harupy 1ddef63
Update snapshots
harupy 3f385b6
Use call
harupy 0d52662
Update crates/ruff_linter/src/rules/flake8_pytest_style/rules/paramet…
harupy b6f6d0f
fmt
harupy 2f39759
Restore snapshot_kind
harupy 509c5c6
Gate behind preview
harupy 5d1f8dc
Fix snapshot path
harupy 837f531
Remove plugin_settings
harupy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ use ruff_macros::{derive_message_formats, violation}; | |
use ruff_python_ast::comparable::ComparableExpr; | ||
use ruff_python_ast::parenthesize::parenthesized_range; | ||
use ruff_python_ast::AstNode; | ||
use ruff_python_ast::{self as ast, Arguments, Decorator, Expr, ExprContext}; | ||
use ruff_python_ast::{self as ast, Expr, ExprCall, ExprContext}; | ||
use ruff_python_codegen::Generator; | ||
use ruff_python_trivia::CommentRanges; | ||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer}; | ||
|
@@ -317,23 +317,21 @@ fn elts_to_csv(elts: &[Expr], generator: Generator) -> Option<String> { | |
/// | ||
/// This method assumes that the first argument is a string. | ||
fn get_parametrize_name_range( | ||
decorator: &Decorator, | ||
call: &ExprCall, | ||
expr: &Expr, | ||
comment_ranges: &CommentRanges, | ||
source: &str, | ||
) -> Option<TextRange> { | ||
decorator.expression.as_call_expr().and_then(|call| { | ||
parenthesized_range( | ||
expr.into(), | ||
call.arguments.as_any_node_ref(), | ||
comment_ranges, | ||
source, | ||
) | ||
}) | ||
parenthesized_range( | ||
expr.into(), | ||
call.arguments.as_any_node_ref(), | ||
comment_ranges, | ||
source, | ||
) | ||
} | ||
|
||
/// PT006 | ||
fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { | ||
fn check_names(checker: &mut Checker, call: &ExprCall, expr: &Expr) { | ||
let names_type = checker.settings.flake8_pytest_style.parametrize_names_type; | ||
|
||
match expr { | ||
|
@@ -343,7 +341,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { | |
match names_type { | ||
types::ParametrizeNameType::Tuple => { | ||
let name_range = get_parametrize_name_range( | ||
decorator, | ||
call, | ||
expr, | ||
checker.comment_ranges(), | ||
checker.locator().contents(), | ||
|
@@ -378,7 +376,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { | |
} | ||
types::ParametrizeNameType::List => { | ||
let name_range = get_parametrize_name_range( | ||
decorator, | ||
call, | ||
expr, | ||
checker.comment_ranges(), | ||
checker.locator().contents(), | ||
|
@@ -797,30 +795,26 @@ fn handle_value_rows( | |
} | ||
} | ||
|
||
pub(crate) fn parametrize(checker: &mut Checker, decorators: &[Decorator]) { | ||
for decorator in decorators { | ||
if is_pytest_parametrize(decorator, checker.semantic()) { | ||
if let Expr::Call(ast::ExprCall { | ||
arguments: Arguments { args, .. }, | ||
.. | ||
}) = &decorator.expression | ||
{ | ||
if checker.enabled(Rule::PytestParametrizeNamesWrongType) { | ||
if let [names, ..] = &**args { | ||
check_names(checker, decorator, names); | ||
} | ||
} | ||
if checker.enabled(Rule::PytestParametrizeValuesWrongType) { | ||
if let [names, values, ..] = &**args { | ||
check_values(checker, names, values); | ||
} | ||
} | ||
if checker.enabled(Rule::PytestDuplicateParametrizeTestCases) { | ||
if let [_, values, ..] = &**args { | ||
check_duplicates(checker, values); | ||
} | ||
} | ||
} | ||
pub(crate) fn parametrize(checker: &mut Checker, call: &ExprCall) { | ||
if !is_pytest_parametrize(call, checker.semantic()) { | ||
return; | ||
} | ||
|
||
if checker.enabled(Rule::PytestParametrizeNamesWrongType) { | ||
if let Some(names) = call.arguments.find_argument("argnames", 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); | ||
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) { | ||
check_duplicates(checker, values); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This aligns with the upstream implementation: 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) |
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
--- | ||
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` | ||
| | ||
|
@@ -228,3 +227,23 @@ PT006.py:69:26: PT006 [*] Wrong type passed to first argument of `@pytest.mark.p | |
69 |[email protected](("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_csv_with_parens_decorator(param1, param2): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
--- | ||
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 `list` | ||
| | ||
|
@@ -190,3 +189,23 @@ PT006.py:69:26: PT006 [*] Wrong type passed to first argument of `@pytest.mark.p | |
69 |[email protected](["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): |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR, but PT rules don't have a
seen_module
guard. Shoud we add it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we could.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a nice good first issue.