Skip to content

Check for late-binding closures (e.g. lambdas) in loops #263

@Zac-HD

Description

@Zac-HD

We just got bitten by the classic late-binding of loop variables used in lambdas at work, and I'd like to make sure this doesn't happen again.

Specifically, it should be an error to use any loop variable inside the body of a function (whether lambda ... or def ...) defined inside that loop, unless the same name is a parameter of that function. e.g. for x in ...: fns.append(lambda x=x: x**2) would be OK, but lambda: x**2 would not.

Loops include (async) for loops, and list/set/dict/generator comprehensions; but exclude while-loops as they have no clearly associated loop variables. While this can still be buggy, I'd like to keep the false-alarm rate as low as reasonably possible.

Related: #79, #175, and the much noisier wemake-style-guide LambdaInsideLoopViolation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions