No, Discard is generated for any expression that has its result unused:
>>> compiler.parse("1")
Module(None, Stmt([Discard(Const(1))]))
>>> compiler.parse("def x(): yield 1")
Module(None, Stmt([Function(None, 'x', (), (), 0, None, Stmt([Discard(Yield(Const(1)))]))]))
For example, InteractiveCodeGenerator
emits a PRINT_EXPR
for Discard nodes, while normal CodeGenerator
just POP_TOP
s its result away.
Your "bad code" is not bad syntax-wise. Bad code will get you a SyntaxError
.