Avoid invalid fix for C417 with separate keys and values (#6954)

Closes https://github.com/astral-sh/ruff/issues/6951.
This commit is contained in:
Charlie Marsh 2023-08-28 16:49:40 -04:00 committed by GitHub
parent ecca125f9a
commit d1ad20c9ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View file

@ -37,3 +37,6 @@ map(lambda x: lambda x: x, range(4))
map(lambda x=1: x, nums)
map(lambda *args: len(args), range(4))
map(lambda **kwargs: len(kwargs), range(4))
# Ok because multiple arguments are allowed.
dict(map(lambda k, v: (k, v), keys, values))

View file

@ -89,7 +89,7 @@ pub(crate) fn unnecessary_map(
ObjectType::Generator => {
// Exclude the parent if already matched by other arms.
if parent
.and_then(ruff_python_ast::Expr::as_call_expr)
.and_then(Expr::as_call_expr)
.and_then(|call| call.func.as_name_expr())
.is_some_and(|name| matches!(name.id.as_str(), "list" | "set" | "dict"))
{
@ -122,7 +122,7 @@ pub(crate) fn unnecessary_map(
// Only flag, e.g., `list(map(lambda x: x + 1, iterable))`.
let [Expr::Call(ast::ExprCall {
func,
arguments: Arguments { args, .. },
arguments: Arguments { args, keywords, .. },
..
})] = args
else {
@ -133,6 +133,10 @@ pub(crate) fn unnecessary_map(
return;
}
if !keywords.is_empty() {
return;
}
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
else {
return;
@ -163,13 +167,21 @@ pub(crate) fn unnecessary_map(
// Only flag, e.g., `dict(map(lambda v: (v, v ** 2), values))`.
let [Expr::Call(ast::ExprCall {
func,
arguments: Arguments { args, .. },
arguments: Arguments { args, keywords, .. },
..
})] = args
else {
return;
};
if args.len() != 2 {
return;
}
if !keywords.is_empty() {
return;
}
let Some(argument) = helpers::first_argument_with_matching_function("map", func, args)
else {
return;