<div dir="ltr">Here's the code: <a href="https://github.com/rmcgibbo/numpy-mypy">https://github.com/rmcgibbo/numpy-mypy</a>.<div><br></div><div>It's not 100% working yet, but it can do simple stuff, like inferring the shape of arrays created from np.zeros(literal_tuple), and fixing out the shape of the result of an indexing operation (i.e. <a href="https://github.com/rmcgibbo/numpy-mypy/blob/master/tests/test_indexing.py">https://github.com/rmcgibbo/numpy-mypy/blob/master/tests/test_indexing.py</a>).</div><div><br></div><div>To implement it, I have the beginnings of the stubs that you'd expect, borrowed from <a href="https://github.com/machinalis/mypy-data">https://github.com/machinalis/mypy-data</a> and then revised. Then, on top of that, I wrote some special type-level functions that are implemented inside of a mypy plugin. So, for example,</div><div>the stub's signature for np.sum is</div><div><br></div><div><table class="gmail-highlight gmail-tab-size gmail-js-file-line-container" style="box-sizing:border-box;border-collapse:collapse;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";font-size:14px"><tbody style="box-sizing:border-box"><tr style="box-sizing:border-box"></tr><tr style="box-sizing:border-box"><td id="gmail-LC323" class="gmail-blob-code gmail-blob-code-inner gmail-js-file-line" style="box-sizing:border-box;padding:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre"><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">def</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">sum</span>(<span class="gmail-pl-smi" style="box-sizing:border-box">a</span>: ndarray[_S, _D], <span class="gmail-pl-smi" style="box-sizing:border-box">axis</span>: AxesType<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">None</span>, <span class="gmail-pl-smi" style="box-sizing:border-box">dtype</span>: DtypeType<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">None</span>, <span class="gmail-pl-smi" style="box-sizing:border-box">out</span>: ndarray<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">None</span>, <span class="gmail-pl-smi" style="box-sizing:border-box">keepdims</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">bool</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">False</span>) -> ndarray[_InferDtypeWithDefault[_S], _InferNdimsReduction[_D]]: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">...</span></td></tr><tr style="box-sizing:border-box"><td id="gmail-L324" class="gmail-blob-num gmail-js-line-number" style="box-sizing:border-box;padding:0px 10px;width:50px;min-width:50px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;line-height:20px;color:rgba(27,31,35,0.298039);text-align:right;white-space:nowrap;vertical-align:top"></td></tr></tbody></table><br></div><div>When the stub is applied, the resut's dtype is determined application of the _InferDtypeWithDefault type function, which defaults, as expected, to the dtype of the input array but checks of that was overridden dtype=None kwarg as well. And the _InferNdimsReduction type function has to check the axis and keepdims arguments as well.</div><div><br></div><div>It's by no means ready for real users, but I hope this is a useful place to build from. Any feedback or contributions would be appreciated.</div><div><br></div><div>-Robert</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 28, 2017 at 2:04 PM, Stephan Hoyer <span dir="ltr"><<a href="mailto:shoyer@gmail.com" target="_blank">shoyer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span class=""><div class="gmail_quote"><div dir="ltr">On Tue, Nov 28, 2017 at 5:11 PM Robert T. McGibbon <<a href="mailto:rmcgibbo@gmail.com" target="_blank">rmcgibbo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm strongly in support of this proposal.  Type annotations have really helped me write more correct code.<div><br></div><div>I started working on numpy type stubs a few months ago. I needed a mypy plugin to support shape-aware functions. Those whole thing is pretty tricky. Still very WIP, but I'll clean them up a little bit and opensource it shortly.</div></div></blockquote><div><br></div></div></span><div dir="ltr"><div class="gmail_quote"><div>Great to hear -- I'd love to see what this looks like, or hear any lessons you learned from the experience!</div><div><br></div><div>Actual experience using and writing such a type checker gives you a valuable perspective to share, as opposed to my speculation.</div></div></div><div dir="ltr"><div class="gmail_quote"><div><br></div><div>Cheers,</div><div>Stephan</div></div></div></div>
<br>______________________________<wbr>_________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/numpy-<wbr>discussion</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>-Robert</div></div></div>
</div></div>