Setting TYPE_CHECKING in an assignment expression makes code unreachable
Description
if TYPE_CHECKING := False:
pass
Code in this statement becomes unreachable. It is unreachable, in reality, but since it is for the variable TYPE_CHECKING type checker should think that it is reachable, which does not happen.
TYPE_CHECKING needs to be imported from the typing module:
from typing import TYPE_CHECKING
if not TYPE_CHECKING:
pass
See https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING
Yes, but, setting the variable manually works too:
TYPE_CHECKING = False
if TYPE_CHECKING:
pass # code is reachable
It also makes the code faster because importing typing module is not required
Using assignment expression, however, stops the same code from being reachable for type checker. Using it can make code easier to read if, for example, this statement is only used once.
It happens in vanilla pyright, too. I created an issue on official pyright repository, but it got closed as not planned (by design). Should I close the issue on this repository too?
The fact that most issues on pyright get closed "as designed" is the main reason I forked it in the first place, so I'll leave this open. If a regular assignment to TYPE_CHECKING works without having to import it from typing I don't see why it shouldn't also work with an assignment expression