[flake8-pytest-style] Improve help message for pytest-incorrect-mark-parentheses-style (PT023) (#13092)
This commit is contained in:
@@ -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> {
|
||||
|
||||
@@ -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::<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("()"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 |
|
||||
|
||||
@@ -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 |
|
||||
|
||||
Reference in New Issue
Block a user