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

PTH201 fix does not consider trailing comma #15165

Open
InSyncWithFoo opened this issue Dec 28, 2024 · 2 comments · May be fixed by #15177
Open

PTH201 fix does not consider trailing comma #15165

InSyncWithFoo opened this issue Dec 28, 2024 · 2 comments · May be fixed by #15177
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@InSyncWithFoo
Copy link
Contributor

from pathlib import Path

# Before:
_ = Path(".",)
#        ^^^ PTH201

# After:
_ = Path(,)

This happens because the fix is a simple range deletion:

let [Expr::StringLiteral(ast::ExprStringLiteral { value, range })] = &*arguments.args else {
    return;
};

if matches!(value.to_str(), "" | ".") {
    let mut diagnostic = Diagnostic::new(PathConstructorCurrentDirectory, *range);
    diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(*range)));
    checker.diagnostics.push(diagnostic);
}

Perhaps it should instead replace arguments.range with ()?

@MichaReiser
Copy link
Member

We could also just search for a trailing comma using the checker.tokens or the SimpleLexer. It might also be worth to see if there's an existing helper method for removing an argument

@MichaReiser MichaReiser added bug Something isn't working fixes Related to suggested fixes for violations labels Dec 28, 2024
@dylwil3
Copy link
Collaborator

dylwil3 commented Dec 28, 2024

It might also be worth to see if there's an existing helper method for removing an argument

There is! It's here:

https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/fix/edits.rs#L207

@InSyncWithFoo InSyncWithFoo linked a pull request Dec 29, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants