You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AST: add an attribute to FunctionDef to distinguish functions from generators and coroutines #73657
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:
assignee=Noneclosed_at=<Date2018-09-19.23:29:22.395>created_at=<Date2017-02-07.09:08:59.541>labels= ['interpreter-core', 'type-feature', '3.7']
title='AST: add an attribute to FunctionDef to distinguish functions from generators and coroutines'updated_at=<Date2018-09-19.23:29:22.393>user='https://github.com/vstinner'
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:
defgenerator():
if0: yield# "hack" to get a generatorpass
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.
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. :-)
Activity
vstinner commentedon Feb 7, 2017
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:
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.
vedgar commentedon Feb 7, 2017
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. :-)
aptrishu commentedon Mar 3, 2017
I'd like to work on this issue.
1st1 commentedon Mar 3, 2017
I'm not sure we need this feature TBH.
vstinner commentedon Sep 19, 2018
Ok, I close the issue.