<div dir="ltr"><div class="markdown-here-wrapper" style="font-size:1em;font-family:Helvetica,arial,freesans,clean,sans-serif;color:rgb(34,34,34);background-color:rgb(255,255,255);border:none;line-height:1.2"><p style="margin:1em 0px"><a href="https://github.com/numpy/numpy/pull/12593" style="color:rgb(51,51,238);text-decoration:none">PR #12593</a> adds a handful of new exception types for . Some consequences of this change are that:</p>
<ol style="padding-left:2em;margin:1em 0px">
<li style="margin:1em 0px">The string formatting is moved to python, allowing us to give better error messages without a lot of work</li>
<li style="margin:1em 0px">The formatting is dispatched lazily, meaning that users trying ufuncs, but catching unsupported loop types don’t have to pay the cost of string formatting</li>
<li style="margin:1em 0px">Users can catch a specific exception type. This might expose more of our internals than we’re willing to though.</li>
<li style="margin:1em 0px">We need to expose these new exception names in the public API.</li>
</ol>
<p style="margin:1em 0px">3 & 4 raise some questions, which I’d like some feedback on:</p>
<ul style="padding-left:2em;margin:1em 0px">
<li style="margin:1em 0px"><p style="margin:1em 0px">Should we actually expose the detailed exception types to the user, or should they be kept an implementation detail? One way to hide the implementation details would be</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;overflow:auto;margin:1em 0px"><code class="hljs language-python" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,255)"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TypeError</span><span class="hljs-params">(builtins.TypeError)</span>:</span>
    <span class="hljs-string">"""actually UFuncCastingError"""</span>
_UFuncCastingError = TypeError  <span class="hljs-comment"># for internal use when raising</span>
_UFuncCastingError.__module__ = <span class="hljs-keyword">None</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TypeError</span><span class="hljs-params">(builtins.TypeError)</span>:</span>
    <span class="hljs-string">"""actually UFuncLoopError"""</span>
_UFuncLoopError= TypeError  <span class="hljs-comment"># for internal use when raising</span>
_UFuncLoopError  .__module__ = <span class="hljs-keyword">None</span>

<span class="hljs-keyword">del</span> TypeError
</code></pre>
<p style="margin:1em 0px">This gives us all the advantages of 1 & 2 without the user noticing that they’re receiving anything other than TypeError, which their tracebacks will continue to show.</p>
</li>
<li style="margin:1em 0px"><p style="margin:1em 0px">If we do expose them, where should these exceptions go? In the past, we also added <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">AxisError</code> and <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">TooHardError</code> - it would be nice to start being consistent about where to expose these things</p>
<ol style="padding-left:2em;margin:1em 0px">
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.UFuncCastingError</code> (expands the global namespace even further)</li>
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.core._exceptions.UFuncCastingError</code> (shouldn’t really be private, since users won’t know how to catch it)</li>
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.core.UFuncCastingError</code></li>
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.core.umath.CastingError</code></li>
<li style="margin:1em 0px">A dedicated namespace for exceptions:<ul style="padding-left:2em;margin:1em 0px">
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.errors.UFuncCastingError</code> (matches <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">pandas</code>)</li>
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.exceptions.UFuncCastingError</code></li>
<li style="margin:1em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">np.exc.UFuncCastingError</code> (matches <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:nowrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">sqlalchemy</code>)</li>
</ul>
</li>
</ol>
</li>
</ul>
<p style="margin:1em 0px">Eric</p>
<div title="MDH:PGRpdj5bUFIgIzEyNTkzXShodHRwczovL2dpdGh1Yi5jb20vbnVtcHkvbnVtcHkvcHVsbC8xMjU5
MykgYWRkcyBhIGhhbmRmdWwg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