With the acceptance of PEP 563 and its soon-to-be-default status, I was thinking that maybe typing.TYPE_CHECKING
could be made a built-in name to allow for situations where typing
would not have to be imported at all at runtime. This would work well in code that is only using simple type hints that doesn’t need access to any of the functions in typing
.
Consider the following toy example:
from __future__ import annotations
from typing import TYPE_CHECKING, Union, Tuple
if TYPE_CHECKING:
from circular.module import circular_import
class A():
def __init__(self) -> None:
pass
def spam(x: circular_import) -> A:
return x.spam()
def eggs(x: int) -> Union[Tuple[int,int], Tuple[str,str]]:
...
With the annotations as strings, that means we’re not actually using Union
and Tuple
at runtime. We could move those imports into the if TYPE_CHECKING
block like so:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Union, Tuple
from circular.module import circular_import
but then we have multiple from typing import
statements and we still have to import the whole typing
module just to get TYPE_CHECKING
. If TYPE_CHECKING
was always available, we could have code like this in a future Python:
if TYPE_CHECKING:
from typing import Union, Tuple
from circular.module import circular_import
class A():
def __init__(self) -> None:
pass
def spam(x: circular_import) -> A:
return x.spam()
def eggs(x: int) -> Union[Tuple[int,int], Tuple[str,str]]:
...
What do you think? Is this worthwhile or is it a premature optimization?