From 065289b0742ff995ae602b861b0a12b9ff6e9c39 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 8 May 2023 22:17:00 -0400 Subject: [PATCH] Specify exact command in incorrect parentheses suggestion --- .../flake8_pytest_style/rules/fixture.rs | 63 ++++++++++++++----- ...e8_pytest_style__tests__PT001_default.snap | 6 +- ...st_style__tests__PT001_no_parentheses.snap | 12 ++-- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs index 3f937b2d04396..2bd5bbdd6e67c 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs @@ -1,6 +1,7 @@ use anyhow::Result; use ruff_text_size::{TextLen, TextRange, TextSize}; use rustpython_parser::ast::{Arguments, Expr, ExprKind, Keyword, Stmt, StmtKind}; +use std::fmt; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; @@ -21,24 +22,40 @@ use super::helpers::{ get_mark_decorators, is_pytest_fixture, is_pytest_yield_fixture, keyword_is_literal, }; +#[derive(Debug, PartialEq, Eq)] +pub 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("()"), + } + } +} + #[violation] pub struct PytestFixtureIncorrectParenthesesStyle { - pub expected_parens: String, - pub actual_parens: String, + pub expected: Parentheses, + pub actual: Parentheses, } impl AlwaysAutofixableViolation for PytestFixtureIncorrectParenthesesStyle { #[derive_message_formats] fn message(&self) -> String { - let PytestFixtureIncorrectParenthesesStyle { - expected_parens, - actual_parens, - } = self; - format!("Use `@pytest.fixture{expected_parens}` over `@pytest.fixture{actual_parens}`") + let PytestFixtureIncorrectParenthesesStyle { expected, actual } = self; + format!("Use `@pytest.fixture{expected}` over `@pytest.fixture{actual}`") } fn autofix_title(&self) -> String { - "Add/remove parentheses".to_string() + let PytestFixtureIncorrectParenthesesStyle { expected, .. } = self; + match expected { + Parentheses::None => "Remove parentheses".to_string(), + Parentheses::Empty => "Add parentheses".to_string(), + } } } @@ -235,14 +252,11 @@ fn pytest_fixture_parentheses( checker: &mut Checker, decorator: &Expr, fix: Fix, - preferred: &str, - actual: &str, + expected: Parentheses, + actual: Parentheses, ) { let mut diagnostic = Diagnostic::new( - PytestFixtureIncorrectParenthesesStyle { - expected_parens: preferred.to_string(), - actual_parens: actual.to_string(), - }, + PytestFixtureIncorrectParenthesesStyle { expected, actual }, decorator.range(), ); if checker.patch(diagnostic.kind.rule()) { @@ -279,7 +293,13 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E && keywords.is_empty() { let fix = Fix::unspecified(Edit::deletion(func.end(), decorator.end())); - pytest_fixture_parentheses(checker, decorator, fix, "", "()"); + pytest_fixture_parentheses( + checker, + decorator, + fix, + Parentheses::None, + Parentheses::Empty, + ); } if checker @@ -334,8 +354,17 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &E .enabled(Rule::PytestFixtureIncorrectParenthesesStyle) && checker.settings.flake8_pytest_style.fixture_parentheses { - let fix = Fix::unspecified(Edit::insertion("()".to_string(), decorator.end())); - pytest_fixture_parentheses(checker, decorator, fix, "()", ""); + let fix = Fix::unspecified(Edit::insertion( + Parentheses::Empty.to_string(), + decorator.end(), + )); + pytest_fixture_parentheses( + checker, + decorator, + fix, + Parentheses::Empty, + Parentheses::None, + ); } } } diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_default.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_default.snap index 434880490eabf..c17efd2cf3675 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_default.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_default.snap @@ -8,7 +8,7 @@ PT001.py:9:2: PT001 [*] Use `@pytest.fixture()` over `@pytest.fixture` 10 | def no_parentheses(): 11 | return 42 | - = help: Add/remove parentheses + = help: Add parentheses ℹ Suggested fix 6 6 | # `import pytest` @@ -27,7 +27,7 @@ PT001.py:34:2: PT001 [*] Use `@pytest.fixture()` over `@pytest.fixture` 35 | def imported_from_no_parentheses(): 36 | return 42 | - = help: Add/remove parentheses + = help: Add parentheses ℹ Suggested fix 31 31 | # `from pytest import fixture` @@ -46,7 +46,7 @@ PT001.py:59:2: PT001 [*] Use `@pytest.fixture()` over `@pytest.fixture` 60 | def aliased_no_parentheses(): 61 | return 42 | - = help: Add/remove parentheses + = help: Add parentheses ℹ Suggested fix 56 56 | # `from pytest import fixture as aliased` diff --git a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_no_parentheses.snap b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_no_parentheses.snap index 7732c59b2fc43..3dc9a14d435e5 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_no_parentheses.snap +++ b/crates/ruff/src/rules/flake8_pytest_style/snapshots/ruff__rules__flake8_pytest_style__tests__PT001_no_parentheses.snap @@ -8,7 +8,7 @@ PT001.py:14:2: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()` 15 | def parentheses_no_params(): 16 | return 42 | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Suggested fix 11 11 | return 42 @@ -30,7 +30,7 @@ PT001.py:24:2: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()` 27 | def parentheses_no_params_multiline(): 28 | return 42 | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Suggested fix 21 21 | return 42 @@ -51,7 +51,7 @@ PT001.py:39:2: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()` 40 | def imported_from_parentheses_no_params(): 41 | return 42 | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Suggested fix 36 36 | return 42 @@ -73,7 +73,7 @@ PT001.py:49:2: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()` 52 | def imported_from_parentheses_no_params_multiline(): 53 | return 42 | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Suggested fix 46 46 | return 42 @@ -94,7 +94,7 @@ PT001.py:64:2: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()` 65 | def aliased_parentheses_no_params(): 66 | return 42 | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Suggested fix 61 61 | return 42 @@ -116,7 +116,7 @@ PT001.py:74:2: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()` 77 | def aliased_parentheses_no_params_multiline(): 78 | return 42 | - = help: Add/remove parentheses + = help: Remove parentheses ℹ Suggested fix 71 71 | return 42