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: Tokenize generates NL instead of NEWLINE for comments
Type: Stage: resolved
Components: Documentation Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Optimal BPM, docs@python, petr.viktorin, skrah
Priority: normal Keywords:

Created on 2015-09-12 09:49 by Optimal BPM, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg250516 - (view) Author: Optimal BPM (Optimal BPM) Date: 2015-09-12 09:49
Quoting the documentation:

tokenize.NL
Token value used to indicate a non-terminating newline. The NEWLINE token indicates the end of a logical line of Python code; NL tokens are generated when a logical line of code is continued over multiple physical lines.

This doesn't seem to be entirely true.
At the end of comments, an .NL, not .NEWLINE is generated:
TokenInfo(type=55 (NL), string='\n', start=(5, 22), end=(5, 23), line='# Some docs for the IF\n')

As a comment cannot extend over several lines, if would appear that a NEWLINE would be appropriate?
msg250517 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2015-09-12 12:04
See:

https://docs.python.org/3/reference/lexical_analysis.html#blank-lines
msg251114 - (view) Author: Optimal BPM (Optimal BPM) Date: 2015-09-19 20:37
OK.
But just to make me understand, would that explain why an else statement gets its DEDENT before the else, instead of before an dedented comment just before it? 
Even if the comment is visually dedented as the else?

Example where tokenize returns the DEDENT in a separate place than where it is visually:

if a=b:
  statement
# Comment
else:
  other statement

In the above case, the DEDENT is reportet before the else, instead of before the comment.
msg251159 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2015-09-20 09:30
As it says in the docs, a "logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored."

If you write:

if a=b:
    statement
# Comment
    another statement

both statements belong to the "if" block.
In your example, the comment is similarly part of the "if" block, so the dedent comes after it.
msg251768 - (view) Author: Optimal BPM (Optimal BPM) Date: 2015-09-28 13:45
Ok, I'll work around that then. 
Thanks for your response!
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69266
2015-09-28 13:45:20Optimal BPMsetmessages: + msg251768
2015-09-20 09:30:17petr.viktorinsetnosy: + petr.viktorin
messages: + msg251159
2015-09-19 20:37:10Optimal BPMsetmessages: + msg251114
2015-09-12 12:04:41skrahsetstatus: open -> closed

type: behavior ->
assignee: docs@python
components: + Documentation, - Interpreter Core

nosy: + docs@python, skrah
messages: + msg250517
resolution: not a bug
stage: resolved
2015-09-12 09:49:19Optimal BPMcreate