From 13e77f3af9bb31c14d5fd47f868f2169bcbe29d3 Mon Sep 17 00:00:00 2001 From: Calum Young Date: Sun, 25 Aug 2024 21:59:04 +0100 Subject: [PATCH 1/3] Update expected snapshots --- ...les__flake8_pytest_style__tests__PT023_default.snap | 10 +++++----- ..._flake8_pytest_style__tests__PT023_parentheses.snap | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) 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 | From 668cb4b669f72b95aee93dacd22fa7ef98e87853 Mon Sep 17 00:00:00 2001 From: Calum Young Date: Sun, 25 Aug 2024 22:02:18 +0100 Subject: [PATCH 2/3] Update PT023 error message --- .../flake8_pytest_style/rules/fixture.rs | 2 +- .../rules/flake8_pytest_style/rules/marks.rs | 45 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 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 d0310883ac2e8..8e57ee47c55ca 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 @@ -606,7 +606,7 @@ impl AlwaysFixableViolation for PytestUnnecessaryAsyncioMarkOnFixture { } #[derive(Debug, PartialEq, Eq)] -enum Parentheses { +pub(crate) enum Parentheses { None, Empty, } 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..bf74a7b38a5fd 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 @@ -6,6 +6,7 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; use crate::registry::Rule; +use crate::rules::flake8_pytest_style::rules::fixture::Parentheses; use super::helpers::get_mark_decorators; @@ -52,8 +53,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 +72,14 @@ impl AlwaysFixableViolation for PytestIncorrectMarkParenthesesStyle { } fn fix_title(&self) -> String { - "Add/remove parentheses".to_string() + let PytestIncorrectMarkParenthesesStyle { + expected_parens: expected, + .. + } = self; + match expected { + Parentheses::None => "Remove parentheses".to_string(), + Parentheses::Empty => "Add parentheses".to_string(), + } } } @@ -121,14 +129,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 +161,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, + ); } } } From 51d1533faa8b5fc902cf8e99dac67aed7619f327 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 26 Aug 2024 01:32:46 +0100 Subject: [PATCH 3/3] nitpicks --- .../rules/flake8_pytest_style/rules/fixture.rs | 18 +----------------- .../rules/flake8_pytest_style/rules/helpers.rs | 17 +++++++++++++++++ .../rules/flake8_pytest_style/rules/marks.rs | 9 ++------- 3 files changed, 20 insertions(+), 24 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 8e57ee47c55ca..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)] -pub(crate) 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 bf74a7b38a5fd..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 @@ -6,9 +6,8 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; use crate::registry::Rule; -use crate::rules::flake8_pytest_style::rules::fixture::Parentheses; -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 @@ -72,11 +71,7 @@ impl AlwaysFixableViolation for PytestIncorrectMarkParenthesesStyle { } fn fix_title(&self) -> String { - let PytestIncorrectMarkParenthesesStyle { - expected_parens: expected, - .. - } = self; - match expected { + match &self.expected_parens { Parentheses::None => "Remove parentheses".to_string(), Parentheses::Empty => "Add parentheses".to_string(), }