Gregory P. Smith <g...@krypto.org> added the comment:

If the body of a conditional does nothing, it seems fine to optimize the 
condition out to me.  But I see code from a low level compiled language 
perspective where that is clearly what would happen.  In reality, who ever 
meaningfully writes code where the body of a conditional does nothing?

 * Placeholder code with a # TODO perhaps.  [fine to optimize out]
 * Unit tests attempting to test the behavior of __bool__().  [an annoying 
behavior change]

Are there others?  Are we expecting this odd "not quite a no-op because we're 
so high level" pattern to ever appear in a performance critical situation?

The workaround for the latter would be to explicitly `if bool(x):` instead of 
`if x:` when the body is a no-op.  Or not make the body a no-op.  I expect 
unittest of __bool__() code owners would be fine with that so long as we call 
it out clearly in What's New docs, it's just that it could be an annoying 
change for them to make.

Ideally we'd also provide a lib2to3 fixer to detect and fixup code exhibiting 
that pattern.

The easiest answer is just not to optimize this out if it isn't actually 
providing us anything deemed important.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42899>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to