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: def is not a keyword with tokenize.py
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, abarry, eric.smith, matrixise, yselivanov
Priority: normal Keywords:

Created on 2015-11-07 15:36 by matrixise, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (11)
msg254277 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-07 15:36
For tokenize.py, 'def' is not a keyword but just a simple identifier.

>>> tokens = tokenize.generate_tokens(io.StringIO('async def foo(): pass').readline)
>>> pprint.pprint(list(tokens))
[TokenInfo(type=55 (ASYNC), string='async', start=(1, 0), end=(1, 5), line='async def foo(): pass'),
 TokenInfo(type=1 (NAME), string='def', start=(1, 6), end=(1, 9), line='async def foo(): pass'),
 TokenInfo(type=1 (NAME), string='foo', start=(1, 10), end=(1, 13), line='async def foo(): pass'),
 TokenInfo(type=53 (OP), string='(', start=(1, 13), end=(1, 14), line='async def foo(): pass'),
 TokenInfo(type=53 (OP), string=')', start=(1, 14), end=(1, 15), line='async def foo(): pass'),
 TokenInfo(type=53 (OP), string=':', start=(1, 15), end=(1, 16), line='async def foo(): pass'),
 TokenInfo(type=1 (NAME), string='pass', start=(1, 17), end=(1, 21), line='async def foo(): pass'),
 TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')]
>>> 'def' in keyword.kwlist
True
>>> tokens = tokenize.generate_tokens(io.StringIO('def foo(): pass').readline)
>>> pprint.pprint(list(tokens))
[TokenInfo(type=1 (NAME), string='def', start=(1, 0), end=(1, 3), line='def foo(): pass'),
 TokenInfo(type=1 (NAME), string='foo', start=(1, 4), end=(1, 7), line='def foo(): pass'),
 TokenInfo(type=53 (OP), string='(', start=(1, 7), end=(1, 8), line='def foo(): pass'),
 TokenInfo(type=53 (OP), string=')', start=(1, 8), end=(1, 9), line='def foo(): pass'),
 TokenInfo(type=53 (OP), string=':', start=(1, 9), end=(1, 10), line='def foo(): pass'),
 TokenInfo(type=1 (NAME), string='pass', start=(1, 11), end=(1, 15), line='def foo(): pass'),
 TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')]
>>>

Is it normal ?
msg254279 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2015-11-07 15:52
>>> keyword.iskeyword('pass')
True

There doesn't seem anything particular about def, none of the keywords have "keyword" type, because there doesn't seem to be one: https://docs.python.org/3.6/library/token.html
msg254280 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-07 15:59
why do you close it ?

def is not a keyword and it's a problem, how do you define a function is def is not a keyword.

async is not a keyword but if you used it before 'def', async will become a keyword.


have you checked the output from the tokenizer ? in the output, def is a NAME and not DEF.
msg254284 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2015-11-07 16:07
Perhaps, my example wasn't as self-explanatory as I hoped. def is not DEF in the token list, because there isn't such a token DEF, as there isn't a token PASS or a token for any other keyword. The fact that there is an ASYNC and AWAIT tokens is something completely different and is not related to keywords as such. The link I provided lists all the tokens in the token module.
msg254302 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-11-07 22:04
I think the error here is that tokenize returns type=ASYNC for 'async', instead of type=NAME.
msg254304 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-07 22:06
how to know if it's an issue or not? we can ask to Yury
msg254305 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-11-07 22:07
It does not currently work, because the docstring logic looks for a string, not an expression. And an f-string is an expression.

It would require changing the compiler to evaluate the f-string expression.
msg254306 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2015-11-07 22:08
??
msg254307 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2015-11-07 22:09
He probably mistook this for #25179
msg254308 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-11-07 22:10
Oops, wrong issue. Ignore my last comment. I shouldn't do two things at once.
msg254579 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-11-13 01:00
Stéphane, sorry for not replying earlier, emails from bugs.python.org sometimes go to spam.

[TokenInfo(type=55 (ASYNC), string='async', start=(1, 0), end=(1, 5), line='async def foo(): pass'),
 TokenInfo(type=1 (NAME), string='def', start=(1, 6), end=(1, 9), line='async def foo(): pass'),

^-- this is indeed correct.  Right now async/await are treated specially in tokenizer.c & parser, and tokenize.py simply mimics their behaviour.
History
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69765
2015-11-13 01:00:18yselivanovsetmessages: + msg254579
2015-11-07 22:10:16eric.smithsetmessages: + msg254308
2015-11-07 22:09:22abarrysetnosy: + abarry
messages: + msg254307
2015-11-07 22:08:42matrixisesetmessages: + msg254306
2015-11-07 22:07:43eric.smithsetmessages: + msg254305
2015-11-07 22:06:36matrixisesetnosy: + yselivanov
messages: + msg254304
2015-11-07 22:04:15eric.smithsetnosy: + eric.smith
messages: + msg254302
2015-11-07 16:07:18SilentGhostsetmessages: + msg254284
2015-11-07 15:59:11matrixisesetmessages: + msg254280
2015-11-07 15:52:28SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg254279

resolution: not a bug
stage: resolved
2015-11-07 15:36:37matrixisecreate