python / cpython

The Python programming language
https://www.python.org
Other
63.35k stars 30.32k forks source link

Indentation explanation is unclear #80138

Open b171d146-7152-4ccc-b3b9-0aa94aa2a4af opened 5 years ago

b171d146-7152-4ccc-b3b9-0aa94aa2a4af commented 5 years ago
BPO 35957
Nosy @willingc, @Mariatta

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = 'https://github.com/Mariatta' closed_at = None created_at = labels = ['type-feature', '3.7', 'docs'] title = 'Indentation explanation is unclear' updated_at = user = 'https://bugs.python.org/JrmeLAURENS' ``` bugs.python.org fields: ```python activity = actor = 'leduyquang753' assignee = 'Mariatta' closed = False closed_date = None closer = None components = ['Documentation'] creation = creator = 'J\xc3\xa9r\xc3\xb4me LAURENS' dependencies = [] files = [] hgrepos = [] issue_num = 35957 keywords = [] message_count = 4.0 messages = ['335165', '335300', '335892', '404871'] nosy_count = 5.0 nosy_names = ['docs@python', 'willingc', 'Mariatta', 'J\xc3\xa9r\xc3\xb4me LAURENS', 'leduyquang753'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue35957' versions = ['Python 3.7'] ```

b171d146-7152-4ccc-b3b9-0aa94aa2a4af commented 5 years ago

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.

b171d146-7152-4ccc-b3b9-0aa94aa2a4af commented 5 years ago

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.

willingc commented 5 years ago

Assigning this to @Mariatta, to be worked on the mentored sprint at PyCon US Cleveland. Verify behavior and update documentation.

f1c5238c-be12-403a-ac82-49bf8ce34cc4 commented 3 years ago

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.