logilab-astng #8771 crash on yield expression [resolved]

Maarteen on python-projects...

Please consider this program:

def f():
    yield 45
    x = yield 123

When running pylint on it, it crashes with this backtrace:

Traceback (most recent call last):
 File "/usr/bin/pylint", line 4, in <module>
   lint.Run(sys.argv[1:])
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 881, in __init__
   linter.check(args)
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 500, in check
   self.check_astng_module(astng, checkers)
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 577, in check_astng_module
   if implements(checker, IASTNGChecker)])
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 594, in astng_events
   self.astng_events(child, checkers, _reversed_checkers)
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 594, in astng_events
   self.astng_events(child, checkers, _reversed_checkers)
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 594, in astng_events
   self.astng_events(child, checkers, _reversed_checkers)
 File "/usr/lib/python2.5/site-packages/pylint/lint.py", line 591, in astng_events
   checker.visit(astng)
 File "/usr/lib/python2.5/site-packages/logilab/astng/utils.py", line 320, in visit
   method(node)
 File "/usr/lib/python2.5/site-packages/pylint/checkers/format.py", line 267, in visit_default
   prev_sibl = node.previous_sibling()
 File "/usr/lib/python2.5/site-packages/logilab/astng/nodes.py", line 251, in previous_sibling
   stmts = self.parent.child_sequence(self)
 File "/usr/lib/python2.5/site-packages/logilab/astng/nodes.py", line 216, in child_sequence
   raise ASTNGError(msg % (repr(child), repr(self)))
logilab.astng._exceptions.ASTNGError: Could not found <_ast.Yield object at 0x1034890> in <_ast.Assign object at 0x1034810>'s children

In previous_sibling(), the Yield node is looked up in its parent. That works for line 2 where yield is used as a statement, but fails for line 3 where yield is used as an expression.

The _ast module always considers yield an expression (both for the line 2 and line 3 case). Taking that as inspiration, I moved "Yield" from STMT_NODES to ALL_NODES in "nodes.py" in astng. That fixes the crash, but I don't know if there is other code that will break when "yield" is never considered a statement.

By the way, the docstring of previous_sibling() looks like it belongs with next_sibling() and vice versa.

prioritynormal
typebug
done in0.19.1
load1.000
load left0.000
closed by<not specified>