classification
Title: AST: add an attribute to FunctionDef to distinguish functions from generators and coroutines
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aptrishu, haypo, inada.naoki, mbdevpl, veky, yselivanov
Priority: normal Keywords:

Created on 2017-02-07 09:08 by haypo, last changed 2017-03-15 06:42 by mbdevpl.

Messages (4)
msg287216 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2017-02-07 09:08
Currently, symtable_visit_expr() has browse into the AST tree to look up yield, yield from or await to check if a function is a generator or a coroutine. If we choose to start to work on AST optimizers, I would suggest to add an attribute to ast.FunctionDef to announce if a function is a generator or a coroutine.

Currently, the peephole optimizer is unable to remove the deadcode, because otherwise the function is no more detected as a generator:

def generator():
  if 0: yield  # "hack" to get a generator
  pass

By the way, it would be nice to add a keyword similar to "async def" to be able to explicitly "document" generators as generators, and avoid the "if 0: yield" hack, but that's a different topic ;-) I feel less confortable to modify the Python language.
msg287221 - (view) Author: Vedran Čačić (veky) * Date: 2017-02-07 09:53
I remember the message from Guido, long time ago when the syntax of generators was discussed. He said he has a hunch that new keyword will not be necessary, that the presence of yield will be enough. It'd be interesting to see if he has changed his mind, or he has some other excuse about async being fundamentally different. :-)
msg288921 - (view) Author: Rishav Kumar (aptrishu) Date: 2017-03-03 20:21
I'd like to work on this issue.
msg288933 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-03-03 21:27
I'm not sure we need this feature TBH.
History
Date User Action Args
2017-03-15 06:42:54mbdevplsetnosy: + mbdevpl
2017-03-03 21:27:06yselivanovsetmessages: + msg288933
2017-03-03 20:21:40aptrishusetnosy: + aptrishu
messages: + msg288921
2017-02-07 09:53:51vekysetnosy: + veky
messages: + msg287221
2017-02-07 09:08:59haypocreate