On Tue, 19 Jan 2021 08:54:33 +0900 Inada Naoki firstname.lastname@example.org wrote:
I want to write type hints without worrying about runtime overhead. Current best practice is:
from __future__ import annotations import typing if typing.TYPE_CHECKING: import xxx # modules used only in type hints.
But it would be nice if I can avoid importing even "typing" module. How about adding TYPE_CHECKING builtin that is False?
How about adding "very core", builtin, implemented in C, module for "language services"? It would have this var, get_annotations(), as language-level elaboration of typing.get_type_hints(), some core annotations (e.g. "const"), etc. Candidate names for such a module would be "lang", "python", or "__present__".
Again, this module would deal with *language* level matters. That would differentiate it clearly from "sys", which largely deals with *implementation* level matters. E.g. sys.settrace() - whether tracing is implemented, and details of it, is implementation-specific matter; sys.intern() - whether interning is implemented, and details of it, is implementation-specific matter. Etc.
Hmm, thinking about it, "is type checking is running currently" is also an implementation-specific matter (e.g., some implementations will run type checking always). So, sys.type_checking would be a place for that variable, sys being the builtin module, importing "instantly".
(But that doesn't reduce need for language-level services module.)