Skip to content

Commit 2496ab4

Browse files
author
Guido van Rossum
committed
Add TYPE_CHECKING constant. Fixes #230.
1 parent 8ab2166 commit 2496ab4

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎pep-0484.txt‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,31 @@ Don't expect a checker to understand obfuscations like
983983
``"".join(reversed(sys.platform)) == "xunil"``.
984984

985985

986+
Runtime or type checking?
987+
-------------------------
988+
989+
Sometimes there's code that must be seen by a type checker (or other
990+
static analysis tools) but should not be executed. For such
991+
situations the ``typing`` module defines a constant,
992+
``TYPE_CHECKING``, that is considered ``True`` during type checking
993+
(or other static analysis) but ``False`` at runtime. Example::
994+
995+
import typing
996+
997+
if typing.TYPE_CHECKING:
998+
import expensive_mod
999+
1000+
def a_func(arg: 'expensive_mod.SomeClass') -> None:
1001+
a_var = arg # type: expensive_mod.SomeClass
1002+
...
1003+
1004+
(Note that the type annotation must be enclosed in quotes, making it a
1005+
"forward reference", to hide the ``expensive_mod`` reference from the
1006+
interpreter runtime. In the ``# type`` comment no quotes are needed.)
1007+
1008+
This approach may also be useful to handle import cycles.
1009+
1010+
9861011
Arbitrary argument lists and default argument values
9871012
----------------------------------------------------
9881013

@@ -1174,7 +1199,7 @@ in expressions, while type comments only apply to assignments.
11741199

11751200

11761201
NewType helper function
1177-
-----------------------
1202+
=======================
11781203

11791204
There are also situations where a programmer might want to avoid logical
11801205
errors by creating simple classes. For example::
@@ -1644,6 +1669,8 @@ Convenience definitions:
16441669
forward references (which are given as string literals) as expressions
16451670
in the context of the original function or method definition.
16461671

1672+
* TYPE_CHECKING, ``False`` at runtime but ``True`` to type checkers
1673+
16471674
Types available in the ``typing.io`` submodule:
16481675

16491676
* IO (generic over ``AnyStr``)

‎python2/typing.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'no_type_check_decorator',
6767
'overload',
6868
'Text',
69+
'TYPE_CHECKING',
6970
]
7071

7172
# The pseudo-submodules 're' and 'io' are part of the public
@@ -1618,6 +1619,10 @@ def new_type(x):
16181619
Text = unicode
16191620

16201621

1622+
# Constant that's True when type checking, but False here.
1623+
TYPE_CHECKING = False
1624+
1625+
16211626
class IO(Generic[AnyStr]):
16221627
"""Generic base class for TextIO and BinaryIO.
16231628

‎src/typing.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'no_type_check_decorator',
7070
'overload',
7171
'Text',
72+
'TYPE_CHECKING',
7273
]
7374

7475
# The pseudo-submodules 're' and 'io' are part of the public
@@ -1669,6 +1670,10 @@ def new_type(x):
16691670
Text = str
16701671

16711672

1673+
# Constant that's True when type checking, but False here.
1674+
TYPE_CHECKING = False
1675+
1676+
16721677
class IO(Generic[AnyStr]):
16731678
"""Generic base class for TextIO and BinaryIO.
16741679

0 commit comments

Comments
 (0)