TYPE_CHECKING
typing.
TYPE_CHECKING
A special constant that is assumed to be True
by 3rd party static type checkers. It is False
at runtime. Usage:
if TYPE_CHECKING:
import expensive_mod
def fun(arg: 'expensive_mod.SomeType') -> None:
local_var: expensive_mod.AnotherType = other_fun()
The first type annotation must be enclosed in quotes, making it a “forward reference”, to hide the expensive_mod
reference from the interpreter runtime. Type annotations for local variables are not evaluated, so the second annotation does not need to be enclosed in quotes.
pytest的示例
import os
import warnings
from pathlib import Path
from typing import Callable
from typing import Iterable
from typing import Iterator
from typing import List
from typing import Optional
from typing import overload
from typing import Set
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
from typing import Union
import py
import _pytest._code
from _pytest._code import getfslineno
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import TerminalRepr
from _pytest.compat import cached_property
from _pytest.config import Config
from _pytest.config import ConftestImportFailure
from _pytest.deprecated import FSCOLLECTOR_GETHOOKPROXY_ISINITPATH
from _pytest.mark.structures import Mark
from _pytest.mark.structures import MarkDecorator
from _pytest.mark.structures import NodeKeywords
from _pytest.outcomes import fail
from _pytest.pathlib import absolutepath
from _pytest.store import Store
if TYPE_CHECKING:
# Imported here due to circular import.
from _pytest.main import Session
from _pytest.warning_types import PytestWarning
from _pytest._code.code import _TracebackStyle
文件链接
https://github.com/pytest-dev/pytest/blob/master/src/_pytest/nodes.py