Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify exact command in incorrect parentheses suggestion #4300

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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(),
}
}
}

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down