Hello,
On Tue, 19 Jan 2021 08:54:33 +0900
Inada Naoki 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
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
___
Python-ideas mailing list --
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
wrote:
> Doesn't explicitly setting it yourself still work?
>
> ```
> TYPE_CHECKING = False
>
> if TYPE_CHECKING:
> import xxx # modules
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).