From bcaac9693b9c56b305b51c35fa524b5cf691939b Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Sun, 22 Oct 2023 23:03:36 +0900 Subject: [PATCH] Refactor `get_mark_decorators` to return a marker name (#8116) --- .../flake8_pytest_style/rules/fixture.rs | 7 +++---- .../flake8_pytest_style/rules/helpers.rs | 13 ++++++------ .../rules/flake8_pytest_style/rules/marks.rs | 21 +++++++++---------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs index eec71dc46f..5918dfd027 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs @@ -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()); diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs index 43a183267f..43c066ff65 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs @@ -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 { +) -> impl Iterator { 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)) }) } diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/marks.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/marks.rs index 51c3bc6e0c..d58dc7f5f7 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/marks.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/marks.rs @@ -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); } } }