CINXE.COM
PEP 724 – Stricter Type Guards | 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 724 – Stricter Type Guards | peps.python.org</title> <link rel="shortcut icon" href="../_static/py.png"> <link rel="canonical" href="https://peps.python.org/pep-0724/"> <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 724 – Stricter Type Guards | peps.python.org'> <meta property="og:description" content="PEP 647 introduced the concept of a user-defined type guard function which returns True if the type of the expression passed to its first parameter matches its return TypeGuard type. For example, a function that has a return type of TypeGuard[str] is as..."> <meta property="og:type" content="website"> <meta property="og:url" content="https://peps.python.org/pep-0724/"> <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="PEP 647 introduced the concept of a user-defined type guard function which returns True if the type of the expression passed to its first parameter matches its return TypeGuard type. For example, a function that has a return type of TypeGuard[str] is as..."> <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 724</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 724 – Stricter Type Guards</h1> <dl class="rfc2822 field-list simple"> <dt class="field-odd">Author<span class="colon">:</span></dt> <dd class="field-odd">Rich Chiodo <rchiodo at microsoft.com>, Eric Traut <erictr at microsoft.com>, Erik De Bonte <erikd at microsoft.com></dd> <dt class="field-even">Sponsor<span class="colon">:</span></dt> <dd class="field-even">Jelle Zijlstra <jelle.zijlstra at gmail.com></dd> <dt class="field-odd">Discussions-To<span class="colon">:</span></dt> <dd class="field-odd"><a class="reference external" href="https://discuss.python.org/t/pep-724-stricter-type-guards/34124">Discourse thread</a></dd> <dt class="field-even">Status<span class="colon">:</span></dt> <dd class="field-even"><abbr title="Removed from consideration by sponsor or authors">Withdrawn</abbr></dd> <dt class="field-odd">Type<span class="colon">:</span></dt> <dd class="field-odd"><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-even">Topic<span class="colon">:</span></dt> <dd class="field-even"><a class="reference external" href="../topic/typing/">Typing</a></dd> <dt class="field-odd">Created<span class="colon">:</span></dt> <dd class="field-odd">28-Jul-2023</dd> <dt class="field-even">Python-Version<span class="colon">:</span></dt> <dd class="field-even">3.13</dd> <dt class="field-odd">Post-History<span class="colon">:</span></dt> <dd class="field-odd"><a class="reference external" href="https://mail.python.org/archives/list/typing-sig@python.org/thread/EMUD2D424OI53DCWQ4H5L6SJD2IXBHUL/" title="Typing-SIG thread">30-Dec-2021</a>, <a class="reference external" href="https://discuss.python.org/t/pep-724-stricter-type-guards/34124" title="Discourse thread">19-Sep-2023</a></dd> </dl> <hr class="docutils" /> <section id="contents"> <details><summary>Table of Contents</summary><ul class="simple"> <li><a class="reference internal" href="#status">Status</a></li> <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="#rationale">Rationale</a></li> <li><a class="reference internal" href="#specification">Specification</a></li> <li><a class="reference internal" href="#additional-examples">Additional Examples</a></li> <li><a class="reference internal" href="#backwards-compatibility">Backwards Compatibility</a><ul> <li><a class="reference internal" href="#breaking-change">Breaking change</a></li> </ul> </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="#stricttypeguard">StrictTypeGuard</a></li> <li><a class="reference internal" href="#typeguard-with-a-second-output-type">TypeGuard with a second output type</a></li> </ul> </li> <li><a class="reference internal" href="#footnotes">Footnotes</a></li> <li><a class="reference internal" href="#copyright">Copyright</a></li> </ul> </details></section> <section id="status"> <h2><a class="toc-backref" href="#status" role="doc-backlink">Status</a></h2> <p>This PEP is withdrawn. The Typing Council was unable to reach consensus on this proposal, and the authors decided to withdraw it.</p> </section> <section id="abstract"> <h2><a class="toc-backref" href="#abstract" role="doc-backlink">Abstract</a></h2> <p><a class="pep reference internal" href="../pep-0647/" title="PEP 647 – User-Defined Type Guards">PEP 647</a> introduced the concept of a user-defined type guard function which returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the type of the expression passed to its first parameter matches its return <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> type. For example, a function that has a return type of <code class="docutils literal notranslate"><span class="pre">TypeGuard[str]</span></code> is assumed to return <code class="docutils literal notranslate"><span class="pre">True</span></code> if and only if the type of the expression passed to its first input parameter is a <code class="docutils literal notranslate"><span class="pre">str</span></code>. This allows type checkers to narrow types when a user-defined type guard function returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p> <p>This PEP refines the <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> mechanism introduced in <a class="pep reference internal" href="../pep-0647/" title="PEP 647 – User-Defined Type Guards">PEP 647</a>. It allows type checkers to narrow types when a user-defined type guard function returns <code class="docutils literal notranslate"><span class="pre">False</span></code>. It also allows type checkers to apply additional (more precise) type narrowing under certain circumstances when the type guard function returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p> </section> <section id="motivation"> <h2><a class="toc-backref" href="#motivation" role="doc-backlink">Motivation</a></h2> <p>User-defined type guard functions enable a type checker to narrow the type of an expression when it is passed as an argument to the type guard function. The <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> mechanism introduced in <a class="pep reference internal" href="../pep-0647/" title="PEP 647 – User-Defined Type Guards">PEP 647</a> is flexible, but this flexibility imposes some limitations that developers have found inconvenient for some uses.</p> <p>Limitation 1: Type checkers are not allowed to narrow a type in the case where the type guard function returns <code class="docutils literal notranslate"><span class="pre">False</span></code>. This means the type is not narrowed in the negative (“else”) clause.</p> <p>Limitation 2: Type checkers must use the <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> return type if the type guard function returns <code class="docutils literal notranslate"><span class="pre">True</span></code> regardless of whether additional narrowing can be applied based on knowledge of the pre-narrowed type.</p> <p>The following code sample demonstrates both of these limitations.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">is_iterable</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">object</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="n">Iterable</span><span class="p">[</span><span class="n">Any</span><span class="p">]]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">)</span> <span class="k">def</span><span class="w"> </span><span class="nf">func</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">list</span><span class="p">[</span><span class="nb">int</span><span class="p">]):</span> <span class="k">if</span> <span class="n">is_iterable</span><span class="p">(</span><span class="n">val</span><span class="p">):</span> <span class="c1"># The type is narrowed to 'Iterable[Any]' as dictated by</span> <span class="c1"># the TypeGuard return type</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># Iterable[Any]</span> <span class="k">else</span><span class="p">:</span> <span class="c1"># The type is not narrowed in the "False" case</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># int | list[int]</span> <span class="c1"># If "isinstance" is used in place of the user-defined type guard</span> <span class="c1"># function, the results differ because type checkers apply additional</span> <span class="c1"># logic for "isinstance"</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">):</span> <span class="c1"># Type is narrowed to "list[int]" because this is</span> <span class="c1"># a narrower (more precise) type than "Iterable[Any]"</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># list[int]</span> <span class="k">else</span><span class="p">:</span> <span class="c1"># Type is narrowed to "int" because the logic eliminates</span> <span class="c1"># "list[int]" from the original union</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># int</span> </pre></div> </div> <p><a class="pep reference internal" href="../pep-0647/" title="PEP 647 – User-Defined Type Guards">PEP 647</a> imposed these limitations so it could support use cases where the return <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> type was not a subtype of the input type. Refer to <a class="pep reference internal" href="../pep-0647/" title="PEP 647 – User-Defined Type Guards">PEP 647</a> for examples.</p> </section> <section id="rationale"> <h2><a class="toc-backref" href="#rationale" role="doc-backlink">Rationale</a></h2> <p>There are a number of issues where a stricter <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> would have been a solution:</p> <ul class="simple"> <li><a class="reference external" href="https://github.com/python/typing/issues/1351">Python typing issue - TypeGuard doesn’t intersect like isinstance</a></li> <li><a class="reference external" href="https://github.com/microsoft/pyright/issues/3450">Pyright issue - TypeGuard not eliminating possibility on branch</a></li> <li><a class="reference external" href="https://github.com/microsoft/pyright/issues/3466">Pyright issue - Type narrowing for Literal doesn’t work</a></li> <li><a class="reference external" href="https://github.com/python/mypy/issues/15305">Mypy issue - TypeGuard is incompatible with exhaustive check</a></li> <li><a class="reference external" href="https://github.com/python/mypy/issues/15520">Mypy issue - Incorrect type narrowing for inspect.isawaitable</a></li> <li><a class="reference external" href="https://github.com/python/typeshed/issues/8009">Typeshed issue - asyncio.iscoroutinefunction is not a TypeGuard</a></li> </ul> </section> <section id="specification"> <h2><a class="toc-backref" href="#specification" role="doc-backlink">Specification</a></h2> <p>The use of a user-defined type guard function involves five types:</p> <ul class="simple"> <li>I = <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> input type</li> <li>R = <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> return type</li> <li>A = Type of argument passed to type guard function (pre-narrowed)</li> <li>NP = Narrowed type (positive)</li> <li>NN = Narrowed type (negative)</li> </ul> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">guard</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="n">I</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="n">R</span><span class="p">]:</span> <span class="o">...</span> <span class="k">def</span><span class="w"> </span><span class="nf">func1</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="n">A</span><span class="p">):</span> <span class="k">if</span> <span class="n">guard</span><span class="p">(</span><span class="n">val</span><span class="p">):</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># NP</span> <span class="k">else</span><span class="p">:</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># NN</span> </pre></div> </div> <p>This PEP proposes some modifications to <a class="pep reference internal" href="../pep-0647/" title="PEP 647 – User-Defined Type Guards">PEP 647</a> to address the limitations discussed above. These limitations are safe to eliminate only when a specific condition is met. In particular, when the output type <code class="docutils literal notranslate"><span class="pre">R</span></code> of a user-defined type guard function is consistent <a class="footnote-reference brackets" href="#isconsistent" id="id1">[1]</a> with the type of its first input parameter (<code class="docutils literal notranslate"><span class="pre">I</span></code>), type checkers should apply stricter type guard semantics.</p> <blockquote> <div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Stricter type guard semantics are used in this case because</span> <span class="c1"># "Kangaroo | Koala" is consistent with "Animal"</span> <span class="k">def</span><span class="w"> </span><span class="nf">is_marsupial</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="n">Animal</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="n">Kangaroo</span> <span class="o">|</span> <span class="n">Koala</span><span class="p">]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">Kangaroo</span> <span class="o">|</span> <span class="n">Koala</span><span class="p">)</span> <span class="c1"># Stricter type guard semantics are not used in this case because</span> <span class="c1"># "list[T]"" is not consistent with "list[T | None]"</span> <span class="k">def</span><span class="w"> </span><span class="nf">has_no_nones</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">T</span> <span class="o">|</span> <span class="kc">None</span><span class="p">])</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="nb">list</span><span class="p">[</span><span class="n">T</span><span class="p">]]:</span> <span class="k">return</span> <span class="kc">None</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">val</span> </pre></div> </div> </div></blockquote> <p>When stricter type guard semantics are applied, the application of a user-defined type guard function changes in two ways.</p> <ul class="simple"> <li>Type narrowing is applied in the negative (“else”) case.</li> </ul> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">is_str</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">str</span> <span class="o">|</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">def</span><span class="w"> </span><span class="nf">func</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">str</span> <span class="o">|</span> <span class="nb">int</span><span class="p">):</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">is_str</span><span class="p">(</span><span class="n">val</span><span class="p">):</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># int</span> </pre></div> </div> <ul class="simple"> <li>Additional type narrowing is applied in the positive “if” case if applicable.</li> </ul> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">is_cardinal_direction</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="n">Literal</span><span class="p">[</span><span class="s2">"N"</span><span class="p">,</span> <span class="s2">"S"</span><span class="p">,</span> <span class="s2">"E"</span><span class="p">,</span> <span class="s2">"W"</span><span class="p">]]:</span> <span class="k">return</span> <span class="n">val</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">"N"</span><span class="p">,</span> <span class="s2">"S"</span><span class="p">,</span> <span class="s2">"E"</span><span class="p">,</span> <span class="s2">"W"</span><span class="p">)</span> <span class="k">def</span><span class="w"> </span><span class="nf">func</span><span class="p">(</span><span class="n">direction</span><span class="p">:</span> <span class="n">Literal</span><span class="p">[</span><span class="s2">"NW"</span><span class="p">,</span> <span class="s2">"E"</span><span class="p">]):</span> <span class="k">if</span> <span class="n">is_cardinal_direction</span><span class="p">(</span><span class="n">direction</span><span class="p">):</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">direction</span><span class="p">)</span> <span class="c1"># "Literal[E]"</span> <span class="k">else</span><span class="p">:</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">direction</span><span class="p">)</span> <span class="c1"># "Literal[NW]"</span> </pre></div> </div> <p>The type-theoretic rules for type narrowing are specified in the following table.</p> <table class="docutils align-default"> <thead> <tr class="row-odd"><th class="head"></th> <th class="head">Non-strict type guard</th> <th class="head">Strict type guard</th> </tr> </thead> <tbody> <tr class="row-even"><td>Applies when</td> <td>R not consistent with I</td> <td>R consistent with I</td> </tr> <tr class="row-odd"><td>NP is ..</td> <td><span class="formula"><i>R</i></span></td> <td><span class="formula"><i>A</i>∧<i>R</i></span></td> </tr> <tr class="row-even"><td>NN is ..</td> <td><span class="formula"><i>A</i></span></td> <td><span class="formula"><i>A</i>∧¬<i>R</i></span></td> </tr> </tbody> </table> <p>In practice, the theoretic types for strict type guards cannot be expressed precisely in the Python type system. Type checkers should fall back on practical approximations of these types. As a rule of thumb, a type checker should use the same type narrowing logic – and get results that are consistent with – its handling of “isinstance”. This guidance allows for changes and improvements if the type system is extended in the future.</p> </section> <section id="additional-examples"> <h2><a class="toc-backref" href="#additional-examples" role="doc-backlink">Additional Examples</a></h2> <p><code class="docutils literal notranslate"><span class="pre">Any</span></code> is consistent <a class="footnote-reference brackets" href="#isconsistent" id="id2">[1]</a> with any other type, which means stricter semantics can be applied.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span> <span class="c1"># Stricter type guard semantics are used in this case because</span> <span class="c1"># "str" is consistent with "Any"</span> <span class="k">def</span><span class="w"> </span><span class="nf">is_str</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="n">Any</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">def</span><span class="w"> </span><span class="nf">test</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">float</span> <span class="o">|</span> <span class="nb">str</span><span class="p">):</span> <span class="k">if</span> <span class="n">is_str</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1"># str</span> <span class="k">else</span><span class="p">:</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1"># float</span> </pre></div> </div> </section> <section id="backwards-compatibility"> <h2><a class="toc-backref" href="#backwards-compatibility" role="doc-backlink">Backwards Compatibility</a></h2> <p>This PEP proposes to change the existing behavior of <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code>. This has no effect at runtime, but it does change the types evaluated by a type checker.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">is_int</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="k">def</span><span class="w"> </span><span class="nf">func</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">):</span> <span class="k">if</span> <span class="n">is_int</span><span class="p">(</span><span class="n">val</span><span class="p">):</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># "int"</span> <span class="k">else</span><span class="p">:</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># Previously "int | str", now "str"</span> </pre></div> </div> <p>This behavioral change results in different types evaluated by a type checker. It could therefore produce new (or mask existing) type errors.</p> <p>Type checkers often improve narrowing logic or fix existing bugs in such logic, so users of static typing will be used to this type of behavioral change.</p> <p>We also hypothesize that it is unlikely that existing typed Python code relies on the current behavior of <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code>. To validate our hypothesis, we implemented the proposed change in pyright and ran this modified version on roughly 25 typed code bases using <a class="reference external" href="https://github.com/hauntsaninja/mypy_primer">mypy primer</a> to see if there were any differences in the output. As predicted, the behavioral change had minimal impact. The only noteworthy change was that some <code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">type:</span> <span class="pre">ignore</span></code> comments were no longer necessary, indicating that these code bases were already working around the existing limitations of <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code>.</p> <section id="breaking-change"> <h3><a class="toc-backref" href="#breaking-change" role="doc-backlink">Breaking change</a></h3> <p>It is possible for a user-defined type guard function to rely on the old behavior. Such type guard functions could break with the new behavior.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">is_positive_int</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="nb">int</span><span class="p">]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">and</span> <span class="n">val</span> <span class="o">></span> <span class="mi">0</span> <span class="k">def</span><span class="w"> </span><span class="nf">func</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">):</span> <span class="k">if</span> <span class="n">is_positive_int</span><span class="p">(</span><span class="n">val</span><span class="p">):</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="c1"># "int"</span> <span class="k">else</span><span class="p">:</span> <span class="c1"># With the older behavior, the type of "val" is evaluated as</span> <span class="c1"># "int | str"; with the new behavior, the type is narrowed to</span> <span class="c1"># "str", which is perhaps not what was intended.</span> <span class="n">reveal_type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> </pre></div> </div> <p>We think it is unlikely that such user-defined type guards exist in real-world code. The mypy primer results didn’t uncover any such cases.</p> </section> </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> <p>Users unfamiliar with <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> are likely to expect the behavior outlined in this PEP, therefore making <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> easier to teach and explain.</p> </section> <section id="reference-implementation"> <h2><a class="toc-backref" href="#reference-implementation" role="doc-backlink">Reference Implementation</a></h2> <p>A reference <a class="reference external" href="https://github.com/microsoft/pyright/commit/9a5af798d726bd0612cebee7223676c39cf0b9b0">implementation</a> of this idea exists in pyright.</p> <p>To enable the modified behavior, the configuration flag <code class="docutils literal notranslate"><span class="pre">enableExperimentalFeatures</span></code> must be set to true. This can be done on a per-file basis by adding a comment:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># pyright: enableExperimentalFeatures=true</span> </pre></div> </div> </section> <section id="rejected-ideas"> <h2><a class="toc-backref" href="#rejected-ideas" role="doc-backlink">Rejected Ideas</a></h2> <section id="stricttypeguard"> <h3><a class="toc-backref" href="#stricttypeguard" role="doc-backlink">StrictTypeGuard</a></h3> <p>A new <code class="docutils literal notranslate"><span class="pre">StrictTypeGuard</span></code> construct was proposed. This alternative form would be similar to a <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> except it would apply stricter type guard semantics. It would also enforce that the return type was consistent <a class="footnote-reference brackets" href="#isconsistent" id="id5">[1]</a> with the input type. See this thread for details: <a class="reference external" href="https://github.com/python/typing/discussions/1013#discussioncomment-1966238">StrictTypeGuard proposal</a></p> <p>This idea was rejected because it is unnecessary in most cases and added unnecessary complexity. It would require the introduction of a new special form, and developers would need to be educated about the subtle difference between the two forms.</p> </section> <section id="typeguard-with-a-second-output-type"> <h3><a class="toc-backref" href="#typeguard-with-a-second-output-type" role="doc-backlink">TypeGuard with a second output type</a></h3> <p>Another idea was proposed where <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code> could support a second optional type argument that indicates the type that should be used for narrowing in the negative (“else”) case.</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">is_int</span><span class="p">(</span><span class="n">val</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="n">TypeGuard</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">str</span><span class="p">]:</span> <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> </pre></div> </div> <p>This idea was proposed <a class="reference external" href="https://github.com/python/typing/issues/996">here</a>.</p> <p>It was rejected because it was considered too complicated and addressed only one of the two main limitations of <code class="docutils literal notranslate"><span class="pre">TypeGuard</span></code>. Refer to this <a class="reference external" href="https://mail.python.org/archives/list/typing-sig@python.org/thread/EMUD2D424OI53DCWQ4H5L6SJD2IXBHUL">thread</a> for the full discussion.</p> </section> </section> <section id="footnotes"> <h2><a class="toc-backref" href="#footnotes" role="doc-backlink">Footnotes</a></h2> <aside class="footnote-list brackets"> <aside class="footnote brackets" id="isconsistent" role="doc-footnote"> <dt class="label" id="isconsistent">[1]<em> (<a href='#id1'>1</a>, <a href='#id2'>2</a>, <a href='#id5'>3</a>) </em></dt> <dd><a class="pep reference internal" href="../pep-0483/#summary-of-gradual-typing" title="PEP 483 – The Theory of Type Hints § Summary of gradual typing">PEP 483’s discussion of is-consistent</a></aside> </aside> </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-0724.rst">https://github.com/python/peps/blob/main/peps/pep-0724.rst</a></p> <p>Last modified: <a class="reference external" href="https://github.com/python/peps/commits/main/peps/pep-0724.rst">2025-02-01 08:55:40 GMT</a></p> </article> <nav id="pep-sidebar"> <h2>Contents</h2> <ul> <li><a class="reference internal" href="#status">Status</a></li> <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="#rationale">Rationale</a></li> <li><a class="reference internal" href="#specification">Specification</a></li> <li><a class="reference internal" href="#additional-examples">Additional Examples</a></li> <li><a class="reference internal" href="#backwards-compatibility">Backwards Compatibility</a><ul> <li><a class="reference internal" href="#breaking-change">Breaking change</a></li> </ul> </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="#stricttypeguard">StrictTypeGuard</a></li> <li><a class="reference internal" href="#typeguard-with-a-second-output-type">TypeGuard with a second output type</a></li> </ul> </li> <li><a class="reference internal" href="#footnotes">Footnotes</a></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-0724.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>