Ok, I think I found a way to make everyone happy. I have updated the issue and the PR with the solution.

Basically the idea is to add an attribute to the compiler that is checked when emitting the bytecode
and if is false, we stop don't emit anything but we do check for errors. Unless I am fundamentally wrong
(which is possible as is late here) is very simple and covers all the cases.

It would be great if someone could review the PR to check if I missed anything with this argument.

On Fri, 5 Jul 2019 at 22:28, Pablo Galindo Salgado <pablogsal@gmail.com> wrote:
Hi,

Recently, we moved the optimization for the removal of dead code of the form

if 0:
  ....

to the ast so we use JUMP bytecodes instead (being completed in PR14116). The reason is that
currently, any syntax error in the block will never be reported. For example:

if 0:
  return

if 1:
    pass
else:
    return

while 0:
    return

at module level do not raise any syntax error (just some examples), In https://bugs.python.org/issue37500 it was reported
that after that, code coverage will decrease as coverage.py sees these new bytecodes (even if they are not executed). In general,
the code object is a bit bigger and the optimization now it requires an JUMP instruction to be executed, but syntax errors are reported.

The discussion on issue 37500 is about if we should prioritize the optimization or the correctness of reporting syntax errors. In my opinion,
SyntaxErrors should be reported with independence of the value of variables (__debug__) or constant as is a property of the code being written
not of the code being executed. Also, as CPython is the reference implementation of Python, the danger here is that it could be interpreted that
this optimization is part of the language and its behavior should be mirrored in every other Python implementation. Elsewhere we have always
prioritize correctness over speed or optimizations.

I am writing this email to know what other people think. Should we revert the change and not report Syntax Errors on optimized blocks? Someone
sees a viable way of reporting the errors and not emitting the bytecode for these block?

Regards,
Pablo