pythonruff

How can I make "ruff check" assume a specific Python version for allowed syntax?


I am on Linux, created a Python 3.9 venv, installed ruff in the venv, wrote this code:

def process_data(data: list[int]) -> str:
    match data:
        case []:
            return "No data"
        case [first, *_] if (average := lambda: sum(data) / len(data)) and average() > 50:
            return f"Data average is high: {average():.2f}, starting with {first}"
        case _:
            return f"Processed {len(data)} items."

The match syntax is not available in Python 3.9, so running ruff check, I would expect an error. I have tried to set project.requires-python and ruff.target-version but the latter seems to be used only for the formatter according to the docs. What am I missing?


Solution

  • Ruff doesn't support that (yet?).

    There's an open issue for precisely this problem.

    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.