mypyc
mypyc copied to clipboard
TYPE_CHECKING block leads to "Reached allegedly unreachable code!"
If a module contains a TYPE_CHECKING block, it can lead to a runtime error.
from __future__ import annotations
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
BaseDict = dict[Any, Any]
else:
BaseDict = dict
class MyDict(BaseDict):
pass
To reproduce
mypyc foo.py
python -c "import foo"
Error
Traceback (most recent call last):
File "<string>", line 1, in <module>
import foo
File "foo.py", line 8, in <module>
BaseDict = dict
RuntimeError: Reached allegedly unreachable code!
Hm, this is pretty bad, if TYPE_CHECKING: ... is a quite common pattern. I am surprised no one encountered this before. cc @JukkaL
We should probably treat TYPE_CHECKING as False when running the code. However, the else block in the example hasn't been fully processed by mypy, so compiling it may be tricky.