Specify `TYPE_CHECKING = False` without typing import

I prefer adding __type_checking__ constant, not builtin.

For example, SQLAlchemy is large library, and uses a lot of if TYPE_CHECKING:.
See elements.py for example.

When replacing if (typing\.)?TYPE_CHECKING with if False, bytecode size reduced about 4%.

$ wc -c *.pyc
  211087 elements.cpython-313.pyc  # original
  192163 elements_false.cpython-313.pyc  # replaced with `if False`

But if False is not looks good for readability.
This expression does not clearly express the intention.

Adding constant allow us to add rich type hints with zero runtime cost.

3 Likes