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

Most upvoted comments

And I have a PR up to fix the preview gate, which should be out in the next patch release next week.

We can do a release after your fix is merged to get it out quickly.

Yeah, I think target-version or requires-python is 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.

# Assume Python 3.12
target-version = "py312"

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.toml etc. Clippy does something like

Hey @MichaReiser, @ntBre, thanks for the swift response. To reply to specifics:

This error is hard to debug as it

Do you have any suggestions on how we could improve the error message to make it easier?

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-version is required so ruff is 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?

but adding an empty tool.ruff table properly loads the requires-python value for me, at least in the CLI:

I think it’s because unless there’s a tool.ruff header in pyproject.toml we don’t read the requires-python field in it. This means that Ruff will target 3.9 (default) even though requires-python says >=3.12. This should be resolved by https://github.com/astral-sh/ruff/pull/16319 at least.

Do you have a [tool.ruff] section in your pyproject.toml? I was able to reproduce this with this file:

No, I did not have a [tool.ruff] section. Adding the empty section and commenting out the ruff.toml’s target-version reproduces the syntax error on match, but deleting the roff.toml completely while leaving the empty section induces a proper usage of the project’s requires-python.

This error is hard to debug as it

Do you have any suggestions on how we could improve the error message to make it easier?

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

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).

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)

Yes, setting the target-version to the python version you use (or use requires-python) is the right fix. It also ensures that the formatter and linter assume the right python version.