simonpercivall/astunparse

_Return Function does not check if a return statement has a value in unparse.py

Closed this issue · 2 comments

It is legal to do this
'''
def f():
return
'''
A fix for the _Return function is this:
'''
def _Return(self, t):
self.fill("return")
if hasattr(t, 'value'):
if t.value:
self.write(" ")
self.dispatch(t.value)
'''

it works as expected on "valid" nodes.

>>> print(astunparse.unparse(ast.parse("def x(): return")))
def x():
    return

Huh, interesting. I wrote a test for it and I could've swore it failed but I re-tested it and it passed. I thought I hit a corner case with this, sorry about that.