Skip to content

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Mar 3, 2025

Summary

Unlike the other syntax errors detected so far, parenthesized keyword arguments are only allowed before 3.8. It sounds like they were only accidentally allowed before that 1.

As an aside, you get a pretty confusing error from Python for this, so it's nice that we can catch it:

>>> def f(**kwargs): ...
... f((a)=1)
...
  File "<python-input-0>", line 2
    f((a)=1)
       ^^^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>>

Test Plan

Inline tests.

Footnotes

  1. https://github.com/python/cpython/issues/78822

Summary
--

Unlike the other syntax errors detected so far, parenthesized keyword arguments
are only allowed *before* 3.8. It sounds like they were only accidentally
allowed before that [^1].

I wanted to mark the whole parenthesized range instead of just the kwarg name,
so I moved the `ParsedExpr::is_parenthesized` bool to an `Option<TextRange>` and
replaced it with an `is_parenthesized` method.

Test Plan
--
Inline tests.

[^1]: python/cpython#78822
@ntBre ntBre added parser Related to the parser preview Related to preview mode features labels Mar 3, 2025
Copy link
Contributor

github-actions bot commented Mar 3, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still slightly prefer to not store the parenthesized range to make this PR smaller and avoid storing information that can be retrieved from the parser already but I'm happy to go with what you / @dhruvmanila think is best

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

As suggested #16482 (comment), I'd get the argument range locally using node_range and use that instead.

@ntBre ntBre changed the title [syntax-errors] Parenthesized keyword arguments after Python 3.8 [syntax-errors] Parenthesized keyword argument names after Python 3.8 Mar 6, 2025
@ntBre ntBre merged commit b3c884f into main Mar 6, 2025
21 checks passed
@ntBre ntBre deleted the brent/syn-paren-kwarg branch March 6, 2025 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Related to the parser preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants