Skip to content

Commit

Permalink
[ruff] Do not emit diagnostic when all arguments to zip() are var…
Browse files Browse the repository at this point in the history
…iadic (`RUF058`) (#15744)
  • Loading branch information
InSyncWithFoo authored Jan 25, 2025
1 parent c824140 commit cb3361e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
6 changes: 5 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/ruff/RUF058.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

starmap(func, zip())
starmap(func, zip([]))
starmap(func, zip(*args))


starmap(func, zip(a, b, c,),)

Expand Down Expand Up @@ -71,3 +71,7 @@
starmap(func, zip(a, b, c, strict=True))
starmap(func, zip(a, b, c, strict=False))
starmap(func, zip(a, b, c, strict=strict))

# https://github.com/astral-sh/ruff/issues/15742
starmap(func, zip(*a))
starmap(func, zip(*a, *b))
7 changes: 7 additions & 0 deletions crates/ruff_linter/src/rules/ruff/rules/starmap_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ pub(crate) fn starmap_zip(checker: &mut Checker, call: &ExprCall) {
return;
}

let positionals = &iterable_call.arguments.args;

// `zip(*a)` where `a` is empty is valid, but `map(_, *a)` isn't.
if !positionals.is_empty() && positionals.iter().all(Expr::is_starred_expr) {
return;
}

if !semantic
.resolve_qualified_name(&call.func)
.is_some_and(|it| matches!(it.segments(), ["itertools", "starmap"]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ RUF058.py:7:1: RUF058 [*] `itertools.starmap` called on `zip` iterable
7 | starmap(func, zip())
| ^^^^^^^^^^^^^^^^^^^^ RUF058
8 | starmap(func, zip([]))
9 | starmap(func, zip(*args))
|
= help: Use `map` instead

Expand All @@ -19,15 +18,14 @@ RUF058.py:7:1: RUF058 [*] `itertools.starmap` called on `zip` iterable
7 |-starmap(func, zip())
7 |+map(func, [])
8 8 | starmap(func, zip([]))
9 9 | starmap(func, zip(*args))
9 9 |
10 10 |

RUF058.py:8:1: RUF058 [*] `itertools.starmap` called on `zip` iterable
|
7 | starmap(func, zip())
8 | starmap(func, zip([]))
| ^^^^^^^^^^^^^^^^^^^^^^ RUF058
9 | starmap(func, zip(*args))
|
= help: Use `map` instead

Expand All @@ -37,43 +35,20 @@ RUF058.py:8:1: RUF058 [*] `itertools.starmap` called on `zip` iterable
7 7 | starmap(func, zip())
8 |-starmap(func, zip([]))
8 |+map(func, [])
9 9 | starmap(func, zip(*args))
9 9 |
10 10 |
11 11 | starmap(func, zip(a, b, c,),)

RUF058.py:9:1: RUF058 [*] `itertools.starmap` called on `zip` iterable
|
7 | starmap(func, zip())
8 | starmap(func, zip([]))
9 | starmap(func, zip(*args))
| ^^^^^^^^^^^^^^^^^^^^^^^^^ RUF058
10 |
11 | starmap(func, zip(a, b, c,),)
|
= help: Use `map` instead

Safe fix
6 6 |
7 7 | starmap(func, zip())
8 8 | starmap(func, zip([]))
9 |-starmap(func, zip(*args))
9 |+map(func, *args)
10 10 |
11 11 | starmap(func, zip(a, b, c,),)
12 12 |

RUF058.py:11:1: RUF058 [*] `itertools.starmap` called on `zip` iterable
|
9 | starmap(func, zip(*args))
10 |
11 | starmap(func, zip(a, b, c,),)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF058
|
= help: Use `map` instead

Safe fix
8 8 | starmap(func, zip([]))
9 9 | starmap(func, zip(*args))
9 9 |
10 10 |
11 |-starmap(func, zip(a, b, c,),)
11 |+map(func, a, b, c,)
Expand Down

0 comments on commit cb3361e

Please sign in to comment.