[New-bugs-announce] [issue29471] AST: add an attribute to FunctionDef to distinguish functions from generators and coroutines

STINNER Victor report at bugs.python.org
Tue Feb 7 04:08:59 EST 2017


New submission from STINNER Victor:

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.

----------
components: Interpreter Core
messages: 287216
nosy: haypo, inada.naoki, yselivanov
priority: normal
severity: normal
status: open
title: AST: add an attribute to FunctionDef to distinguish functions from generators and coroutines
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29471>
_______________________________________


More information about the New-bugs-announce mailing list