-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-comprehensions
] Handled special case for C401
which also …
…matches `C416` (#10596) ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Similar to #10419, there was a case where there is a collision of C401 and C416 (as discussed in #10101). Fixed this by implementing short-circuit for the comprehension of the form `{x for x in foo}`. ## Test Plan <!-- How was it tested? --> Extended `C401.py` with the case where `set` is not builtin function, and divided the case where the short-circuit should occur. Removed the last testcase of `print(f"{ {set(a for a in 'abc')} }")` test as this is invalid as a python code, but should I keep this?
- Loading branch information
1 parent
80b4688
commit a28776e
Showing
3 changed files
with
287 additions
and
234 deletions.
There are no files selected for viewing
34 changes: 22 additions & 12 deletions
34
crates/ruff_linter/resources/test/fixtures/flake8_comprehensions/C401.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
x = set(x for x in range(3)) | ||
x = set(x for x in range(3)) | ||
y = f"{set(a if a < 6 else 0 for a in range(3))}" | ||
_ = "{}".format(set(a if a < 6 else 0 for a in range(3))) | ||
print(f"Hello {set(a for a in range(3))} World") | ||
|
||
# Cannot conbime with C416. Should use set comprehension here. | ||
even_nums = set(2 * x for x in range(3)) | ||
odd_nums = set( | ||
2 * x + 1 for x in range(3) | ||
) | ||
small_nums = f"{set(a if a < 6 else 0 for a in range(3))}" | ||
|
||
def f(x): | ||
return x | ||
|
||
|
||
print(f'Hello {set(a for a in "abc")} World') | ||
print(f"Hello {set(a for a in 'abc')} World") | ||
print(f"Hello {set(f(a) for a in 'abc')} World") | ||
print(f"Hello { set(f(a) for a in 'abc') } World") | ||
|
||
|
||
# Short-circuit case, combine with C416 and should produce x = set(range(3)) | ||
x = set(x for x in range(3)) | ||
x = set( | ||
x for x in range(3) | ||
) | ||
print(f"Hello {set(a for a in range(3))} World") | ||
print(f"{set(a for a in 'abc') - set(a for a in 'ab')}") | ||
print(f"{ set(a for a in 'abc') - set(a for a in 'ab') }") | ||
|
||
# The fix generated for this diagnostic is incorrect, as we add additional space | ||
# around the set comprehension. | ||
print(f"{ {set(a for a in 'abc')} }") | ||
|
||
# Not built-in set. | ||
def set(*args, **kwargs): | ||
return None | ||
|
||
set(2 * x for x in range(3)) | ||
set(x for x in range(3)) |
This file contains 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
Oops, something went wrong.