How to properly use mypy with PyTorch?

Hi there,
Could anyone please provide an example on how to setup mypy to properly work with PyTorch?
I’m currently using :

[mypy-torch.*]
# https://github.com/pytorch/pytorch/issues/42787#issuecomment-672419289  
implicit_reexport = True

I am aware that typing with PyTorch is still a work in progress. So it would be very much appreciated if additional suggested flags could come with some explanation as to why we currently need them instead of pure strict.
If you also feel this could be useful in your project, please comment or like this post.
Thanks!

Hi,

You can check our own mypy ini file here: https://github.com/pytorch/pytorch/blob/master/mypy.ini
It is updated every time we add typing to new part of our code base.

1 Like

Is pytorch + mypy working out for you? I’ve got a very simple example that gives me trouble.

import torch.nn as nn

class Foo(nn.Module):
    pass

mypy complains:

error: Class cannot subclass "Module" (has type "Any")
1 Like

I have the same issue. Is the only solution to ignore the mypy error using something like # type: ignore?