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

unnecessary-literal-within-tuple-call (C409) rule transforms single element list literals incorrectly #10323

Closed
bardiharborow opened this issue Mar 11, 2024 · 7 comments
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations good first issue Good for newcomers linter Related to the linter

Comments

@bardiharborow
Copy link

bardiharborow commented Mar 11, 2024

The unnecessary-literal-within-tuple-call (C409) rule detects the violation here:

tuple([1])

ruff check --fix --unsafe-fixes fails to transform the code correctly, resulting in this, which is not a tuple:

(1)

Instead it should transform the code to:

(1,)

Environment:

% ruff --version
ruff 0.3.2

% cat pyproject.toml
[tool.ruff]
lint.select = [
    "C4"
]
@bardiharborow bardiharborow changed the title unnecessary-literal-within-tuple-call (C409) rule fixes single element tuples incorrectly unnecessary-literal-within-tuple-call (C409) rule fixes zero and single element lists incorrectly Mar 11, 2024
@bardiharborow bardiharborow changed the title unnecessary-literal-within-tuple-call (C409) rule fixes zero and single element lists incorrectly unnecessary-literal-within-tuple-call (C409) rule transforms single element list literals incorrectly Mar 11, 2024
@zanieb zanieb added bug Something isn't working good first issue Good for newcomers fixes Related to suggested fixes for violations linter Related to the linter labels Mar 11, 2024
@zanieb
Copy link
Member

zanieb commented Mar 11, 2024

Thanks for the report!

@WindowGenerator
Copy link
Contributor

@zanieb Hi!
Can I take it too?

@charliermarsh
Copy link
Member

Go for it @WindowGenerator.

@bardiharborow
Copy link
Author

I don't know much about Ruff internals but it feels like it might be safer to take the approach of transforming and then rendering the AST, rather than replacing specific string indexes, since the latter is vulnerable to producing invalid or incorrect code in this fashion.

@dhruvmanila
Copy link
Member

Yeah, we have a Generator struct which does exactly that. Moving the fix to use that would solve the issue as it takes one element tuple into consideration.

@charliermarsh
Copy link
Member

We should just add this case to the fix. Text-based fixes are preferable because they preserve all trivia. (This fix used to use LibCST but we intentionally changed it because it’s slow and we want to remove our LibCST dependency altogether eventually.)

I think there was just a misunderstanding on what the fix is supposed to do based on the comment above it in the code, which led to this oversight.

WindowGenerator added a commit to WindowGenerator/ruff that referenced this issue Mar 20, 2024
WindowGenerator added a commit to WindowGenerator/ruff that referenced this issue Mar 20, 2024
WindowGenerator added a commit to WindowGenerator/ruff that referenced this issue Mar 20, 2024
WindowGenerator added a commit to WindowGenerator/ruff that referenced this issue Mar 20, 2024
charliermarsh pushed a commit that referenced this issue Mar 21, 2024
…10491)

# Summary
Fixed: incorrect rule transformation rule C409 with single element.

# Test Plan
Added examples from #10323 to test fixtures.
@bardiharborow
Copy link
Author

Fixed by #10491.

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 good first issue Good for newcomers linter Related to the linter
Projects
None yet
Development

No branches or pull requests

5 participants