Fix unnecessary-direct-lambda-call when the lambda supplies a class-body scope - #11337
Open
chiruu12 wants to merge 2 commits into
Open
Fix unnecessary-direct-lambda-call when the lambda supplies a class-body scope#11337chiruu12 wants to merge 2 commits into
chiruu12 wants to merge 2 commits into
Conversation
DanielNoord
previously approved these changes
Aug 26, 2026
Collaborator
|
@chiruu12 Can you fix the spelling issues? Either by updating the dictionary we use or by changing the wording? :) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11337 +/- ##
==========================================
+ Coverage 96.42% 96.43% +0.01%
==========================================
Files 178 178
Lines 20112 20144 +32
==========================================
+ Hits 19392 19425 +33
+ Misses 720 719 -1
🚀 New features to boost your workflow:
|
This comment has been minimized.
This comment has been minimized.
Author
|
Done, reworded. Happy to add the word to |
Collaborator
|
@Pierre-Sassoulas I'll be away for a bit. Can you review this and merge it? Thanks in advance! |
Member
|
Let's rebase on origin/main, the primer is a mess and brings a month of fixes :D |
Contributor
|
🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉 This comment was generated for commit 2a7a752 |
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.
Type of Changes
Description
Closes #9294.
unnecessary-direct-lambda-callfired on a directly called lambda whose body holds a comprehensioncontaining an assignment expression, inside a class body. Doing what the message says produces code
that does not compile:
PEP 572 forbids that combination, and the lambda is what supplies the intervening function scope.
Scope of the restriction
Checked each form rather than assuming, since the guard should be no wider than the rule:
So the new helper suppresses only when the call's frame is a
ClassDefand the lambda body holds acomprehension containing a
NamedExpr.One case is deliberately still reported:
Inlining the outer lambda leaves the comprehension inside the inner one, which keeps a scope of its
own, so that compiles and both calls stay flagged. The helper checks that the comprehension's
nearest enclosing lambda or function is the called lambda itself, not something further in.
isinstanceagainst concrete node types throughout, perAGENTS.md.Tests
Appended to
tests/functional/u/unnecessary/unnecessary_direct_lambda_call.py: the four suppressedcomprehension forms, plus four that must still report (bare walrus, plain comprehension, the nested
lambda above, and the same construct in a function rather than a class body). Expected output
regenerated with
--update-functional-output.The file fails on
mainand passes with the change.Validation
The 14 failures are pre-existing. I ran the same selection on
mainwith the change stashed anddiffed the failure lists: identical, same 14 names.
News fragment added at
doc/whatsnew/fragments/9294.false_positive.