Skip to content

Docs: Replace "if False" trick with "if TYPE_CHECKING". #2181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2016
Merged
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
13 changes: 5 additions & 8 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,16 @@ imports to a module and those imports cause cycles that didn't exist
before. If those cycles become a problem when running your program,
there's a trick: if the import is only needed for type annotations in
forward references (string literals) or comments, you can write the
imports inside ``if False:`` so that they are not executed at runtime.
The reason this works is that mypy (currently) does not analyze
unreachable code like this. Example:
imports inside ``if TYPE_CHECKING:`` so that they are not executed at runtime.
Example:

File ``foo.py``:

.. code-block:: python

from typing import List
from typing import List, TYPE_CHECKING

if False:
if TYPE_CHECKING:
import bar

def listify(arg: 'bar.BarClass') -> 'List[bar.BarClass]':
Expand All @@ -289,7 +288,5 @@ File ``bar.py``:

.. note::

It is possible that in the future, mypy will change its dead code
analysis and this trick will stop working. We will then offer an
alternative, e.g. a constant defined by the ``typing`` module that
The ``TYPE_CHECKING`` constant defined by the ``typing`` module
is ``False`` at runtime but ``True`` while type checking.