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

[flake8-comprehensions] Detect overshadowed list/set/dict, ignore variadics and named expressions (C417) #15955

Merged
merged 4 commits into from
Feb 7, 2025

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Part of #15809 and #15876.

This change brings several bugfixes:

  • The nested map() call in list(map(lambda x: x, [])) where list is overshadowed is now correctly reported.
  • The call will no longer reported if:
    • Any arguments given to map() are variadic.
    • Any of the iterables contain a named expression.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Feb 5, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added bug Something isn't working rule Implementing or modifying a lint rule labels Feb 5, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. It's sort of difficult to see the actual fixes in between the refactors you did. Would you mind commenting on the diff to highlight the fixes for each bullet point?

@InSyncWithFoo
Copy link
Contributor Author

InSyncWithFoo commented Feb 7, 2025

Would you mind commenting on the diff to highlight the fixes for each bullet point?

The diff is a tad too big to reason about, so here's something else to the same effect.


The nested map() call in list(map(lambda x: x, [])) where list is overshadowed is now correctly reported.

Before:

if parent
    .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"))
{
    return;
}

After:

if parent_call_func.is_some_and(|func| is_list_set_or_dict(func, semantic)) {
    return;
}

// ...

fn is_list_set_or_dict(func: &Expr, semantic: &SemanticModel) -> bool {
    semantic
        .resolve_qualified_name(func)
        .is_some_and(|qualified_name| {
            matches!(
                qualified_name.segments(),
                ["" | "builtins", "list" | "set" | "dict"]
            )
        })
}
  • The call will no longer reported if:
    • Any arguments given to map() are variadic.
    • Any of the iterables contain a named expression.

This check was present, but only for the ::Generator branch:

// For example, (x+1 for x in (c:=a)) is invalid syntax
// so we can't suggest it.
if any_over_expr(iterable, &|expr| expr.is_named_expr()) {
    return;
}

Now, it is placed outside of the branches, along with a new check for starred expressions:

for iterable in iterables {
    // For example, (x+1 for x in (c:=a)) is invalid syntax
    // so we can't suggest it.
    if any_over_expr(iterable, &|expr| expr.is_named_expr()) {
        return;
    }

    if iterable.is_starred_expr() {
        return;
    }
}

Similarly, other pieces that are applicable to all branches have also been moved into map_lambda_and_iterables().

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect and thanks for the explanations. They were very helpful when reviewing the PR

@MichaReiser MichaReiser enabled auto-merge (squash) February 7, 2025 08:55
@MichaReiser MichaReiser merged commit 7db5a92 into astral-sh:main Feb 7, 2025
20 checks passed
@InSyncWithFoo InSyncWithFoo deleted the C417-2 branch February 7, 2025 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants