CINXE.COM
PEP 781 – Make TYPE_CHECKING a built-in constant | peps.python.org
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="color-scheme" content="light dark"> <title>PEP 781 – Make TYPE_CHECKING a built-in constant | peps.python.org</title> <link rel="shortcut icon" href="../_static/py.png"> <link rel="canonical" href="https://peps.python.org/pep-0781/"> <link rel="stylesheet" href="../_static/style.css" type="text/css"> <link rel="stylesheet" href="../_static/mq.css" type="text/css"> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" media="(prefers-color-scheme: light)" id="pyg-light"> <link rel="stylesheet" href="../_static/pygments_dark.css" type="text/css" media="(prefers-color-scheme: dark)" id="pyg-dark"> <link rel="alternate" type="application/rss+xml" title="Latest PEPs" href="https://peps.python.org/peps.rss"> <meta property="og:title" content='PEP 781 – Make TYPE_CHECKING a built-in constant | peps.python.org'> <meta property="og:description" content="This PEP proposes adding a new built-in variable, TYPE_CHECKING, to improve the experience of writing Python code with type annotations. It is evaluated as True when the code is being analyzed by a static type checker, and as False during normal runtime..."> <meta property="og:type" content="website"> <meta property="og:url" content="https://peps.python.org/pep-0781/"> <meta property="og:site_name" content="Python Enhancement Proposals (PEPs)"> <meta property="og:image" content="https://peps.python.org/_static/og-image.png"> <meta property="og:image:alt" content="Python PEPs"> <meta property="og:image:width" content="200"> <meta property="og:image:height" content="200"> <meta name="description" content="This PEP proposes adding a new built-in variable, TYPE_CHECKING, to improve the experience of writing Python code with type annotations. It is evaluated as True when the code is being analyzed by a static type checker, and as False during normal runtime..."> <meta name="theme-color" content="#3776ab"> </head> <body> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-sun-half" viewBox="0 0 24 24" pointer-events="all"> <title>Following system colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="9"></circle> <path d="M12 3v18m0-12l4.65-4.65M12 14.3l7.37-7.37M12 19.6l8.85-8.85"></path> </svg> </symbol> <symbol id="svg-moon" viewBox="0 0 24 24" pointer-events="all"> <title>Selected dark colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"></path> <path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z"></path> </svg> </symbol> <symbol id="svg-sun" viewBox="0 0 24 24" pointer-events="all"> <title>Selected light colour scheme</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="5"></circle> <line x1="12" y1="1" x2="12" y2="3"></line> <line x1="12" y1="21" x2="12" y2="23"></line> <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line> <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line> <line x1="1" y1="12" x2="3" y2="12"></line> <line x1="21" y1="12" x2="23" y2="12"></line> <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line> <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line> </svg> </symbol> </svg> <script> document.documentElement.dataset.colour_scheme = localStorage.getItem("colour_scheme") || "auto" </script> <section id="pep-page-section"> <header> <h1>Python Enhancement Proposals</h1> <ul class="breadcrumbs"> <li><a href="https://www.python.org/" title="The Python Programming Language">Python</a> » </li> <li><a href="../pep-0000/">PEP Index</a> » </li> <li>PEP 781</li> </ul> <button id="colour-scheme-cycler" onClick="setColourScheme(nextColourScheme())"> <svg aria-hidden="true" class="colour-scheme-icon-when-auto"><use href="#svg-sun-half"></use></svg> <svg aria-hidden="true" class="colour-scheme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg aria-hidden="true" class="colour-scheme-icon-when-light"><use href="#svg-sun"></use></svg> <span class="visually-hidden">Toggle light / dark / auto colour theme</span> </button> </header> <article> <section id="pep-content"> <h1 class="page-title">PEP 781 – Make <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> a built-in constant</h1> <dl class="rfc2822 field-list simple"> <dt class="field-odd">Author<span class="colon">:</span></dt> <dd class="field-odd">Inada Naoki <songofacandy at gmail.com></dd> <dt class="field-even">Discussions-To<span class="colon">:</span></dt> <dd class="field-even"><a class="reference external" href="https://discuss.python.org/t/85728">Discourse thread</a></dd> <dt class="field-odd">Status<span class="colon">:</span></dt> <dd class="field-odd"><abbr title="Proposal under active discussion and revision">Draft</abbr></dd> <dt class="field-even">Type<span class="colon">:</span></dt> <dd class="field-even"><abbr title="Normative PEP with a new feature for Python, implementation change for CPython or interoperability standard for the ecosystem">Standards Track</abbr></dd> <dt class="field-odd">Topic<span class="colon">:</span></dt> <dd class="field-odd"><a class="reference external" href="../topic/typing/">Typing</a></dd> <dt class="field-even">Created<span class="colon">:</span></dt> <dd class="field-even">24-Mar-2025</dd> <dt class="field-odd">Python-Version<span class="colon">:</span></dt> <dd class="field-odd">3.14</dd> <dt class="field-even">Post-History<span class="colon">:</span></dt> <dd class="field-even"><a class="reference external" href="https://discuss.python.org/t/76766" title="Discourse thread">11-Jan-2025</a>, <a class="reference external" href="https://discuss.python.org/t/85728" title="Discourse thread">24-Mar-2025</a></dd> </dl> <hr class="docutils" /> <section id="contents"> <details><summary>Table of Contents</summary><ul class="simple"> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#motivation">Motivation</a></li> <li><a class="reference internal" href="#specification">Specification</a></li> <li><a class="reference internal" href="#backwards-compatibility">Backwards Compatibility</a></li> <li><a class="reference internal" href="#how-to-teach-this">How to Teach This</a></li> <li><a class="reference internal" href="#reference-implementation">Reference Implementation</a></li> <li><a class="reference internal" href="#rejected-ideas">Rejected Ideas</a><ul> <li><a class="reference internal" href="#eliminate-type-checking-only-code">Eliminate type-checking-only code</a></li> </ul> </li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> </details></section> <section id="abstract"> <h2><a class="toc-backref" href="#abstract" role="doc-backlink">Abstract</a></h2> <p>This PEP proposes adding a new built-in variable, <code class="xref py py-data docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code>, to improve the experience of writing Python code with type annotations. It is evaluated as <code class="docutils literal notranslate"><span class="pre">True</span></code> when the code is being analyzed by a static type checker, and as <code class="docutils literal notranslate"><span class="pre">False</span></code> during normal runtime execution. Unlike <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING" title="(in Python v3.13)"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.TYPE_CHECKING</span></code></a>, which this variable replaces, it does not require an import statement.</p> </section> <section id="motivation"> <h2><a class="toc-backref" href="#motivation" role="doc-backlink">Motivation</a></h2> <p>Type annotations were defined for Python by <a class="pep reference internal" href="../pep-0484/" title="PEP 484 – Type Hints">PEP 484</a>, and have enjoyed widespread adoption. A challenge with fully-annotated code is that many more imports are required in order to bring the relevant name into scope, potentially causing import cycles without careful design. This has been recognized by <a class="pep reference internal" href="../pep-0563/" title="PEP 563 – Postponed Evaluation of Annotations">PEP 563</a> and later <a class="pep reference internal" href="../pep-0649/" title="PEP 649 – Deferred Evaluation Of Annotations Using Descriptors">PEP 649</a>, which introduce two different mechanisms for deferred evaluation of type annotations. As PEP 563 notes, “type hints are … not computationally free”. The <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING" title="(in Python v3.13)"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.TYPE_CHECKING</span></code></a> constant was thus <a class="reference external" href="https://github.com/python/typing/issues/230">introduced</a>, initially to aid in breaking cyclic imports.</p> <p>In situations where startup time is critical, such as command-line interfaces, applications, or core libraries, programmers may place all import statements not required for runtime execution within a ‘TYPE_CHECKING block’, or even defer certain imports to within functions. The <code class="docutils literal notranslate"><span class="pre">typing</span></code> module itself though can take as much as 10ms to import, longer than Python takes to initialize. The time taken to import the <code class="docutils literal notranslate"><span class="pre">typing</span></code> module clearly cannot be ignored.</p> <p>To avoid importing <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> from <code class="docutils literal notranslate"><span class="pre">typing</span></code>, developers currently define a module-level variable such as <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span> <span class="pre">=</span> <span class="pre">False</span></code> or use code like <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">False:</span>  <span class="pre">#</span> <span class="pre">TYPE_CHECKING</span></code>. Providing a standard method will allow many tools to implement the same behavior consistently. It will also allow third-party tools in the ecosystem to standardize on a single behavior with guaranteed semantics, as for example some static type checkers currently do not permit local constants, only recognizing <code class="docutils literal notranslate"><span class="pre">typing.TYPE_CHECKING</span></code>.</p> </section> <section id="specification"> <h2><a class="toc-backref" href="#specification" role="doc-backlink">Specification</a></h2> <p><code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> is a built-in constant and its value is <code class="docutils literal notranslate"><span class="pre">False</span></code>. Unlike <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, <code class="docutils literal notranslate"><span class="pre">None</span></code>, and <code class="docutils literal notranslate"><span class="pre">__debug__</span></code>, <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> is not a real constant; assigning to it will not raise a <code class="docutils literal notranslate"><span class="pre">SyntaxError</span></code>.</p> <p>Static type checkers must treat <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> as <code class="docutils literal notranslate"><span class="pre">True</span></code>, similar to <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING" title="(in Python v3.13)"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.TYPE_CHECKING</span></code></a>.</p> <p>If this PEP is accepted, the new <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> constant will be the preferred approach, instead of the existing <code class="docutils literal notranslate"><span class="pre">typing.TYPE_CHECKING</span></code>. However, <code class="docutils literal notranslate"><span class="pre">typing.TYPE_CHECKING</span></code> will not be deprecated in the foreseeable future.</p> </section> <section id="backwards-compatibility"> <h2><a class="toc-backref" href="#backwards-compatibility" role="doc-backlink">Backwards Compatibility</a></h2> <p>Since <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> doesn’t prohibit assignment, existing code using <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> will continue to work.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># This code will continue to work</span> <span class="n">TYPE_CHECKING</span> <span class="o">=</span> <span class="kc">False</span> <span class="kn">from</span><span class="w"> </span><span class="nn">typing</span><span class="w"> </span><span class="kn">import</span> <span class="n">TYPE_CHECKING</span> </pre></div> </div> <p>User can remove the assignment to <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span></code> after they stop using Python 3.13 or older versions.</p> </section> <section id="how-to-teach-this"> <h2><a class="toc-backref" href="#how-to-teach-this" role="doc-backlink">How to Teach This</a></h2> <ul class="simple"> <li>Use <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">TYPE_CHECKING:</span></code> for skipping type-checking code at runtime.</li> <li>Use <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">typing</span> <span class="pre">import</span> <span class="pre">TYPE_CHECKING</span></code> to support Python versions before 3.14.</li> <li>Workarounds like <code class="docutils literal notranslate"><span class="pre">TYPE_CHECKING</span> <span class="pre">=</span> <span class="pre">False</span></code> or <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">False:</span>  <span class="pre">#</span> <span class="pre">TYPE_CHECKING</span></code> will continue to work, but are not recommended.</li> </ul> </section> <section id="reference-implementation"> <h2><a class="toc-backref" href="#reference-implementation" role="doc-backlink">Reference Implementation</a></h2> <ul class="simple"> <li><a class="reference external" href="https://github.com/python/cpython/pull/131793">python/cpython#131793</a></li> </ul> </section> <section id="rejected-ideas"> <h2><a class="toc-backref" href="#rejected-ideas" role="doc-backlink">Rejected Ideas</a></h2> <section id="eliminate-type-checking-only-code"> <h3><a class="toc-backref" href="#eliminate-type-checking-only-code" role="doc-backlink">Eliminate type-checking-only code</a></h3> <p>It is considered to add real constant named <code class="docutils literal notranslate"><span class="pre">__type_checking__</span></code> to eliminate type-checking-only code at compile time.</p> <p>However, adding real constant to language increase complexity of the language. Benefit from eliminating type-checking-only code is estimated to be not enough to justify the complexity.</p> </section> </section> <section id="copyright"> <h2><a class="toc-backref" href="#copyright" role="doc-backlink">Copyright</a></h2> <p>This document is placed in the public domain or under the CC0-1.0-Universal license, whichever is more permissive.</p> </section> </section> <hr class="docutils" /> <p>Source: <a class="reference external" href="https://github.com/python/peps/blob/main/peps/pep-0781.rst">https://github.com/python/peps/blob/main/peps/pep-0781.rst</a></p> <p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-0781.rst">2025-03-28 01:16:17 GMT</a></p> </article> <nav id="pep-sidebar"> <h2>Contents</h2> <ul> <li><a class="reference internal" href="#abstract">Abstract</a></li> <li><a class="reference internal" href="#motivation">Motivation</a></li> <li><a class="reference internal" href="#specification">Specification</a></li> <li><a class="reference internal" href="#backwards-compatibility">Backwards Compatibility</a></li> <li><a class="reference internal" href="#how-to-teach-this">How to Teach This</a></li> <li><a class="reference internal" href="#reference-implementation">Reference Implementation</a></li> <li><a class="reference internal" href="#rejected-ideas">Rejected Ideas</a><ul> <li><a class="reference internal" href="#eliminate-type-checking-only-code">Eliminate type-checking-only code</a></li> </ul> </li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> <br> <a id="source" href="https://github.com/python/peps/blob/main/peps/pep-0781.rst">Page Source (GitHub)</a> </nav> </section> <script src="../_static/colour_scheme.js"></script> <script src="../_static/wrap_tables.js"></script> <script src="../_static/sticky_banner.js"></script> </body> </html>