Repository navigation
gh-137570: Improve syntax error for unparenthesized assignment expression in lambda - #161927
Open
flxamartinez wants to merge 1 commit into
Open
flxamartinez wants to merge 1 commit into
flxamartinez wants to merge 1 commit into
Conversation
…expression in lambda lambda x: foo := 2 used to report 'cannot use assignment expressions with lambda', which suggests that assignment expressions are not allowed in a lambda at all. It now reports 'cannot use named expression without parentheses here', the same message used for assert x := 1.
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.
Before:
After:
The old message makes it look like you can't use := in a lambda, but you can if you add parentheses:
lambda x: (foo := 2).I added a rule for
lambda ...: name := ...before the general one, so now it gives the same message asassert x := 1.I also changed
test_named_expression_invalid_15because it was checking the old message.This only fixes the lambda case.
f(lambda: x := 1)still says "invalid syntax", andlambda x: foo.bar := 2keeps the old message because parentheses don't fix that one. I saw that @brianschubert's idea in #146601 also covers this case.