Pytorch type annotations are buggy: how to suppress mypy errors

username: bluehood
cooked: <p>Hi,<br>
consider this snippet:</p>
<pre><code class="lang-python">import torch as to

def test_div() -&gt; None:
    a: to.Tensor = to.arange(3)
    a.div_(2)
    assert to.equal(a, to.LongTensor([0, 0, 1]))

if __name__ == "__main__":
    test_div()
</code></pre>
<p>This snippet, which runs correctly, produces two mypy errors:</p>
<pre><code class="lang-auto">$ mypy  test.py
test.py:5: error: "Tensor" has no attribute "div_"
test.py:6: error: Too many arguments for "LongTensor"
Found 2 errors in 1 file (checked 1 source file)
</code></pre>
<p>The one way I found to suppress these errors is to pass <code>--no-site-packages --ignore-missing-imports</code> to the <code>mypy</code> invocation, but that has other side-effects that are not ideal.</p>
<p>Should type hints maybe be packaged separately until they are stable? How do people typically set up their projects to avoid these false positives?</p>
<p>Cheers,<br>
Enrico</p>

username: albanD
cooked: <p>Hi,</p>
<p>We are dropping support for python 2 in January. After that, we will be able to add proper type annotations and run mypy in CI so that will be better.</p>
<p>In the meantime, I’m not sure how to silence specific errors in mypy <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

username: bluehood
cooked: <p>Hi,<br>
thank you for the quick reply.</p>
<aside class="quote no-group" data-username="albanD" data-post="2" data-topic="61484">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>I’m not sure how to silence specific errors in mypy</p>
</blockquote>
</aside>
<p>So far I couldn’t find a combination of flags that would ignore spurious errors coming from pytorch but would not hide any other actual error.</p>
<p>My two cents: it might be better to not distribute type hints at all rather than having them in, but half-broken: on the user side it becomes quite a pain.</p>
<p>Cheers,<br>
Enrico</p>

username: albanD
cooked: <p>This is actually the first report I see of the type hints not working (maybe because very few people try to use them).<br>
The main use case for them in the current code base is to make IDEs happy with pytorch and support auto-completion.</p>

username: bluehood
cooked: <p>It could very well be that IDEs suppress errors, but checking a codebase that depends on pytorch with mypy does not work very well – e.g. the three simple lines above produce two errors.</p>
<p>I’m currently running mypy with <code>--ignore-missing-imports --no-site-packages</code> but I think that might suppress more than I’d like. I would like to opt out of pytorch type hints but apparently it’s not possible.</p>

username: albanD
cooked: <p>cc <a class="mention" href="/u/smth">@smth</a> is there someone that will take care of managing  such problems when we move to python 3 only?</p>

username: bluehood
cooked: <p>In case someone else stumbles upon this problem, this is the best I could do (suggested by the mypy devs on gitter):</p>
<pre><code class="lang-auto">[mypy]
# unrelated but typically required
ignore_missing_imports = True

[mypy-torch.*]
follow_imports = skip
follow_imports_for_stubs = True
</code></pre>
<p>Cheers,<br>
Enrico</p>