Skip to content

Syntax errors related to indentation after backslashes #19301

@dscorbett

Description

@dscorbett

Summary

Ruff has false positives and false negatives when detecting syntax errors related to indentation after backslashes. Python’s documentation says “the whitespace up to the first backslash determines the indentation”, so the leading white space of the next line shouldn’t cause a syntax error, though this principle has been interpreted inconsistently in different Python versions. See python/cpython#90249.

False positive (unexpected indentation) (in all Python versions):

$ cat >test_1.py <<'# EOF'
if True:
    pass
    \
        print("1")
# EOF

$ python3.13 test_1.py
1

$ ruff --isolated check test_1.py --output-format concise -q --target-version py313
test_1.py:3:1: SyntaxError: Unexpected indentation
test_1.py:5:1: SyntaxError: Expected a statement

False positive (missing indentation) (except in Python 3.9 only, where it is a true positive):

$ cat >test_2.py <<'# EOF'
if True:
    \
print("2")
# EOF

$ python3.13 test_2.py
2

$ ruff --isolated check test_2.py --output-format concise -q --target-version py313
test_2.py:3:1: SyntaxError: Expected an indented block after `if` statement

$ python3.9 test_2.py 2>&1 | tail -n 1
IndentationError: expected an indented block

$ ruff --isolated check test_2.py --output-format concise -q --target-version py39
test_2.py:3:1: SyntaxError: Expected an indented block after `if` statement

False negative (unexpected indentation) (except in Python 3.9 and earlier, where it is a true negative):

$ cat >test_3.py <<'# EOF'
\
    \
print("3")
# EOF

$ python3.13 test_3.py 2>&1 | tail -n 1
IndentationError: unexpected indent

$ ruff --isolated check test_3.py --target-version py313
All checks passed!

$ python3.9 test_3.py
3

$ ruff --isolated check test_3.py --target-version py39
All checks passed!

False positive (unexpected indentation) (except in Python 3.10 and later, where it is a true positive):

$ cat >test_4.py <<'# EOF'
\
    print("4")
# EOF

$ python3.9 test_4.py
4

$ ruff --isolated check test_4.py --output-format concise -q --target-version py39
test_4.py:1:1: SyntaxError: Unexpected indentation
test_4.py:3:1: SyntaxError: Expected a statement

$ python3.10 test_4.py 2>&1 | tail -n 1
IndentationError: unexpected indent

$ ruff --isolated check test_4.py --output-format concise -q --target-version py310
test_4.py:1:1: SyntaxError: Unexpected indentation
test_4.py:3:1: SyntaxError: Expected a statement

False positive (no matching indentation) (in all Python versions):

$ cat >test_5.py <<'# EOF'
if True:
    pass
    \
  print("5")
# EOF

$ python3.13 test_5.py
5

$ ruff --isolated check test_5.py --output-format concise -q --target-version py313
test_5.py:3:1: SyntaxError: unindent does not match any outer indentation level
test_5.py:5:1: SyntaxError: Expected dedent, found end of file

Version

ruff 0.12.3 (5bc81f2 2025-07-11)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingparserRelated to the parser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions