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

Conversation

augustelalande
Copy link
Contributor

@augustelalande augustelalande commented Mar 23, 2024

Summary

This error was found browsing https://github.com/qarmin/Automated-Fuzzer/actions/runs/8396966850. Which failed when trying to autofix the PT014 violation in the following code:

@pytest.mark.parametrize('data, spec', [(1.0, 1.0), (1.0, 1.0)])
def test_numbers(data, spec):
    ...

Investigation revealed that the implementation was not properly tested, when the duplicate value was also the last in the list. In particular the following function, which is in charge of finding the comma following an element to create the suggested fix,

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())
}

would find the next comma even if it was outside the list itself leading to a lot of code being deleted.

This PR fixes that.

Test Plan

Added misbehaving code to the test fixture.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 23, 2024
@charliermarsh charliermarsh changed the title Fix PT014 invalid fix Fix PT014 autofix for last item in list Mar 23, 2024
@charliermarsh charliermarsh merged commit 0c194f5 into astral-sh:main Mar 23, 2024
17 checks passed
@augustelalande augustelalande deleted the pt014-fix-bug branch March 23, 2024 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants