Fix false SyntaxError for named expression as positional argument (f(a := 1, b))#238
Closed
apoorvdarshan wants to merge 1 commit into
Closed
Fix false SyntaxError for named expression as positional argument (f(a := 1, b))#238apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
parso raised a false 'SyntaxError: positional argument follows keyword argument' for valid calls such as f(a := 1, b). A named expression argument (name := value) was misclassified as a keyword argument because _ArglistRule treated every non-*/** argument as a keyword, so it set kw_only and flagged any following positional argument. Detect the ':=' operator and treat such arguments as positional. They no longer set kw_only, and they now correctly raise when they themselves follow a keyword argument or keyword-argument unpacking, matching CPython. Fixes davidhalter#212.
Owner
|
Hi, I'm sorry to say this, but I decided to not work at all with AI generated pull requests/content. There are multiple reasons for this:
Thank you anyway for trying to contribute to Open Source. I would really appreciate if you avoid the usage of LLMs in my projects. It is obviously fine to use LLMs as a search/brainstorming tool with my projects, just don't use it to interact with me. PS: As a side note I want to mention that I'm probably not the only maintainer that has a difficult relationship with LLM content. So you probably should ask first before you use LLMs for other repositories. |
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.
Summary
parso reported a false
SyntaxError: positional argument follows keyword argumentfor valid calls that use a named expression (walrus) as a positional argument, e.g.f(a := 1, b)(issue #212):CPython accepts
f(a := 1, b). The single-argument formf(a := 1)was already accepted, so only the "argument follows" ordering check was affected.Cause
In
_ArglistRule.is_issue(parso/python/errors.py), everyargumentnode that is not*/**unpacking and not a comprehension fell into theelse: # Is a keyword argumentbranch. A named expression argument (name := value) has the same top-level node type (argument) as a keyword argument (name = value), so it was misclassified as a keyword argument: it setkw_only = True, which then made any following positional argument raise.Fix
Detect the
:=operator (argument.children[1] == ':=') and treat such arguments as positional. They no longer setkw_only. They still correctly raise when a named expression itself follows a keyword argument or keyword-argument unpacking, matching CPython:f(a := 1, b)f(a := 1, x=2)f(b, a := 1)f(x=2, a := 1)positional argument follows keyword argumentf(**x, a := 1)positional argument follows keyword argument unpackingTests / Verification
test_valid_namedexprintest/test_python_errors.py(f(a := 1, b),f(a := 1, b=2),f(a := 1, *b),f(a := 1, **b),f(b, a := 1),f(*b, a := 1)). Thef(a := 1, b)case fails before the fix and passes after.f(x=2, a := 1),f(**x, a := 1)) totest/failing_examples.py, which are cross-checked against the running Python's own error message.1994 passed.flake8 --extend-ignore F401 parso test/*.py setup.py scripts/is clean.CHANGELOG.rstentry and my name toAUTHORS.txt.Fixes #212.
Disclosure: prepared with AI assistance; reviewed and verified locally.