python / python/cpython

TabError and IndentationError from line continuation

Open
#144,018 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-parser type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The documentation states:

Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

When this rule applies to some, but not all, lines in a block, a TabError is sometimes unexpectedly raised:

L1 = 'if 1:\n'
L2 = '    1\n'
L3 = '    \\\n'
L4 = '        1\n'
exec(L1 + L2 + L3 + L4)
# works as expected

L1 = 'if 1:\n'
L2 = '\t1\n'
L3 = '\t\\\n'
L4 = '\t\t1\n'
exec(L1 + L2 + L3 + L4)
# TabError: inconsistent use of tabs and spaces in indentation

Also, when a line containing only a backslash is explicitly joined with an indented line, an IndentationError is unexpectedly raised:

exec('\\\n    1')
# IndentationError: unexpected indent
CPython versions tested on:

3.10, 3.11, 3.12, 3.13, 3.14

Operating systems tested on:

macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two exec() reproducers on the listed CPython versions, then trace the indentation and line-continuation handling involved in both cases. Add regression coverage for the reported TabError and IndentationError behavior, and confirm the tests capture the intended results without changing unrelated indentation errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.