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

[flake8-pytest-style] Improve help message for pytest-incorrect-mark-parentheses-style (PT023) #13092

Merged
merged 3 commits into from
Aug 26, 2024
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
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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
Expand Down Expand Up @@ -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> {
Expand Down
17 changes: 17 additions & 0 deletions crates/ruff_linter/src/rules/flake8_pytest_style/rules/helpers.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -93,3 +95,18 @@ pub(super) fn split_names(names: &str) -> Vec<&str> {
})
.collect::<Vec<&str>>()
}

#[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("()"),
}
}
}
42 changes: 31 additions & 11 deletions crates/ruff_linter/src/rules/flake8_pytest_style/rules/marks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.<marker>()` decorators with or
Expand Down Expand Up @@ -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 {
Expand All @@ -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(),
}
}
}

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