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 d0310883ac2e8..3af592adce7f5 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 @@ -1,5 +1,3 @@ -use std::fmt; - use ruff_diagnostics::{AlwaysFixableViolation, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -20,6 +18,7 @@ use crate::registry::Rule; use super::helpers::{ get_mark_decorators, is_pytest_fixture, is_pytest_yield_fixture, keyword_is_literal, + Parentheses, }; /// ## What it does @@ -605,21 +604,6 @@ impl AlwaysFixableViolation for PytestUnnecessaryAsyncioMarkOnFixture { } } -#[derive(Debug, PartialEq, Eq)] -enum Parentheses { - None, - Empty, -} - -impl fmt::Display for Parentheses { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match self { - Parentheses::None => fmt.write_str(""), - Parentheses::Empty => fmt.write_str("()"), - } - } -} - /// Visitor that skips functions #[derive(Debug, Default)] struct SkipFunctionsVisitor<'a> { 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 8e4c1b28ca8e6..edbe837e2c1dd 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,3 +1,5 @@ +use std::fmt; + use ruff_python_ast::helpers::map_callable; use ruff_python_ast::name::UnqualifiedName; use ruff_python_ast::{self as ast, Decorator, Expr, Keyword}; @@ -93,3 +95,18 @@ pub(super) fn split_names(names: &str) -> Vec<&str> { }) .collect::>() } + +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +pub(super) enum Parentheses { + None, + Empty, +} + +impl fmt::Display for Parentheses { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match self { + Parentheses::None => fmt.write_str(""), + Parentheses::Empty => fmt.write_str("()"), + } + } +} 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 9b9fa984bc754..b814fac80a4af 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 @@ -7,7 +7,7 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; use crate::registry::Rule; -use super::helpers::get_mark_decorators; +use super::helpers::{get_mark_decorators, Parentheses}; /// ## What it does /// Checks for argument-free `@pytest.mark.()` decorators with or @@ -52,8 +52,8 @@ use super::helpers::get_mark_decorators; #[violation] pub struct PytestIncorrectMarkParenthesesStyle { mark_name: String, - expected_parens: String, - actual_parens: String, + expected_parens: Parentheses, + actual_parens: Parentheses, } impl AlwaysFixableViolation for PytestIncorrectMarkParenthesesStyle { @@ -71,7 +71,10 @@ impl AlwaysFixableViolation for PytestIncorrectMarkParenthesesStyle { } fn fix_title(&self) -> String { - "Add/remove parentheses".to_string() + match &self.expected_parens { + Parentheses::None => "Remove parentheses".to_string(), + Parentheses::Empty => "Add parentheses".to_string(), + } } } @@ -121,14 +124,14 @@ fn pytest_mark_parentheses( decorator: &Decorator, marker: &str, fix: Fix, - preferred: &str, - actual: &str, + preferred: Parentheses, + actual: Parentheses, ) { let mut diagnostic = Diagnostic::new( PytestIncorrectMarkParenthesesStyle { mark_name: marker.to_string(), - expected_parens: preferred.to_string(), - actual_parens: actual.to_string(), + expected_parens: preferred, + actual_parens: actual, }, decorator.range(), ); @@ -153,13 +156,30 @@ fn check_mark_parentheses(checker: &mut Checker, decorator: &Decorator, marker: && keywords.is_empty() { let fix = Fix::safe_edit(Edit::deletion(func.end(), decorator.end())); - pytest_mark_parentheses(checker, decorator, marker, fix, "", "()"); + pytest_mark_parentheses( + checker, + decorator, + marker, + fix, + Parentheses::None, + Parentheses::Empty, + ); } } _ => { if checker.settings.flake8_pytest_style.mark_parentheses { - let fix = Fix::safe_edit(Edit::insertion("()".to_string(), decorator.end())); - pytest_mark_parentheses(checker, decorator, marker, fix, "()", ""); + let fix = Fix::safe_edit(Edit::insertion( + Parentheses::Empty.to_string(), + decorator.end(), + )); + pytest_mark_parentheses( + checker, + decorator, + marker, + fix, + Parentheses::Empty, + Parentheses::None, + ); } } } diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_default.snap b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_default.snap index d53baff08eac4..62aa7f5890622 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_default.snap +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_default.snap @@ -8,7 +8,7 @@ PT023.py:46:1: PT023 [*] Use `@pytest.mark.foo` over `@pytest.mark.foo()` 47 | def test_something(): 48 | pass | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Safe fix 43 43 | # With parentheses @@ -27,7 +27,7 @@ PT023.py:51:1: PT023 [*] Use `@pytest.mark.foo` over `@pytest.mark.foo()` 52 | class TestClass: 53 | def test_something(): | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Safe fix 48 48 | pass @@ -47,7 +47,7 @@ PT023.py:58:5: PT023 [*] Use `@pytest.mark.foo` over `@pytest.mark.foo()` 59 | def test_something(): 60 | pass | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Safe fix 55 55 | @@ -67,7 +67,7 @@ PT023.py:64:5: PT023 [*] Use `@pytest.mark.foo` over `@pytest.mark.foo()` 65 | class TestNestedClass: 66 | def test_something(): | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Safe fix 61 61 | @@ -88,7 +88,7 @@ PT023.py:72:9: PT023 [*] Use `@pytest.mark.foo` over `@pytest.mark.foo()` 73 | def test_something(): 74 | pass | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Safe fix 69 69 | diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_parentheses.snap b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_parentheses.snap index 2b2a29aafbd63..57bf657ad295a 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_parentheses.snap +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/snapshots/ruff_linter__rules__flake8_pytest_style__tests__PT023_parentheses.snap @@ -8,7 +8,7 @@ PT023.py:12:1: PT023 [*] Use `@pytest.mark.foo()` over `@pytest.mark.foo` 13 | def test_something(): 14 | pass | - = help: Add/remove parentheses + = help: Add parentheses ℹ Safe fix 9 9 | # Without parentheses @@ -27,7 +27,7 @@ PT023.py:17:1: PT023 [*] Use `@pytest.mark.foo()` over `@pytest.mark.foo` 18 | class TestClass: 19 | def test_something(): | - = help: Add/remove parentheses + = help: Add parentheses ℹ Safe fix 14 14 | pass @@ -47,7 +47,7 @@ PT023.py:24:5: PT023 [*] Use `@pytest.mark.foo()` over `@pytest.mark.foo` 25 | def test_something(): 26 | pass | - = help: Add/remove parentheses + = help: Add parentheses ℹ Safe fix 21 21 | @@ -67,7 +67,7 @@ PT023.py:30:5: PT023 [*] Use `@pytest.mark.foo()` over `@pytest.mark.foo` 31 | class TestNestedClass: 32 | def test_something(): | - = help: Add/remove parentheses + = help: Add parentheses ℹ Safe fix 27 27 | @@ -88,7 +88,7 @@ PT023.py:38:9: PT023 [*] Use `@pytest.mark.foo()` over `@pytest.mark.foo` 39 | def test_something(): 40 | pass | - = help: Add/remove parentheses + = help: Add parentheses ℹ Safe fix 35 35 |