mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:09:22 +00:00
Refactor get_mark_decorators
to return a marker name (#8116)
This commit is contained in:
parent
d6f59e4131
commit
bcaac9693b
3 changed files with 19 additions and 22 deletions
|
@ -900,10 +900,9 @@ fn check_fixture_addfinalizer(checker: &mut Checker, parameters: &Parameters, bo
|
|||
|
||||
/// PT024, PT025
|
||||
fn check_fixture_marks(checker: &mut Checker, decorators: &[Decorator]) {
|
||||
for (expr, call_path) in get_mark_decorators(decorators) {
|
||||
let name = call_path.last().expect("Expected a mark name");
|
||||
for (expr, marker) in get_mark_decorators(decorators) {
|
||||
if checker.enabled(Rule::PytestUnnecessaryAsyncioMarkOnFixture) {
|
||||
if *name == "asyncio" {
|
||||
if marker == "asyncio" {
|
||||
let mut diagnostic =
|
||||
Diagnostic::new(PytestUnnecessaryAsyncioMarkOnFixture, expr.range());
|
||||
let range = checker.locator().full_lines_range(expr.range());
|
||||
|
@ -913,7 +912,7 @@ fn check_fixture_marks(checker: &mut Checker, decorators: &[Decorator]) {
|
|||
}
|
||||
|
||||
if checker.enabled(Rule::PytestErroneousUseFixturesOnFixture) {
|
||||
if *name == "usefixtures" {
|
||||
if marker == "usefixtures" {
|
||||
let mut diagnostic =
|
||||
Diagnostic::new(PytestErroneousUseFixturesOnFixture, expr.range());
|
||||
let line_range = checker.locator().full_lines_range(expr.range());
|
||||
|
|
|
@ -1,22 +1,21 @@
|
|||
use ruff_python_ast::{self as ast, Constant, Decorator, Expr, Keyword};
|
||||
|
||||
use ruff_python_ast::call_path::{collect_call_path, CallPath};
|
||||
use ruff_python_ast::call_path::collect_call_path;
|
||||
use ruff_python_ast::helpers::map_callable;
|
||||
use ruff_python_semantic::SemanticModel;
|
||||
use ruff_python_trivia::PythonWhitespace;
|
||||
|
||||
pub(super) fn get_mark_decorators(
|
||||
decorators: &[Decorator],
|
||||
) -> impl Iterator<Item = (&Decorator, CallPath)> {
|
||||
) -> impl Iterator<Item = (&Decorator, &str)> {
|
||||
decorators.iter().filter_map(|decorator| {
|
||||
let Some(call_path) = collect_call_path(map_callable(&decorator.expression)) else {
|
||||
return None;
|
||||
};
|
||||
if call_path.len() > 2 && call_path.as_slice()[..2] == ["pytest", "mark"] {
|
||||
Some((decorator, call_path))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let ["pytest", "mark", marker] = call_path.as_slice() else {
|
||||
return None;
|
||||
};
|
||||
Some((decorator, *marker))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ use ruff_python_ast::{self as ast, Arguments, Decorator, Expr};
|
|||
|
||||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::call_path::CallPath;
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
|
@ -117,14 +116,14 @@ impl AlwaysFixableViolation for PytestUseFixturesWithoutParameters {
|
|||
fn pytest_mark_parentheses(
|
||||
checker: &mut Checker,
|
||||
decorator: &Decorator,
|
||||
call_path: &CallPath,
|
||||
marker: &str,
|
||||
fix: Fix,
|
||||
preferred: &str,
|
||||
actual: &str,
|
||||
) {
|
||||
let mut diagnostic = Diagnostic::new(
|
||||
PytestIncorrectMarkParenthesesStyle {
|
||||
mark_name: (*call_path.last().unwrap()).to_string(),
|
||||
mark_name: marker.to_string(),
|
||||
expected_parens: preferred.to_string(),
|
||||
actual_parens: actual.to_string(),
|
||||
},
|
||||
|
@ -134,7 +133,7 @@ fn pytest_mark_parentheses(
|
|||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
|
||||
fn check_mark_parentheses(checker: &mut Checker, decorator: &Decorator, call_path: &CallPath) {
|
||||
fn check_mark_parentheses(checker: &mut Checker, decorator: &Decorator, marker: &str) {
|
||||
match &decorator.expression {
|
||||
Expr::Call(ast::ExprCall {
|
||||
func,
|
||||
|
@ -151,20 +150,20 @@ fn check_mark_parentheses(checker: &mut Checker, decorator: &Decorator, call_pat
|
|||
&& keywords.is_empty()
|
||||
{
|
||||
let fix = Fix::safe_edit(Edit::deletion(func.end(), decorator.end()));
|
||||
pytest_mark_parentheses(checker, decorator, call_path, fix, "", "()");
|
||||
pytest_mark_parentheses(checker, decorator, marker, fix, "", "()");
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if checker.settings.flake8_pytest_style.mark_parentheses {
|
||||
let fix = Fix::safe_edit(Edit::insertion("()".to_string(), decorator.end()));
|
||||
pytest_mark_parentheses(checker, decorator, call_path, fix, "()", "");
|
||||
pytest_mark_parentheses(checker, decorator, marker, fix, "()", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_useless_usefixtures(checker: &mut Checker, decorator: &Decorator, call_path: &CallPath) {
|
||||
if *call_path.last().unwrap() != "usefixtures" {
|
||||
fn check_useless_usefixtures(checker: &mut Checker, decorator: &Decorator, marker: &str) {
|
||||
if marker != "usefixtures" {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -191,12 +190,12 @@ pub(crate) fn marks(checker: &mut Checker, decorators: &[Decorator]) {
|
|||
let enforce_parentheses = checker.enabled(Rule::PytestIncorrectMarkParenthesesStyle);
|
||||
let enforce_useless_usefixtures = checker.enabled(Rule::PytestUseFixturesWithoutParameters);
|
||||
|
||||
for (decorator, call_path) in get_mark_decorators(decorators) {
|
||||
for (decorator, marker) in get_mark_decorators(decorators) {
|
||||
if enforce_parentheses {
|
||||
check_mark_parentheses(checker, decorator, &call_path);
|
||||
check_mark_parentheses(checker, decorator, marker);
|
||||
}
|
||||
if enforce_useless_usefixtures {
|
||||
check_useless_usefixtures(checker, decorator, &call_path);
|
||||
check_useless_usefixtures(checker, decorator, marker);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue