Skip to content

Ruff should warn on all CPython SyntaxWarnings #20196

@JelleZijlstra

Description

@JelleZijlstra

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 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 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
    • Not implemented in Ruff. See this thread and linked discussions for background.
  • Invalid escape sequences in strings (e.g., "\99") string_parser.c
    • 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)
    • Also W605, but uses a different code path in CPython.
  • Control flow in a finally block (see PEP 765) (ast_preprocess.c)
    • Ruff B012.
  • is / is not with a literal (() is ()) codegen.c
    • Ruff F632.
  • assert that is always true (assert (1,)) codegen.c
    • Ruff F631.
  • Calling something that definitely isn't callable ({}()) codegen.c
    • 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
    • Not implemented in Ruff; similar considerations to the last one.
  • Using a wrong index type for some types ("a"["b"]) codegen.c
    • Ruff RUF016.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-designNeeds further design before implementationruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions