-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit diagnostics for new syntax as per the target Python version #6591
Comments
The Match and Type alias statement can be implemented in the AST Statement analyzer within the match arms of the respective statement and checking the I would suggest to add this as a new rule under the |
There might be other new syntax worth addressing:
Some of these might be closer to functional changes than syntax and hard to detect. But perhaps the easy ones could be folded in? There are advantages to python 3.12 that might make people want to develop with it even though their code is meant to run on python 3.9 (say). |
Just to say we have a use case for targeting 3.7. Thanks for the great work on this! |
So far, most new syntaxes were not "auto-applied" during formatting, but PEP-701 f-string placeholder updates are auto-applied to all codes. This prevents backporting those codes to older Python versions seamlessly as Ruff does not report syntax errors in older Python versions. Looking forward to see this issue to be resolved. 👀 |
We'd appreciate contributions here if someone is interested in working on the project. We can do this relatively incrementally, but there is some initial complexity to determine how diagnostics are propagated. @dhruvmanila / @charliermarsh will provide some additional details. |
@td-anne For visibility, I've updated the PR description based on your list but this issue is mainly focused on the syntax itself and not the semantic meaning. For example, dictionary union (
But, the following can possibly be done as a separate rule and is not part of what we're proposing here.
Can you expand on the following? I don't see any new syntax related to PEP 617.
|
These did involve the introduction of some new syntax: unpacking using |
I see. Thanks for pointing that out. I'll update the PR description. |
The goal here is to update Ruff to detect syntax which is invalid as per the
For (1),
For (2),
I'm more leaning towards (2). An open question here is to whether implement this as a new rule or add it to |
I support detecting these syntax errors via the linter (Option (2)) wherever possible. I think it'll be great to have these errors be I'd much prefer to have these syntax errors be detected as part of |
I think one key difference between the two is that you should never suppress a syntax error that is an error regardless of the python version (I don't even know if we support suppressing them). I don't know if there are good reasons for suppressing python-version specific syntax errors but I could see an argument for that. I don't think these should be suppressed by inline noqa comments but a user might decide to suppress them with |
The only reasons I can see for wanting to suppress a syntax error diagnostic are:
If you have the right target version in your config file, I can't personally see any reason why you'd want to be able to suppress Python-version-specific syntax error diagnostics, or treat them any differently from syntax that's invalid on all Python versions. |
Any updates on this? We are still holding back the ruff version due to the f-string backporting issue. |
Once astral-sh/ruff#6591 is fixed, ruff will do this. (and I should also have CI, but meh)
Our parser doesn't take into account the Python version aka
target-version
setting while parsing the source code. This means that we would allow having a match statement when the target Python version is 3.9 or lower. We want to signal this to the user.One solution is to provide a diagnostic after the parsing is done. This diagnostic would indicate to the user that there's a syntax usage which isn't valid for the current target version. This is what's being done in Rome and Pyright1. We would still continue linting the file and emit other diagnostics as well.
Following is a non-exhaustive list of syntax changes with the minimum required Python version:
3.13
3.12
type
statement andtype
parameter lists3.11
except*
syntax*args
via PEP 6463.10
match
statement3.9
3.8
Footnotes
Pyright: Type alias statement requires Python 3.12 or newer
↩The text was updated successfully, but these errors were encountered: