You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Treat TYPE_CHECKING = False as an internal const value #19734
In order to support type_checking, it is common to import types from the typing or typing_extension modules.
To minimize runtime impact; this is often done conditional :
TYPE_CHECKING=FalseifTYPE_CHECKING:
fromtypingimportList, Optional, Union# Add all your types here
While running type checking, TYPE_CHECKING is always True , so typecheckers see the imports, runtime avoids them.
Note: that while the spec mentions typing.TYPE_CHECKING in practice all current type checkers also support the bare TYPE_CHECKING to avoid needing to import the typing module completely.
While this limits runtime processing, the code is still emitted into the .mpy or memory.
If the parser/compiler could special case TYPE_CHECKING = False to be treated as / equivalent to : _TYPE_CHECKING = Const(False)
then these type checking guards, and the code branches inside them, would be completely removed from the emitted bytecode.
This would completely avoid runtime and firmware size overhead, exchanges for a additional complexity in the parser/compiler.
The same guard can be used multiple times, for instance to define abstract methods that can be used in static type checking, but do not grow the modules compiled size.
Activity