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

Fix PT014 autofix for last item in list #10532

Merged
merged 1 commit into from
Mar 23, 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
Expand Up @@ -51,3 +51,8 @@ def test_error_parentheses_trailing_comma(x):
@pytest.mark.parametrize("x", [1, 2])
def test_ok(x):
...


@pytest.mark.parametrize('data, spec', [(1.0, 1.0), (1.0, 1.0)])
def test_numbers(data, spec):
...
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,15 @@ fn check_values(checker: &mut Checker, names: &Expr, values: &Expr) {
/// Tokenize this range to locate the comma.
/// )
/// ```
fn trailing_comma(element: &Expr, source: &str) -> Option<TextSize> {
SimpleTokenizer::starts_at(element.end(), source)
.find(|token| token.kind == SimpleTokenKind::Comma)
.map(|token| token.start())
fn trailing_comma(element: &Expr, source: &str, max_index: TextSize) -> TextSize {
for token in SimpleTokenizer::starts_at(element.end(), source) {
if matches!(token.kind, SimpleTokenKind::Comma) {
return token.start();
} else if token.start() >= max_index {
return max_index;
}
}
max_index
}

/// PT014
Expand All @@ -672,9 +677,9 @@ fn check_duplicates(checker: &mut Checker, values: &Expr) {
if let Some(prev) = prev {
let values_end = values.end() - TextSize::new(1);
let previous_end =
trailing_comma(prev, checker.locator().contents()).unwrap_or(values_end);
trailing_comma(prev, checker.locator().contents(), values_end);
let element_end =
trailing_comma(element, checker.locator().contents()).unwrap_or(values_end);
trailing_comma(element, checker.locator().contents(), values_end);
let deletion_range = TextRange::new(previous_end, element_end);
if !checker
.indexer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,20 @@ PT014.py:44:11: PT014 [*] Duplicate of test case at index 0 in `@pytest_mark.par
46 45 | )
47 46 | def test_error_parentheses_trailing_comma(x):

PT014.py:56:53: PT014 [*] Duplicate of test case at index 0 in `@pytest_mark.parametrize`
|
56 | @pytest.mark.parametrize('data, spec', [(1.0, 1.0), (1.0, 1.0)])
| ^^^^^^^^^^ PT014
57 | def test_numbers(data, spec):
58 | ...
|
= help: Remove duplicate test case

ℹ Unsafe fix
53 53 | ...
54 54 |
55 55 |
56 |[email protected]('data, spec', [(1.0, 1.0), (1.0, 1.0)])
56 |[email protected]('data, spec', [(1.0, 1.0)])
57 57 | def test_numbers(data, spec):
58 58 | ...
Loading