This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Indentation explanation is unclear
Type: enhancement Stage:
Components: Documentation Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Mariatta Nosy List: Jérôme LAURENS, Mariatta, docs@python, leduyquang753, willingc
Priority: normal Keywords:

Created on 2019-02-10 17:26 by Jérôme LAURENS, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg335165 - (view) Author: Jérôme LAURENS (Jérôme LAURENS) Date: 2019-02-10 17:26
https://docs.python.org/3/reference/lexical_analysis.html#indentation reads

Point 1:
"Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight"

and in the next paragraph

Point 2:
"Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces"

In point 1, each tab has definitely a unique space counterpart, in point 2, tabs may have different space counterpart, which one is reliable ?

The documentation should state that Point 1 concerns cPython, or at least indicate that the 8 may depend on the implementation, which then gives sense to point 2.
msg335300 - (view) Author: Jérôme LAURENS (Jérôme LAURENS) Date: 2019-02-12 13:00
To be more precise, consider code

def f(x):
       \tx=0 # 7 spaces + one tab
        return x # 8 spaces

In cpython, both indentation levels are 8 and no indentation error is reported (this is the case where both tab size and alt tab size are equal)

If instead of 8 the tab would count for 6 spaces, then we would have 12 and 8 as indentation level, resulting in a mismatch and an indentation error being reported, according to the documentation. This is inconsistent.
Then either the documentation is faulty or cpython is.

Actually, cpython accepts a mix of space and tabs only when tabs are in 8, 16, 24... positions.
msg335892 - (view) Author: Carol Willing (willingc) * (Python committer) Date: 2019-02-19 06:32
Assigning this to @Mariatta, to be worked on the mentored sprint at PyCon US Cleveland. Verify behavior and update documentation.
msg404871 - (view) Author: Quang Lê Duy (leduyquang753) Date: 2021-10-23 12:51
Reading from the source code (Parser/tokenizer.c from line 1364 as in Python 3.10), I derive these as the actual indentation rules:
– Tab makes the indentation amount the next multiple of 8.
– Among lines with the same indentation amount in the same parent line, the total number of tabs and spaces must match, in other words, the tabs' total padded width must be equal.
– If it's an indent, the total number of spaces and tabs must exceed that of the parent line.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80138
2021-10-23 12:51:22leduyquang753setnosy: + leduyquang753
messages: + msg404871
2019-02-19 06:32:50willingcsetassignee: docs@python -> Mariatta

messages: + msg335892
nosy: + Mariatta, willingc
2019-02-12 13:00:45Jérôme LAURENSsetmessages: + msg335300
2019-02-10 17:26:57Jérôme LAURENScreate