Introduce type keyword for type only imports

I generally think a lazy import mechanism is mostly sufficient for all existing use cases, and like others, do not think that forbidding evaluation of annotations is a good thing.

There are already many patterns that break runtime typechecking for the sake of performance because they use if TYPE_CHECKING: ... without a safe fallback value in the else branch, we should be striving for fewer cases like this, not more.

I would be mildly in favor of allowing combining type alias statements with imports as

type import foo

with no support for from / as imports, requiring secondary aliasing such as:

type import typing
type Any = typing.Any

as a signal that these are only intended for use in typing contexts, (and which would have some benefit, as wrapping like this prevents accidental use as a value in non-typing aware contexts, without locking out runtime typecheckers) but between this and pep 810, if we only have one, I would strongly prefer 810. The more general solution is more valuable.

8 Likes