You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: types.html
+28-2Lines changed: 28 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -457,11 +457,37 @@ <h1>Types<a class="headerlink" href="#types" title="Link to this heading">#</a><
457
457
<spanclass="k">return</span><spanclass="s2">"dict of int to str"</span>
458
458
</pre></div>
459
459
</div>
460
-
<p><strong>Note:</strong>
461
-
Although parametric types such as <codeclass="docutils literal notranslate"><spanclass="pre">List[int]</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">Dict[int,</span><spanclass="pre">str]</span></code> are fully
460
+
<p>Although parametric types such as <codeclass="docutils literal notranslate"><spanclass="pre">List[int]</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">Dict[int,</span><spanclass="pre">str]</span></code> are fully
462
461
supported, they do incur a performance penalty.
463
462
For optimal performance, is recommended to use parametric types only where necessary.
464
463
<codeclass="docutils literal notranslate"><spanclass="pre">Union</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">Optional</span></code> do not incur a performance penalty.</p>
464
+
<divclass="admonition important">
465
+
<pclass="admonition-title">Important</p>
466
+
<p>Plum’s type system is powered by <aclass="reference external" href="https://github.com/beartype/beartype">Beartype</a>.
467
+
To ensure constant-time performance,
468
+
Beartype checks the types of containers by checking the type of a random single element.
469
+
This means that it is not safe to use containers with mixed element types!</p>
<spanclass="k">return</span><spanclass="s2">"list of int"</span>
478
+
</pre></div>
479
+
</div>
480
+
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="gp">>>> </span><spanclass="n">f</span><spanclass="p">([</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="s2">"1"</span><spanclass="p">])</span><spanclass="c1"># It might happen to check the first element.</span>
481
+
<spanclass="go">"list of int"</span>
482
+
483
+
<spanclass="gp">>>> </span><spanclass="n">f</span><spanclass="p">([</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="s2">"1"</span><spanclass="p">])</span><spanclass="c1"># Or it might check the second. :(</span>
484
+
<spanclass="go">NotFoundLookupError: `f([1, '1'])` could not be resolved.</span>
485
+
</pre></div>
486
+
</div>
487
+
<p>In the future, Beartype
488
+
<aclass="reference external" href="https://beartype.readthedocs.io/en/latest/api_decor/#beartype.BeartypeStrategy.On">will support exhaustive type checking</a>.
489
+
Plum already opts into this behaviour and will use it once it becomes available.</p>
490
+
</div>
465
491
<p>The type system is <em>covariant</em>, as opposed to Julia’s type
466
492
system, which is <em>invariant</em>.
467
493
For example, this means that <codeclass="docutils literal notranslate"><spanclass="pre">List[T1]</span></code> is a subtype of <codeclass="docutils literal notranslate"><spanclass="pre">List[T2]</span></code> whenever
0 commit comments