Repository navigation
B023: don't flag a lambda that is called where it is defined - #638
Merged
cooperlees merged 1 commit intoNov 18, 2026
Merged
Conversation
cooperlees
approved these changes
Nov 18, 2026
cooperlees
left a comment
Collaborator
There was a problem hiding this comment.
This review was written by Claude (Anthropic's AI assistant) on behalf of @cooperlees.
Thanks @Kernel-Hunter. This looks good to me, and the exemption is scoped about right.
What I checked
ast.walkis breadth-first, so theCallis visited before itsLambdachild. That means the lambda is already insafe_functionsby the time the "find unsafe functions" branch reaches it. 👍- The only expression-level constructs that delay evaluation past the call are nested lambdas,
yield/yield from, and generator expressions. Nested lambdas are still visited and checked on their own, and the other two are excluded by yourast.walkguard. List, set and dict comprehensions run eagerly, so it's right not to exclude them. - On the PR branch,
pytest tests -k "not selfclean"gives 161 passed, 3 skipped. - I also ran some extra cases against the branch:
for i in range(3): x = i (lambda: x)() # not flagged ✅ (lambda: [x for _ in "ab"])() # not flagged ✅ (listcomp is eager) (lambda: (lambda: x)())() # not flagged ✅ f = (lambda: x) # flagged ✅ (escapes) (lambda: sum(x for _ in "a"))() # flagged (see nit) (lambda: (lambda: (yield x)))() # flagged ✅
Non-blocking nit
The GeneratorExp guard is conservative. (lambda: sum(x for _ in y))() still warns even though the genexp is consumed before the lambda returns. That's a reasonable trade-off for keeping the check simple, and a false positive is better than a missed late-binding bug. It might be worth a short comment there so nobody "fixes" it later without thinking about the cases where the genexp escapes.
Approving.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
B023 flags a lambda that is called on the spot, even though it cannot outlive the iteration:
The lambda runs to completion before the next iteration, so reading
xis not the late-binding problem the rule is about. #567 already exempted a named function that is only called inside the loop, andmap/filter/reduce/key=lambdas are exempt too. This adds the same exemption for(lambda: ...)().I kept the exemption narrow. A lambda that yields, or whose body contains a generator expression, only builds a generator when it is called, so those are still reported, and so is a lambda returned from the called lambda. Cases for all of them are in
tests/eval_files/b023.py, and the README changelog has an entry. The new test cases fail without the change and the rest of the suite is unchanged (the twotest_selfclean_*tests fail on main in my checkout too, with a FileNotFoundError).