Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion pep-0484.txt
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,31 @@ Don't expect a checker to understand obfuscations like
``"".join(reversed(sys.platform)) == "xunil"``.


Runtime or type checking?
-------------------------

Sometimes there's code that must be seen by a type checker (or other
static analysis tools) but should not be executed. For such
situations the ``typing`` module defines a constant,
``TYPE_CHECKING``, that is considered ``True`` during type checking
(or other static analysis) but ``False`` at runtime. Example::

import typing

if typing.TYPE_CHECKING:
import expensive_mod

def a_func(arg: 'expensive_mod.SomeClass') -> None:
a_var = arg # type: expensive_mod.SomeClass
...

(Note that the type annotation must be enclosed in quotes, making it a
"forward reference", to hide the ``expensive_mod`` reference from the
interpreter runtime. In the ``# type`` comment no quotes are needed.)

This approach may also be useful to handle import cycles.


Arbitrary argument lists and default argument values
----------------------------------------------------

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


NewType helper function
-----------------------
=======================

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

* TYPE_CHECKING, ``False`` at runtime but ``True`` to type checkers

Types available in the ``typing.io`` submodule:

* IO (generic over ``AnyStr``)
Expand Down
5 changes: 5 additions & 0 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
'no_type_check_decorator',
'overload',
'Text',
'TYPE_CHECKING',
]

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


# Constant that's True when type checking, but False here.
TYPE_CHECKING = False


class IO(Generic[AnyStr]):
"""Generic base class for TextIO and BinaryIO.

Expand Down
5 changes: 5 additions & 0 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
'no_type_check_decorator',
'overload',
'Text',
'TYPE_CHECKING',
]

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


# Constant that's True when type checking, but False here.
TYPE_CHECKING = False


class IO(Generic[AnyStr]):
"""Generic base class for TextIO and BinaryIO.

Expand Down