astral-sh / astral-sh/ruff

Ruff should warn on all CPython SyntaxWarnings

Open
#20,196 0 comments 4 reactions 0 assignees View on GitHub
needs-design rule
Dominant language
Rust
Stars
49.7k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
458

Description

### Summary

CPython emits a SyntaxWarning for certain code, but SyntaxWarnings have some issues that can make them difficult to reliably detect in CI (current discussion in [https://discuss.python.org/t/pep-765-disallow-return-break-continue-that-exit-a-finally-block/71348](discuss.python.org) about PEP 765). So it would be nice if Ruff could guarantee that it emits an error on any code for which CPython would produce a SyntaxWarning.

Here is a [playground link](https://play.ruff.rs/0437f5dd-3ea3-463c-b27e-71814287282e) covering all current SyntaxWarnings I could find in the CPython source code. Ruff already warns for most of them, though the logic may not always match exactly.

* Invalid numeric literals (e.g., `123or 1`) [lexer.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Parser/lexer/lexer.c#L346)
* Not implemented in Ruff. See [this thread](https://discuss.python.org/t/whats-going-on-with-syntaxwarning-invalid-decimal-literal/52694) and linked discussions for background.
* Invalid escape sequences in strings (e.g., `"\99"`) [string_parser.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Parser/string_parser.c#L47)
* Ruff W605. Might need auditing of the code to verify this matches the CPython logic exactly.
* Backslash next to `{` or `}` in f-strings or t-strings (e.g., `f"\{1}"`) ([lexer.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Parser/lexer/lexer.c#L1578))
* Also W605, but uses a different code path in CPython.
* Control flow in a `finally` block (see PEP 765) ([ast_preprocess.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Python/ast_preprocess.c#L71))
* Ruff B012.
* `is` / `is not` with a literal (`() is ()`) [codegen.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Python/codegen.c#L1836)
* Ruff F632.
* `assert` that is always true (`assert (1,)`) [codegen.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Python/codegen.c#L2951)
* Ruff F631.
* Calling something that definitely isn't callable (`{}()`) [codegen.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Python/codegen.c#L3650)
* Not implemented in Ruff. Type checkers will surely catch this (and more), but it may be valuable to add a rule specifically for the SyntaxWarning-triggering code here.
* Subscripting something that definitely is not subscriptable (`{1}[1]`) [codegen.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Python/codegen.c#L3679)
* Not implemented in Ruff; similar considerations to the last one.
* Using a wrong index type for some types (`"a"["b"]`) [codegen.c](https://github.com/python/cpython/blob/2a54acf3c3d9f388c3d878a17ea804a801affca9/Python/codegen.c#L3715)
* Ruff RUF016.

Contributor guide

Open the contributing guide

Research direction

Start with the linked CPython lexer.c, string_parser.c, ast_preprocess.c, and codegen.c locations, then compare the existing Ruff rules W605, B012, F632, F631, and RUF016 against the playground examples. Review the missing invalid-literal, non-callable, and non-subscriptable cases and determine the coverage needed. Done means Ruff reliably reports every listed case with matching tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.