Join GitHub today
Make binder handle unconditional ifs with no else #3567
This pull request fixes #2673.
The problem was that in the semantic analysis phase, we can only ever mark blocks as unreachable. So, if we have code of the form:
if typing.TYPE_CHECKING: assert isinstance(...)
...nothing happens since there's no "else" block to mark as unreachable, which means the binder sees nothing special about this if statement and consequently ignores the special conditional.
I decided to just add a dummy empty "else" block if one doesn't already exist and mark it as unreachable. Two other solutions I considered were to add another field somewhere to record reachability or to repeat the special conditional checks in the typechecking phase, but these solutions seemed a bit redundant/wasteful.
Also, to clarify, this fix applies to any if statements involving OS platform checks, Python version checks, "are we using mypy" checks, and the like.
I'm not sure if "unconditional ifs" is really the right phrase to use here, but naming things is hard...