Making TYPE_CHECKING builtin.

Hi, all. 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? ``` from __future__ import annotations if TYPE_CHECKING: from typing import Any, Optional # We can use Any, Optional, etc here. ``` I wonder if we can make TYPE_CHECKING constant like True, False, and None. But it will break existing `from typing import TYPE_CHECKING` codes. Regards, -- Inada Naoki <songofacandy@gmail.com>

Doesn't explicitly setting it yourself still work? ``` TYPE_CHECKING = False if TYPE_CHECKING: import xxx # modules used only in type hints. ``` This seems to work for mypy, at least. Even just doing `if False:` works correctly (and is arguably the most efficient at runtime).

That's a mypy-specific hack, not something that's guaranteed by a PEP -- but it works great with mypy! On Mon, Jan 18, 2021 at 4:40 PM Brandt Bucher <brandtbucher@gmail.com> wrote:
Doesn't explicitly setting it yourself still work?
``` TYPE_CHECKING = False
if TYPE_CHECKING: import xxx # modules used only in type hints. ```
This seems to work for mypy, at least. Even just doing `if False:` works correctly (and is arguably the most efficient at runtime). _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/P5W5ET... Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>

Thank you! I didn't know that. I will use `if False: # TYPE_CHECKING` so the compiler will remove all imports inner it. But the official way is preferred so that all typing ecosystems follow it. -- Inada Naoki <songofacandy@gmail.com>

Hello, On Tue, 19 Jan 2021 08:54:33 +0900 Inada Naoki <songofacandy@gmail.com> wrote:
Hi, all.
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.) [] -- Best regards, Paul mailto:pmiscml@gmail.com
participants (4)
-
Brandt Bucher
-
Guido van Rossum
-
Inada Naoki
-
Paul Sokolovsky