-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ruff] add fix safety section (RUF007)
#17755
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
Merged
ntBre
merged 4 commits into
astral-sh:main
from
VascoSch92:fix-safety-section-zip-instead-of-pairwise
May 14, 2025
Merged
[ruff] add fix safety section (RUF007)
#17755
ntBre
merged 4 commits into
astral-sh:main
from
VascoSch92:fix-safety-section-zip-instead-of-pairwise
May 14, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
|
Contributor
Author
|
@dylwil3 Hey ;-) I think it is ready to review |
ntBre
reviewed
May 9, 2025
crates/ruff_linter/src/rules/ruff/rules/zip_instead_of_pairwise.rs
Outdated
Show resolved
Hide resolved
Contributor
Author
ntBre
approved these changes
May 14, 2025
Contributor
ntBre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! And nice catch on the slicing behavior. That is very tricky.
Glyphack
pushed a commit
to Glyphack/ruff
that referenced
this pull request
May 21, 2025
The PR add the `fix safety` section for rule `RUF007` (astral-sh#15584 ) It seems that the fix was always marked as unsafe astral-sh#14401 ## Unsafety example This first example is a little extreme. In fact, the class `Foo` overrides the `__getitem__` method but in a very special, way. The difference lies in the fact that `zip(letters, letters[1:])` call the slice `letters[1:]` which is behaving weird in this case, while `itertools.pairwise(letters)` call just `__getitem__(0), __getitem__(1), ...` and so on. Note that the diagnostic is emitted: [playground](https://play.ruff.rs) I don't know if we want to mention this problem, as there is a subtile bug in the python implementation of `Foo` which make the rule unsafe. ```python from dataclasses import dataclass import itertools @DataClass class Foo: letters: str def __getitem__(self, index): return self.letters[index] + "_foo" letters = Foo("ABCD") zip_ = zip(letters, letters[1:]) for a, b in zip_: print(a, b) # A_foo B, B_foo C, C_foo D, D_foo _ pair = itertools.pairwise(letters) for a, b in pair: print(a, b) # A_foo B_foo, B_foo C_foo, C_foo D_foo ``` This other example is much probable. here, `itertools.pairwise` was shadowed by a costume function [(playground)](https://play.ruff.rs) ```python from dataclasses import dataclass from itertools import pairwise def pairwise(a): return [] letters = "ABCD" zip_ = zip(letters, letters[1:]) print([(a, b) for a, b in zip_]) # [('A', 'B'), ('B', 'C'), ('C', 'D')] pair = pairwise(letters) print(pair) # [] ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The PR add the
fix safetysection for ruleRUF007(#15584 )It seems that the fix was always marked as unsafe #14401
Unsafety example
This first example is a little extreme. In fact, the class
Foooverrides the__getitem__method but in a very special, way. The difference lies in the fact thatzip(letters, letters[1:])call the sliceletters[1:]which is behaving weird in this case, whileitertools.pairwise(letters)call just__getitem__(0), __getitem__(1), ...and so on.Note that the diagnostic is emitted: playground
I don't know if we want to mention this problem, as there is a subtile bug in the python implementation of
Foowhich make the rule unsafe.This other example is much probable.
here,
itertools.pairwisewas shadowed by a costume function (playground)