ruff: `ruff` 0.9.8 raising error 'SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)' in Python 3.13
Summary
When running ruff check for the first time on a Python file (version 3.13.1) containing match statements (ie either when ruff has just been installed, or the file has just been created), the error
SyntaxError: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)
is returned. This error is hard to debug as it will not appear for subsequent runs of ruff check in the same repo (all checks pass) – I suspect this is why I’ve been unable to replicate in the playground
Replicated in Python 3.12 and Python 3.13 on Mac running Sequoia 15.3.1, and for the same Python versions on a Windows machine running WSL. MWE here: ruff-demo.zip
As a short-term fix you can either revert to ruff<=0.9.7 or include the line target-version = "py313" in your ruff config (I assume it would work for any version >3.9)
Version
ruff 0.9.8 (568cf88c6 2025-02-27)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 11
- Comments: 16 (11 by maintainers)
Commits related to this issue
- Show version-related syntax errors in the playground (#16419) ## Summary Fixes part of https://github.com/astral-sh/ruff/issues/16417 by converting `unsupported_syntax_errors` into playground diagno... — committed to astral-sh/ruff by ntBre 2 years ago
- Check `LinterSettings::preview` for version-related syntax errors Summary -- Fixes part of the report in https://github.com/astral-sh/ruff/issues/16417#issuecomment-2689600543 by checking `LinterSett... — committed to astral-sh/ruff by ntBre 2 years ago
We can do a release after your fix is merged to get it out quickly.
Yeah, I think
target-versionorrequires-pythonis currently necessary to specify a non-default value (we currently default to 3.9), but we may need to update that behavior, especially combined with these new errors. With that said, I think you’re right that the error here is that it doesn’t occur again 😆 . I’ll work on that caching and the playground integration for this issue and open a separate issue to discuss the version-awareness. It seems like we could do something better there too.That removed the error. I do hope that isn’t a ‘fix’ and is just a work-around.
This is probably somewhat involved but something that could help here as well is to explain why Ruff assumes Python 3.9.
E.g. Python 3.9 is the default or 3.9 is configured in
ruff.tomletc. Clippy does something likeHey @MichaReiser, @ntBre, thanks for the swift response. To reply to specifics:
the error message is perfectly clear, it’s just occurring in an invalid context (since I am on Python 3.13)
Is it the case that
target-versionis required soruffis aware of the error (so it is defaulting to below 3.10) or is there some other version-awareness present? If it is the case that it’s required then the only issue is the fact that the error doesn’t occur again 😃 If it isn’t the case that it’s required then I guess version-awareness is broken somehow?I think it’s because unless there’s a
tool.ruffheader inpyproject.tomlwe don’t read therequires-pythonfield in it. This means that Ruff will target3.9(default) even thoughrequires-pythonsays>=3.12. This should be resolved by https://github.com/astral-sh/ruff/pull/16319 at least.No, I did not have a
[tool.ruff]section. Adding the empty section and commenting out theruff.toml’starget-versionreproduces the syntax error onmatch, but deleting theroff.tomlcompletely while leaving the empty section induces a proper usage of the project’srequires-python.Do you have any suggestions on how we could improve the error message to make it easier?
This is interesting. That would suggest that we fail to cache the diagnostics correctly and it also seems that we missed to add the diagnostics to the playground. We should look into both (@ntBre).
Yes, setting the
target-versionto the python version you use (or userequires-python) is the right fix. It also ensures that the formatter and linter assume the right python version.